Skip to content

Flutter release builds (via flutter build apk --release) aren't optimized #136879

@mkustermann

Description

@mkustermann

It seems flutter release builds result in APKs that have dex in them (from java/kotlin) that wasn't optimized.

This can be seen as follows:

% flutter create hello && cd hello

### Build the APK
hello %  flutter build apk --release

### Examine the R8/Proguard rules used for tree shaking
hello % cat build/app/outputs/mapping/release/configuration.txt
...
# Optimization is turned off by default. ...
-dontoptimize
...


### Examine size of resulting DEX bytecode
hello % unzip -l build/app/outputs/flutter-apk/app-release.apk | grep classes.dex
   689484  1981-01-01 01:01   classes.dex

The root cause is that flutter's gradle file explicitly tells the R8 to use unoptimized android proguard rules.

If we fix this via:

diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
index f3c1a4145c..f5950024e2 100644
--- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
+++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
@@ -308,7 +308,7 @@ class FlutterPlugin implements Plugin<Project> {
                     shrinkResources isBuiltAsApp(project)
                     // Fallback to `android/app/proguard-rules.pro`.
                     // 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"
                 }
             }
         }

and rebuild we get the following:

hello % flutter build apk --release

hello % unzip -l build/app/outputs/flutter-apk/app-release.apk | grep classes.dex
   512376  1981-01-01 01:01   classes.dex

=> The classes.dex file shrinks by 25% (689484 -> 512376).
=> Bigger apps that use various java/kotlin plugins may benefit quite significantly (compared with the hello world)

/cc @reidbaker

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: buildBuilding flutter applications with the toolplatform-androidAndroid applications specificallyteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions