Skip to content

Enable Proguard by default on release mode#39986

Merged
blasten merged 12 commits intoflutter:masterfrom
blasten:proguard
Sep 11, 2019
Merged

Enable Proguard by default on release mode#39986
blasten merged 12 commits intoflutter:masterfrom
blasten:proguard

Conversation

@blasten
Copy link

@blasten blasten commented Sep 6, 2019

Description

Add --proguard flag to flutter build apk and flutter build appbundle and enable it by default for release builts.

This change makes it possible to add custom rules by placing the file proguard-rules.pro in the android/ directory in case a plugin requires special rules.

Size in bytes

hello_world flutter_gallery
with proguard 4,473,289 41,522,555
without proguard 4,524,765 41,820,143

cc @Hixie

Related Issues

Fixes #11099

Tests

  • build_apk_test.dart

  • build_appbundle_test.dart

These tests aren't hermetic yet, but these are the first ones to test the calls to Gradle.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Sep 6, 2019
@codecov
Copy link

codecov bot commented Sep 6, 2019

Codecov Report

Merging #39986 into master will decrease coverage by 0.23%.
The diff coverage is 78.26%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #39986      +/-   ##
==========================================
- Coverage   58.56%   58.33%   -0.24%     
==========================================
  Files         192      194       +2     
  Lines       18443    18749     +306     
==========================================
+ Hits        10801    10937     +136     
- Misses       7642     7812     +170
Flag Coverage Δ
#flutter_tool 58.33% <78.26%> (-0.24%) ⬇️
Impacted Files Coverage Δ
packages/flutter_tools/lib/src/build_info.dart 77.04% <ø> (+1.63%) ⬆️
...lutter_tools/lib/src/commands/build_appbundle.dart 100% <100%> (ø) ⬆️
packages/flutter_tools/lib/src/context_runner.dart 71.69% <100%> (+0.54%) ⬆️
...ages/flutter_tools/lib/src/commands/build_apk.dart 100% <100%> (ø) ⬆️
packages/flutter_tools/lib/src/android/gradle.dart 74.27% <74.35%> (+11.88%) ⬆️
...ackages/flutter_tools/lib/src/commands/doctor.dart 28.57% <0%> (-35.72%) ⬇️
...lutter_tools/lib/src/android/android_emulator.dart 32.65% <0%> (-30.62%) ⬇️
.../flutter_tools/lib/src/windows/windows_device.dart 30% <0%> (-20%) ⬇️
...lib/src/build_runner/web_compilation_delegate.dart 14.15% <0%> (-11.24%) ⬇️
...ackages/flutter_tools/lib/src/reporting/usage.dart 77.85% <0%> (-10.74%) ⬇️
... and 37 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1222926...f2ecef3. Read the comment docs.

@blasten
Copy link
Author

blasten commented Sep 6, 2019

Now I just realized that coverage report has false positives. The decreased coverage in some files is just the result of moving code, so it can be reused.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple nits - was there some reason we didn't enable this in the past? I was under the impression it could be destructive in some way for plugins that would be non-obvious for new users. I see a way to override that for plugins here, but will that be something that (new) users will be able to understand and use correctly?

@blasten
Copy link
Author

blasten commented Sep 9, 2019

If Proguard failed, the tool will log the event and tell the user to use --no-proguard.

Screen Shot 2019-09-09 at 10 37 18 AM

If we detect that a lot of folks are seeing warnings from Proguard, we could flip the default value and scoped down the issues. For instance: #37839.

@dnfield
Copy link
Contributor

dnfield commented Sep 9, 2019

Wouldn't the failure potentially happen at runtime though? E.g. if proguard removed a class you actually want to use but it thought was safe to remove.

@blasten
Copy link
Author

blasten commented Sep 9, 2019

@dnfield I can only think of JNI calls or reflection. This won't be an issue in the engine since the rules are configured, but if a plugin is doing something similar then that plugin should use the @Keep annotation or provide Proguard rules. Are you aware of any plugin that might be doing something like that? This might need better docs from our side.

@dnfield
Copy link
Contributor

dnfield commented Sep 9, 2019

I'm not sure about any plugins using reflection. I may be mis-remembering, but I had a vague notion that the way we do the plugin registration could end up having proguard shake out plugin code we want to keep.

Is there a test that covers this flag with a plugin enabled?

@blasten
Copy link
Author

blasten commented Sep 9, 2019

Is there a test that covers this flag with a plugin enabled?

yes, the Flutter gallery. I also tested manually and on Firebase device lab.

we do the plugin registration could end up having proguard shake out plugin code we want to keep.

AFAICT, GeneratedPluginRegistrant imports each of the plugins and calls the registerWith method on the main plugin class.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit

Copy link
Member

@zanderso zanderso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm w/ nits

@blasten blasten merged commit f098de1 into flutter:master Sep 11, 2019
@blasten blasten deleted the proguard branch September 11, 2019 00:23
@rostopira
Copy link
Contributor

rostopira commented Sep 11, 2019

This broke my build:

Initializing gradle...                                              0.5s
Resolving dependencies...                                           2.5s
                                                                        
FAILURE: Build failed with an exception.                                
                                                                        
* Where:                                                                
Script '/Users/rostopira/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 163
                                                                        
* What went wrong:                                                      
A problem occurred evaluating script.                                   
> Failed to apply plugin [class 'FlutterPlugin']                        
   > No signature of method: static FlutterPlugin.useProguard() is applicable for argument types: (Boolean) values: [true]
                                                                        
* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
                                                                        
* Get more help at https://help.gradle.org   

@blasten
Copy link
Author

blasten commented Sep 11, 2019

@rostopira what version of Gradle and the Android Gradle plugin are you using?

This issue is being discussed on #40213

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable ProGuard for Android in release mode

7 participants