@@ -26,48 +26,28 @@ import org.gradle.work.DisableCachingByDefault
2626import javax.inject.Inject
2727
2828/* *
29- * Copies the APKs that AGP built for one variant into the flutter-apk directory, where
30- * `flutter run` and `flutter build apk` look for them, under the names the Flutter tool expects
31- * (see [apkFileName]).
29+ * Copies one variant's APKs into the flutter-apk directory, renamed to what the Flutter tool
30+ * expects (see [apkFileName]).
3231 *
33- * Every variant copies into the same flutter-apk directory, so declaring that directory as an
34- * output would make the variants' outputs overlap. The task declares the individual
35- * [outputApks] instead. Those and the files the copy writes are both named by [apkFileName] from
36- * the same inputs, so the declared outputs cannot drift from what the task produces.
32+ * Every variant writes into the same directory, so the task declares the individual
33+ * [outputApks] as outputs rather than the directory.
3734 *
38- * The task only copies files, so like Gradle's own `Copy` and `Sync` tasks, which carry the same
39- * annotation and reason, it is not cached.
40- *
41- * The task fails, and names the APK artifact (`SingleArtifact.APK`) in its error, when another
42- * plugin or build script transforms that artifact so that either:
43- * - the directory has no `output-metadata.json`. A transform registered with `toTransformMany`
44- * and `ArtifactTransformationRequest` gets the file written by AGP. A transform registered with
45- * `toTransform` must write it with `BuiltArtifacts.save`.
46- * - an APK has an ABI filter that none of the variant's outputs has.
47- *
48- * The task cannot fix either case, so the errors point at the plugin that did the transform, not
49- * at Flutter. Plugins that only read the APK artifact, or that write extra APKs after `assemble`
50- * (as channel-packaging plugins do), do not cause either failure.
35+ * Fails if another plugin transforms `SingleArtifact.APK` without writing `output-metadata.json`,
36+ * or into APKs whose ABI filters differ from the variant outputs. Flutter cannot fix either, so
37+ * the error points at that plugin.
5138 */
52- @DisableCachingByDefault(because = " Not worth caching " )
39+ @DisableCachingByDefault(because = " Copies local APKs; a cache entry would only duplicate them " )
5340abstract class CopyFlutterApksTask : DefaultTask () {
5441 /* * The variant's `SingleArtifact.APK` directory: the APKs and AGP's metadata file. */
5542 @get:InputDirectory
5643 @get:PathSensitive(PathSensitivity .RELATIVE )
5744 abstract val apkDirectory: DirectoryProperty
5845
59- /* *
60- * Reads the metadata in [apkDirectory]. Not an input itself, because [apkDirectory] already
61- * covers every file it reads.
62- */
46+ /* * Reads the metadata in [apkDirectory], which already covers its inputs. */
6347 @get:Internal
6448 abstract val builtArtifactsLoader: Property <BuiltArtifactsLoader >
6549
66- /* *
67- * The ABI of each APK the variant produces, taken from the ABI filters of the variant's
68- * outputs. An output without an ABI filter is listed as [NO_ABI]: the single APK of a build
69- * without ABI splits, or a universal APK.
70- */
50+ /* * The ABI of each variant output, or [NO_ABI] for an output without an ABI filter. */
7151 @get:Input
7252 abstract val outputAbis: ListProperty <String >
7353
@@ -80,10 +60,7 @@ abstract class CopyFlutterApksTask : DefaultTask() {
8060 @get:Input
8161 abstract val buildMode: Property <String >
8262
83- /* *
84- * The flutter-apk directory. Shared by every variant's copy task, so it is not declared as an
85- * output; [outputApks] are the declared outputs.
86- */
63+ /* * The flutter-apk directory, shared by every variant, so not an output itself. */
8764 @get:Internal
8865 abstract val destinationDir: DirectoryProperty
8966
@@ -101,37 +78,28 @@ abstract class CopyFlutterApksTask : DefaultTask() {
10178 @TaskAction
10279 fun copyApks () {
10380 val apkDir = apkDirectory.get()
104- // `load` returns null only when the directory has no `output-metadata.json`. AGP's
105- // packaging task always writes one. A plugin that transforms the APK artifact with
106- // `toTransformMany` and `ArtifactTransformationRequest` gets one written by AGP. So this
107- // means another plugin or build script replaced the artifact with `toTransform` and did
108- // not call `BuiltArtifacts.save`. An unreadable or malformed file throws instead.
81+ // `load` returns null only when `output-metadata.json` is missing.
10982 val builtArtifacts =
11083 builtArtifactsLoader.get().load(apkDir)
111- ? : throw GradleException (
112- " Flutter could not read the APK metadata in $apkDir (no output-metadata.json), " +
113- " so it cannot copy the APKs to ${destinationDir.get()} . Another Gradle " +
114- " plugin or build script in this project replaced the APK artifact " +
115- " (SingleArtifact.APK) without writing output-metadata.json. Find the " +
116- " plugin or script that transforms SingleArtifact.APK, and ask its " +
117- " maintainer to write the metadata, for example with " +
118- " ArtifactTransformationRequest or BuiltArtifacts.save(). "
84+ ? : throw transformedApkError (
85+ problem =
86+ " Flutter could not read the APK metadata in $apkDir " +
87+ " (no output-metadata.json), so it cannot copy the APKs to " +
88+ " ${destinationDir.get()} . " ,
89+ fix =
90+ " write output- metadata.json , for example with " +
91+ " ArtifactTransformationRequest or BuiltArtifacts.save()"
11992 )
12093 val declaredAbis = outputAbis.get()
12194 builtArtifacts.elements.forEach { artifact ->
12295 val abi = abiOf(artifact.filters)
123- // AGP writes one APK per variant output, and the declared ABIs come from those same
124- // outputs, so with AGP alone they always match. They differ only if another plugin
125- // or build script transforms the APK artifact into APKs with different ABI filters.
126- // Fail rather than write a file that Gradle does not track.
96+ // With AGP alone this always matches, because both come from the variant outputs.
12797 if (abi !in declaredAbis) {
128- throw GradleException (
129- " Flutter expected APKs for the ABIs $declaredAbis , but the APK metadata in " +
130- " $apkDir lists ${artifact.outputFile} for ABI '$abi '. Another Gradle " +
131- " plugin or build script in this project changed the APK artifact " +
132- " (SingleArtifact.APK). Find the plugin or script that transforms " +
133- " SingleArtifact.APK, and ask its maintainer to keep one APK per variant " +
134- " output with the same ABI filters."
98+ throw transformedApkError(
99+ problem =
100+ " Flutter expected APKs for the ABIs $declaredAbis , but the APK metadata " +
101+ " in $apkDir lists ${artifact.outputFile} for ABI '$abi '." ,
102+ fix = " keep one APK per variant output, with the same ABI filters"
135103 )
136104 }
137105 val fileName = apkFileName(abi, flavorName.orNull, buildMode.get())
@@ -147,22 +115,14 @@ abstract class CopyFlutterApksTask : DefaultTask() {
147115 /* * The [outputAbis] entry for an APK that has no ABI filter. */
148116 const val NO_ABI : String = " "
149117
150- /* *
151- * The ABI that [filters] select, or [NO_ABI] if they have no ABI filter.
152- *
153- * Used both for the variant outputs at configuration time and for the built APKs at
154- * execution time, so the two always agree on what "no ABI" means.
155- */
118+ /* * The ABI that [filters] select, or [NO_ABI] if they have no ABI filter. */
156119 internal fun abiOf (filters : Collection <FilterConfiguration >): String =
157120 filters.find { it.filterType == FilterConfiguration .FilterType .ABI }?.identifier ? : NO_ABI
158121
159122 /* *
160- * The file name the Flutter tool expects for an APK:
161123 * `app[-<abi>][-<flavor>]-<build mode>.apk`, with the flavor in lower case.
162124 *
163- * Must stay in sync with `listApkPaths` in
164- * `packages/flutter_tools/lib/src/android/gradle.dart`, which is how the Flutter tool
165- * finds these files.
125+ * Must match `listApkPaths` in `packages/flutter_tools/lib/src/android/gradle.dart`.
166126 */
167127 internal fun apkFileName (
168128 abi : String ,
@@ -173,5 +133,15 @@ abstract class CopyFlutterApksTask : DefaultTask() {
173133 val flavorPart = if (flavorName.isNullOrEmpty()) " " else " -${flavorName.lowercase()} "
174134 return " app$abiPart$flavorPart -$buildMode .apk"
175135 }
136+
137+ private fun transformedApkError (
138+ problem : String ,
139+ fix : String
140+ ): GradleException =
141+ GradleException (
142+ " $problem Another Gradle plugin or build script in this project transforms the " +
143+ " APK artifact (SingleArtifact.APK). Find the plugin or script that transforms " +
144+ " SingleArtifact.APK, and ask its maintainer to $fix ."
145+ )
176146 }
177147}
0 commit comments