Unverified Commit 1dd21f8d authored by Martin Kustermann's avatar Martin Kustermann Committed by GitHub

Ensure `flutter build apk --release` optimizes+shrinks platform code (#136880)

Since the original PR that supposedly enabled proguard, it was using the
android proguard rules that disable optimizations. See initial PR in [0]

This PR changes the flutter gradle plugin to use the
`proguard-android-optimize.txt` (instead of `proguard-android.txt`)
which will enable optimizations/shrinking of platform code (i.e.
java/kotlin).

For a simple flutter hello world this results in a 25% reduction in the
resulting DEX file (`classes.dex` of the APK).

Note for users:

For some users this may result in issues because their java/kotlin code is
now better optimized & tree shaken and thereby symbols may be no longer
available or being obfuscated.

To fix those issues it's best to craft precise proguard rules describing the
extra symbols that are needed by the app (see [1]). But it's also possible to
opt out entirely of optimizations by using the unoptimized proguard rules.

To add custom proguard rules or use the unoptimized android rules, one can
update `android/app/build.gradle`:
```
android {
    ...
    buildTypes {
        release {
            ...
+            proguardFiles(
+                // Not ideal: Disables optimizations by using unoptimized android rules.
+                getDefaultProguardFile("proguard-android.txt"),
+
+                // Better: Have precise keep rules to only keep things that are needed.
+                "custom-rules.pro",
+            )
        }
    }
}
```


[0] f098de1f
[1] https://developer.android.com/build/shrink-code

Fixes https://github.com/flutter/flutter/issues/136879
parent 64f31b2f
...@@ -308,7 +308,7 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -308,7 +308,7 @@ class FlutterPlugin implements Plugin<Project> {
shrinkResources isBuiltAsApp(project) shrinkResources isBuiltAsApp(project)
// Fallback to `android/app/proguard-rules.pro`. // Fallback to `android/app/proguard-rules.pro`.
// This way, custom Proguard rules can be configured as needed. // This way, custom Proguard rules can be configured as needed.
proguardFiles project.android.getDefaultProguardFile("proguard-android.txt"), flutterProguardRules, "proguard-rules.pro" proguardFiles project.android.getDefaultProguardFile("proguard-android-optimize.txt"), flutterProguardRules, "proguard-rules.pro"
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment