Skip to content

Commit

Permalink
android: preprocess impeller code in gradle
Browse files Browse the repository at this point in the history
full impeller support requires flutter 3.24, android breaks source code compatibility again. no conditional compilation in java, preprocess manually
  • Loading branch information
wang-bin committed Aug 29, 2024
1 parent e67d3b7 commit e2d2744
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ jobs:
fail-fast: false
matrix:
host: [ubuntu]
version: ['3.22.x', 'any']
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- uses: subosito/flutter-action@v2
with:
flutter-version: ${{ matrix.version }}
channel: 'stable'
cache: true
- uses: actions/setup-java@v4
Expand All @@ -183,7 +185,7 @@ jobs:
- name: Upload
uses: actions/upload-artifact@v4
with:
name: fvp-example-android-${{ matrix.host }}
name: fvp-example-android-${{ matrix.host }}-${{ matrix.version }}
path: example/fvp_example_android.apk


Expand Down
38 changes: 37 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ android {
}

defaultConfig {
minSdkVersion 21 // lower version error in ndk 26
minSdkVersion 21 // lower version error in ndk 26. requires flutter > 3.19

externalNativeBuild {
cmake {
Expand Down Expand Up @@ -86,3 +86,39 @@ android {
}
}
}

def flutterSdkVersion = {
def properties = new Properties()
file(rootDir.absolutePath + "/local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
if (flutterSdkPath == null) {
flutterSdkPath = System.env.FLUTTER_ROOT // from flutter.groovy
}
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
def version = file(flutterSdkPath + "/version")
assert version.exists(), "flutter version file not found"
return version.text.trim()
}()

println "flutterSdkVersion: ${flutterSdkVersion}"

def preprocessJava(Map textMap) {
def f = file('src/main/java/com/mediadevkit/fvp/FvpPlugin.java')
assert f.exists(), path + "not found"
def content = f.text
def newContent = content
textMap.forEach { oldText, newText ->
newContent = newContent.replaceAll(oldText, newText)
}
if (content != newContent) {
println "rewrite " + f.path
f.write(newContent)
}
}
// VersionNumber is deprecated
if (Float.parseFloat(flutterSdkVersion.replaceAll('\\.[0-9]*$', '')) < 3.24) {
println 'Impeller is not perfect for flutter < 3.24'
preprocessJava(['//// FLUTTER_3.24_BEGIN': '/\\*// FLUTTER_3.24_BEGIN-', '//// FLUTTER_3.24_END': '\\*/// FLUTTER_3.24_END-'])
} else {
preprocessJava(['/\\*// FLUTTER_3.24_BEGIN-': '//// FLUTTER_3.24_BEGIN', '\\*/// FLUTTER_3.24_END-': '//// FLUTTER_3.24_END'])
}
2 changes: 2 additions & 0 deletions android/src/main/java/com/mediadevkit/fvp/FvpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
textures.put(texId, te);
surfaces.put(texId, surface);
result.success(texId);
//// FLUTTER_3.24_BEGIN
if (sp != null) { // FIXME: requires 3.24. how to build conditionally?
// 3.24: https://docs.flutter.dev/release/breaking-changes/android-surface-plugins
sp.setCallback(
Expand All @@ -109,6 +110,7 @@ public void onSurfaceDestroyed() {
}
);
}
//// FLUTTER_3.24_END
} else if (call.method.equals("ReleaseRT")) {
final int texId = call.argument("texture"); // 32bit int, 0, 1, 2 .... but SurfaceTexture.id() is long
final long texId64 = texId; // MUST cast texId to long, otherwise remove() error
Expand Down

0 comments on commit e2d2744

Please sign in to comment.