-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Ensure flutter build apk --release optimizes+shrinks platform code
#136880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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). [0] f098de1
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie or stuartmorgan on the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
@reidbaker Are you the right person to review or do you have a suggestion for someone else who could take a look? |
|
Note, proguard changes are hard to debug and this will silently apply to all users. We should make sure to pay attention to reports from users if they update flutter and run into proguard configuration issues. Also this should be listed in our release notes for the next version. Thanks for adding this change btw, for most users this should be a win. |
Very true, some plugins and apps will run into trouble with this. But users of apps/plugins can then craft the right proguard rules or opt-out entirely again by using the unoptimized android config via updating android {
...
buildTypes {
release {
...
+ proguardFiles(
+ getDefaultProguardFile("proguard-android.txt"),
+ )
}
}
}Though if we don't do this here, we basically prevent all apps from being optimized and make them unnecessarily big. That seems a very poor choice - especially given that app size is a focus for many users (and our teams). |
|
@reidbaker I couldn't find a |
…orm code" (#137433) Reverts #136880 Initiated by: camsim99 This change reverts the following previous change: Original Description: 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). [0] f098de1 Fixes #136879
|
After some debugging by @gmackall, it looks like this PR caused a failure in the But we also see a failure indicating that the Kotlin Gradle Plugin version of Flutter apps would need to be increased to accomodate this change as well: @mkustermann @reidbaker Reverting this PR for now, let us know if we can help provide any further context! |
Looking at R8 code it seems like an
@reidbaker Is the Kotlin Gradle Plugin backwards compatible? (seems like minor version update to kotlin metadata format - i.e. backwards compatible) If so maybe we can update that plugin? |
|
We have run into downstream effects when bumping other dependencies that also depend on the kotlin library. Something about duplicate class definitions if customer apps use an old version of kotlin. Which most do because it is in our templates. |
Roll Flutter from c555599 to 5907c97 (45 revisions) flutter/flutter@c555599...5907c97 2023-10-27 [email protected] Add `isLogicalKeyPressed` to `KeyEvent` (flutter/flutter#136856) 2023-10-27 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Ensure `flutter build apk --release` optimizes+shrinks platform code" (flutter/flutter#137433) 2023-10-27 [email protected] Roll Flutter Engine from bedc49efc85c to a198ad4e740d (20 revisions) (flutter/flutter#137429) 2023-10-27 [email protected] Make `SemanticsNode.isMergedIntoParent` Readonly (flutter/flutter#137304) 2023-10-27 [email protected] Manual roll Flutter Engine from bedc49efc85c to 71e1a0430232 (16 revisions) (flutter/flutter#137422) 2023-10-27 [email protected] Roll Packages from fea24c5 to 2af6954 (5 revisions) (flutter/flutter#137421) 2023-10-27 [email protected] Run tests on either macOS 12 or 13 (flutter/flutter#137365) 2023-10-27 [email protected] Revert "Update `DataTable` test when data row is pressed for Material 3 (#137230)" (flutter/flutter#137407) 2023-10-27 [email protected] Revert "Reland - Update `OutlinedButton` tests for Material 3 (#136809) (#137247)" (flutter/flutter#137406) 2023-10-27 [email protected] Ensure `flutter build apk --release` optimizes+shrinks platform code (flutter/flutter#136880) 2023-10-27 [email protected] Update `DataTable` test when data row is pressed for Material 3 (flutter/flutter#137230) 2023-10-27 [email protected] Reland - Update `OutlinedButton` tests for Material 3 (#136809) (flutter/flutter#137247) 2023-10-27 [email protected] update asset manifest file name referenced in `WebServiceWorker` (flutter/flutter#135954) 2023-10-27 [email protected] give `throwsToolExit` a more useful description (flutter/flutter#136694) 2023-10-27 [email protected] Add ConstrainedLayoutBuilder.updateShouldRebuild() (flutter/flutter#136691) 2023-10-27 [email protected] Fix. typos (flutter/flutter#137325) 2023-10-27 [email protected] Roll Flutter Engine from 87f384c2d70b to bedc49efc85c (1 revision) (flutter/flutter#137382) 2023-10-26 [email protected] Fix Typos (flutter/flutter#137292) 2023-10-26 [email protected] AnimationController should dispatch creation in constructor. (flutter/flutter#134839) 2023-10-26 [email protected] Roll Flutter Engine from 9788bb9ff83e to 87f384c2d70b (4 revisions) (flutter/flutter#137380) 2023-10-26 [email protected] Roll Flutter Engine from ce1c1ee54107 to 9788bb9ff83e (3 revisions) (flutter/flutter#137373) 2023-10-26 [email protected] Marks Windows_arm64 windows_startup_test to be unflaky (flutter/flutter#137228) 2023-10-26 [email protected] Marks Windows_arm64 flutter_tool_startup__windows to be unflaky (flutter/flutter#137229) 2023-10-26 [email protected] Marks Windows_arm64 windows_home_scroll_perf__timeline_summary to be unflaky (flutter/flutter#137221) 2023-10-26 [email protected] Declare dependency on copyFlutterAssetsTask in bundleAarTask (flutter/flutter#137370) 2023-10-26 [email protected] Marks Windows_arm64 complex_layout_win_desktop__start_up to be unflaky (flutter/flutter#137225) 2023-10-26 [email protected] Roll Flutter Engine from 394744d2c4d0 to ce1c1ee54107 (2 revisions) (flutter/flutter#137367) 2023-10-26 [email protected] Marks Windows_arm64 hot_mode_dev_cycle_win_target__benchmark to be unflaky (flutter/flutter#137217) 2023-10-26 [email protected] Marks Windows_arm64 flutter_view_win_desktop__start_up to be unflaky (flutter/flutter#137226) 2023-10-26 [email protected] Marks Windows_arm64 platform_view_win_desktop__start_up to be unflaky (flutter/flutter#137227) 2023-10-26 [email protected] Marks Windows_arm64 flutter_gallery_win_desktop__start_up to be unflaky (flutter/flutter#137224) 2023-10-26 [email protected] Marks Windows_arm64 hello_world_win_desktop__compile to be unflaky (flutter/flutter#137222) 2023-10-26 [email protected] Marks Windows_arm64 flutter_gallery_win_desktop__compile to be unflaky (flutter/flutter#137223) 2023-10-26 [email protected] Marks Windows_arm64 run_release_test_windows to be unflaky (flutter/flutter#137220) 2023-10-26 [email protected] Fix dislocated doc and comment on ThemeData localize cache (flutter/flutter#137315) 2023-10-26 [email protected] Marks Windows_arm64 platform_channel_sample_test_windows to be unflaky (flutter/flutter#137218) 2023-10-26 [email protected] Roll Flutter Engine from 7c5c8f587992 to 394744d2c4d0 (1 revision) (flutter/flutter#137364) 2023-10-26 [email protected] Roll Flutter Engine from 9363fe6ba503 to 7c5c8f587992 (3 revisions) (flutter/flutter#137363) 2023-10-26 [email protected] Roll Flutter Engine from 542f8bc4c019 to 9363fe6ba503 (2 revisions) (flutter/flutter#137354) 2023-10-26 [email protected] Unified analytics events for doctor validators (flutter/flutter#136647) 2023-10-26 [email protected] Roll Flutter Engine from d8132d5070bf to 542f8bc4c019 (2 revisions) (flutter/flutter#137349) 2023-10-26 [email protected] Marks Windows_arm64 run_debug_test_windows to be unflaky (flutter/flutter#137219) 2023-10-26 [email protected] Roll Flutter Engine from 0a6253dbfafd to d8132d5070bf (1 revision) (flutter/flutter#137347) 2023-10-26 [email protected] Roll Flutter Engine from 5da115661f01 to 0a6253dbfafd (1 revision) (flutter/flutter#137341) 2023-10-26 [email protected] Roll Flutter Engine from 61ae5ef94e9c to 5da115661f01 (1 revision) (flutter/flutter#137340) ...
The R8 team confirmed this. Seems like the bug has been seemingly fixed for over a year. The reason we run into this bug is because the flutter project uses an old android gradle plugin (i.e. nothing to do with kotlin gradle plugin version) - because R8 is shipped as part of android gradle plugin.
So to reproduce the failure one has to do roughly this: Running The way one can fix this is to basically upgrading the android gradle plugin, which for me required: (There's no need to update
It seems like a big problem if we generate templates with hard-coded versions in them and users don't upgrade. It makes user's apps have old gradle android plugin, which makes users not get bugfixes / performance improvements, ... It also seems problematic because users old gradle deps depend on newest flutter gradle plugin, so the flutter gradle plugin has to operate in an environment of old deps itself as well. @reidbaker Is there a plan forward how to get our users onto the latest versions? Or is the solution here to just update the android gradle plugin for our CI and re-land this change? |
|
Getting users to update versions of software we don't ship is a constant struggle. We have a few patterns for helping users here. Sometimes we print build warnings and sometimes for things that we know will break we will do auto migrations. Specifically for agp we have been building tooling that evaluates incompatibilitys with other dependencies since that can be hard for flutter users to figure out. Also there is active work to set a minimum ago version for flutter projects. That said the bulk of your concern is valid. We generate a lot of versions in our template files and do not and for many can not have a good way to update them automatically since they are project level files and changing the values for existing projects can cause breakages or bugs. |
|
To clarify: To make this PR re-land without causing the diff --git a/packages/flutter_tools/lib/src/android/gradle_utils.dart b/packages/flutter_tools/lib/src/android/gradle_utils.dart
index 14a3d1462b..3145fea00a 100644
--- a/packages/flutter_tools/lib/src/android/gradle_utils.dart
+++ b/packages/flutter_tools/lib/src/android/gradle_utils.dart
@@ -26,8 +26,8 @@ import 'android_sdk.dart';
// However, this currently requires to migrate existing integration tests to the latest supported values.
//
// Please see the README before changing any of these values.
-const String templateDefaultGradleVersion = '7.5';
-const String templateAndroidGradlePluginVersion = '7.3.0';
+const String templateDefaultGradleVersion = '8.0';
+const String templateAndroidGradlePluginVersion = '8.1.0';
const String templateAndroidGradlePluginVersionForModule = '7.3.0';
const String templateKotlinGradlePluginVersion = '1.7.10'; |
|
Following up on this because there has been more discussion, I think this would be a good change that we should try to re-land. The error message for the bad AGP versions is unfortunately a bit opaque, though, and made more so by the fact that we are handling the definition of the proguard file. So it would be significantly harder for a flutter developer to identify this root cause without knowledge of the inner workings of the flutter gradle plugin. Concrete things that could be added that would make me comfortable with landing:
|
|
@gmackall Sounds promising. Who would be a good owner for this? |
…m code" (#153868) Re-lands #136880, fixes #136879. Additions to/things that are different from the original PR: - Adds an entry to `gradle_errors.dart` that tells people when they run into the R8 bug because of using AGP 7.3.0 (https://issuetracker.google.com/issues/242308990). - Previous PR moved templates off of AGP 7.3.0. - Packages repo has been moved off AGP 7.3.0 (flutter/packages#7432). Also, unrelatedly: - Deletes an entry in `gradle_errors.dart` that informed people to build with `--no-shrink`. This flag [doesn't do anything](flutter/website#11022 (comment)), so it can't be the solution to any error. - Uniquely lowers the priority of the `incompatibleKotlinVersionHandler`. This is necessary because the ordering of the errors doesn't fully determine the priority of which handler we decide to use, but also the order of the log lines. The kotlin error lines often print before the other error lines, so putting it last in the list of handlers isn't sufficient to lower it to be the lowest priority handler.
…m code" (flutter#153868) Re-lands flutter#136880, fixes flutter#136879. Additions to/things that are different from the original PR: - Adds an entry to `gradle_errors.dart` that tells people when they run into the R8 bug because of using AGP 7.3.0 (https://issuetracker.google.com/issues/242308990). - Previous PR moved templates off of AGP 7.3.0. - Packages repo has been moved off AGP 7.3.0 (flutter/packages#7432). Also, unrelatedly: - Deletes an entry in `gradle_errors.dart` that informed people to build with `--no-shrink`. This flag [doesn't do anything](flutter/website#11022 (comment)), so it can't be the solution to any error. - Uniquely lowers the priority of the `incompatibleKotlinVersionHandler`. This is necessary because the ordering of the errors doesn't fully determine the priority of which handler we decide to use, but also the order of the log lines. The kotlin error lines often print before the other error lines, so putting it last in the list of handlers isn't sufficient to lower it to be the lowest priority handler.
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 ofproguard-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.dexof the APK).[0] f098de1
Fixes #136879