flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
164.83k stars 27.16k forks source link

Inject proguard rules after Dart AOT compilation to gradle #154170

Open HosseinYousefi opened 2 weeks ago

HosseinYousefi commented 2 weeks ago

Once the Dart code is compiled in AOT mode, we can understand which methods and classes are used from the jnigen generated bindings and can use that to generate proguard rules. The tree-shaking information is accessible through hook/link.dart.

I'm not sure how the release build pipeline looks like, for example if the process starts with gradle can we then inject the generated proguard rules mid build?

Context: https://github.com/dart-lang/native/issues/681

@gmackall @reidbaker @dcharkes @mkustermann

dcharkes commented 2 weeks ago

We need to support two flows:

We need to support two flows:

So that means that we only know about the proguard rules halfway during the Gradle build. (Flutter assemble invokes the build and link hooks in the native assets target: lib/src/build_system/targets/native_assets.dart)

I'm not sure how the release build pipeline looks like, for example if the process starts with gradle can we then inject the generated proguard rules mid build?

I don't know much about Gradle.

gmackall commented 2 weeks ago

I'm not sure how the release build pipeline looks like, for example if the process starts with gradle can we then inject the generated proguard rules mid build?

Yes, I believe this should be possible.

The current general ordering of steps is (with more before and after): ...

  1. The Flutter tool invokes Gradle.
  2. Gradle invokes our FlutterPlugin (a Gradle plugin).
  3. Our FlutterPlugin invokes the Flutter tool again. ...

See the somewhat outdated go/flutter-build-android-app-lifecycle (flutter.gradle -> flutter.groovy).

Also, we currently configure proguard rules here, as a breadcrumb.

reidbaker commented 2 weeks ago

I would add that google/android has a default proguard file that is included as part of AGP. https://www.zacsweers.dev/android-proguard-rules/

reidbaker commented 1 week ago

@HosseinYousefi what parts of this feature are you wanting team-android to do? @gmackall mentioned in triage that this is likely tracking work you are actively doing.

HosseinYousefi commented 1 week ago

@HosseinYousefi what parts of this feature are you wanting team-android to do? @gmackall mentioned in triage that this is likely tracking work you are actively doing.

I'm currently working on #154165. I'll work on this next. I'll reach out to y'all if I had problems.