Skip to content

Commit 8c9a54d

Browse files
Unify add-to-app module wiring on the variant API
Add-to-app module (library) variants use the same androidComponents.onVariants path as application variants: the compile, assets and jniLibs tasks are registered lazily per library variant, and the assets and native libraries are generated source directories of the variant. The host app consumes them like any Android library. Deletes the host-project lookup, the libraryVariants x applicationVariants cross-wiring, the merge<HostVariant>Assets.dependsOn edge, addFlutterDepsForModule, addCopyFlutterAssetsDependency, shouldConfigureFlutterTask(Project, Task) and buildModeFor(com.android.builder.model.BuildType). flutter.hostAppProjectName has no effect and logs a warning. The jniLibs copy is wired from the compile task provider, so its input directory is required in both copy tasks.
1 parent 33f3987 commit 8c9a54d

12 files changed

Lines changed: 525 additions & 374 deletions

File tree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError
22
android.useAndroidX=true
3-
flutter.hostAppProjectName=SampleApp
43
android.newDsl=false

‎docs/platforms/android/Migrating-Flutter-Gradle-Plugin-to-AGP-public-API.md‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ projects. That opt-out dies with AGP 10.
9999
5. **`buildModeFor` semantics.** Every variant-scope call uses the
100100
`(name, debuggable)` overload with the public `Component.debuggable`.
101101
Name-based inference is confined to the one DSL-scope case with no public
102-
signal (the library-plugin build-type copy in `PluginHandler`). This
103-
preserves add-to-app custom-debuggable matching (a host `staging`
104-
debuggable build type maps to debug engine artifacts).
102+
signal (the library-plugin build-type copy in `PluginHandler`). In an
103+
add-to-app module the call reads the module's own variant, so the build mode
104+
follows the module variant that the host's `matchingFallbacks` select (see
105+
"Features that must break" item 6).
105106
6. **P3 pre-spike / PR 4 (afterEvaluate DSL mutation under newDsl).** The planned scratch-app
106107
spike (AGP 9.1 + `newDsl=true` + custom build type, verifying that build-type
107108
creation from `pluginProject.afterEvaluate` still works) could not run in the
@@ -237,12 +238,36 @@ else serializes through `FlutterPlugin.kt` / `FlutterPluginUtils.kt`.
237238
but C++ debugging will be broken for those custom build types.
238239
5. **Asset merge**: flutter assets become a merged source dir instead of a
239240
post-merge overwrite; collisions resolve by AGP source-set priority.
240-
6. **Add-to-app**: the explicit `:app:merge<V>Assets.dependsOn` edge and
241-
host-project lookup are removed; `flutter.hostAppProjectName` becomes a
242-
no-op with a deprecation warning naming a removal milestone. Build scripts that
243-
reference Flutter's `copyFlutterAssets<V>` tasks by name (e.g. `tasks.getByPath(...)`)
244-
will crash with a `Task with path ... not found` error because the tasks are now
245-
registered lazily.
241+
6. **Add-to-app**: the module registers its Flutter compile, assets and jniLibs tasks in
242+
its own `onVariants`, and adds the staged assets and native libraries as generated
243+
source directories of each library variant. The host app consumes them like the assets
244+
of any Android library, so the host-project lookup, the `libraryVariants` ×
245+
host `applicationVariants` loop and the explicit `:app:merge<V>Assets.dependsOn`
246+
edge are removed. Effects:
247+
- `flutter.hostAppProjectName` has no effect. Setting it logs a warning that says so
248+
and names no removal milestone. A module whose host project is not `:app` does not
249+
need it.
250+
- Every module variant (`debug`, `profile`, `release`) is configured, whatever the
251+
command line names. A host task such as `:app:assembleDemoStaging` cannot be mapped
252+
to the module variant it consumes, which AGP selects through the host's
253+
`matchingFallbacks`. The tasks are registered lazily, so a build runs only the
254+
module variant the host consumes. One cost: a host build of only
255+
`assemble<Variant>AndroidTest` runs the module's `flutter assemble` for that
256+
variant, which the command-line check skipped for the module.
257+
- The Flutter build mode comes from the module variant the host consumes (its
258+
`debuggable` flag and the `profile` name), not from the host build type. A host
259+
build type that is debuggable but falls back to the module's `release` variant gets
260+
release Flutter artifacts. The deleted code matched module variants to host variants
261+
by build mode instead. For that host build type, built as the only task on the
262+
command line, it ran the module's debug Flutter build, which the host does not
263+
package, and no Flutter build for the module's `release` variant: the APK had no
264+
`flutter_assets` and no `libapp.so`.
265+
- The module's assets are a generated assets source directory, not a copy into the
266+
module's merged-assets output after `mergeAssets` (which the deleted code forced to
267+
re-run with `clean<MergeAssetsTask>`). Collisions resolve by source-set priority
268+
(item 5).
269+
- `copyFlutterAssets<V>` in the module is a `CopyFlutterAssetsTask`, not a `Copy`
270+
(item 7).
246271
7. **Task realization/type**: flutter tasks become lazy `TaskProvider`s, and
247272
`copyFlutterAssets<V>` changes type from `org.gradle.api.tasks.Copy` to a
248273
custom task class — `tasks.named(..., Copy::class)` casts fail.

‎docs/platforms/android/website-page-draft.md‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,27 @@ aliases:
194194

195195
### Add-to-app (Flutter module in a host app)
196196

197-
- Flutter no longer looks up or configures the host `:app` project from the
198-
module. The dependency between your host's asset merging and Flutter's asset
199-
copy is expressed through the Variant API instead of an explicit
200-
`merge<Variant>Assets.dependsOn(...)` edge. Build scripts that reference
201-
Flutter's `copyFlutterAssets<Variant>` tasks by name or type will break.
202-
For example, `tasks.getByPath(":flutter:copyFlutterAssetsDebug")` will crash
203-
your build with a `Task with path ... not found` error because the
204-
tasks are now registered lazily and are no longer of type
205-
`org.gradle.api.tasks.Copy`.
206-
- `flutter.hostAppProjectName` in `gradle.properties` is now a no-op. Flutter
207-
prints a deprecation warning naming the removal milestone. It was only used
208-
for the host-project lookup, which no longer exists.
209-
- Flutter maps host build types to Flutter build modes using the public
210-
"debuggable" flag: `profile` stays `profile`, debuggable build types map to
211-
`debug`, everything else maps to `release`. If your host has no `profile`
212-
build type, add `matchingFallbacks`:
197+
- The Flutter module adds its assets and native libraries to its own variants
198+
through the Variant API. Your host app consumes them like the assets of any
199+
Android library. Flutter does not look up or configure the host `:app`
200+
project, and adds no `merge<Variant>Assets.dependsOn(...)` edge to it.
201+
- `flutter.hostAppProjectName` in `gradle.properties` has no effect. If it is
202+
set, Flutter prints a warning that says so. You can remove it.
203+
- The Flutter module's `copyFlutterAssets<Variant>` tasks are of type
204+
`CopyFlutterAssetsTask`, not `org.gradle.api.tasks.Copy`. Build scripts that
205+
look them up with the `Copy` type fail.
206+
- The Flutter build mode comes from the module variant your host build
207+
consumes: the module's `debug` variant builds debug Flutter artifacts,
208+
`profile` builds profile, and `release` builds release. Host `debug`,
209+
`profile` and `release` build types consume the module variant of the same
210+
name. For a custom host build type, AGP picks the module variant through the
211+
build type's `matchingFallbacks`, so list the module build type you want
212+
first:
213213

214214
```kotlin
215215
create("staging") {
216216
initWith(getByName("debug"))
217-
isDebuggable = true // staging gets debug Flutter artifacts
218-
matchingFallbacks += listOf("debug", "release")
217+
matchingFallbacks += listOf("debug", "release") // staging gets debug Flutter artifacts
219218
}
220219
```
221220

0 commit comments

Comments
 (0)