From f964f15dcb3d585d8a87515c9d65e3a481a0f526 Mon Sep 17 00:00:00 2001 From: Gray Mackall <34871572+gmackall@users.noreply.github.com> Date: Tue, 10 Sep 2024 09:42:22 -0700 Subject: [PATCH] Fix `flutter build aar` for modules that use a plugin (#154757) https://github.com/flutter/flutter/pull/151675 bumped module templates to AGP 8.1. In doing so, I tried to work around a behavior change [that was new in AGP 8.0](https://developer.android.com/build/releases/past-releases/agp-8-0-0-release-notes): > AGP 8.0 creates no SoftwareComponent by default. Instead AGP creates SoftwareComponents only for variants that are configured to be published using the publishing DSL. by using AGP's publishing DSL to define which variants to publish in the module's ephemeral gradle files: ``` android.buildTypes.all {buildType -> if (!android.productFlavors.isEmpty()) { android.productFlavors.all{productFlavor -> android.publishing.singleVariant(productFlavor.name + buildType.name.capitalize()) { withSourcesJar() withJavadocJar() } } } else { android.publishing.singleVariant(buildType.name) { withSourcesJar() withJavadocJar() } } } ``` The problem is that this doesn't get applied to the plugin projects used by the module, so if a module uses any plugin it breaks. This PR fixes that by applying similar logic, but to each project (not just the module's project). Tested manually with https://github.com/gmackall/GrayAddToApp, and also re-enabled an old test that tested this use case as a part of the PR. Fixes: https://github.com/flutter/flutter/issues/154371 --- .ci.yaml | 22 +++++++++++++ .../bin/tasks/build_aar_module_test.dart | 1 - .../gradle/aar_init_script.gradle | 33 +++++++++++++++++++ .../Flutter.tmpl/build.gradle.tmpl | 15 --------- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/.ci.yaml b/.ci.yaml index e12dcbf99d..eab5f164f9 100644 --- a/.ci.yaml +++ b/.ci.yaml @@ -424,6 +424,28 @@ targets: task_name: android_views timeout: 60 + - name: Linux build_aar_module_test + bringup: true + recipe: devicelab/devicelab_drone + timeout: 60 + properties: + add_recipes_cq: "true" + dependencies: >- + [ + {"dependency": "android_sdk", "version": "version:34v3"}, + {"dependency": "chrome_and_driver", "version": "version:125.0.6422.141"}, + {"dependency": "open_jdk", "version": "17"} + ] + tags: > + ["devicelab","hostonly"] + task_name: build_aar_module_test + scheduler: luci + runIf: + - dev/** + - packages/flutter_tools/** + - bin/** + - .ci.yaml + - name: Linux build_tests_1_5 bringup: true recipe: flutter/flutter_drone diff --git a/dev/devicelab/bin/tasks/build_aar_module_test.dart b/dev/devicelab/bin/tasks/build_aar_module_test.dart index ed9988c6ba..f493bd2682 100644 --- a/dev/devicelab/bin/tasks/build_aar_module_test.dart +++ b/dev/devicelab/bin/tasks/build_aar_module_test.dart @@ -65,7 +65,6 @@ Future main() async { ' path: ../plugin_with_android$platformLineSep' ' plugin_without_android:$platformLineSep' ' path: ../plugin_without_android$platformLineSep' - ' webcrypto: 0.5.2$platformLineSep', // Plugin that uses NDK. ); modulePubspec.writeAsStringSync(content, flush: true); diff --git a/packages/flutter_tools/gradle/aar_init_script.gradle b/packages/flutter_tools/gradle/aar_init_script.gradle index e6fa84cc17..aa6bac1a39 100644 --- a/packages/flutter_tools/gradle/aar_init_script.gradle +++ b/packages/flutter_tools/gradle/aar_init_script.gradle @@ -116,6 +116,39 @@ allprojects { apply plugin: "maven-publish" } +afterProject { project -> + // Exit early if either: + // 1. The project doesn't have the Android Gradle plugin applied. + // 2. The project has already defined which variants to publish (trying to re-define which + // variants to publish will result in an error). + if (!project.hasProperty("android")) { + return + } + if (project.android.publishing.singleVariants.size() != 0) { + return + } + + Closure addSingleVariants = {buildType -> + if (!project.android.productFlavors.isEmpty()) { + project.android.productFlavors.all{productFlavor -> + project.android.publishing.singleVariant( + productFlavor.name + buildType.name.capitalize() + ) { + withSourcesJar() + withJavadocJar() + } + } + } else { + project.android.publishing.singleVariant(buildType.name) { + withSourcesJar() + withJavadocJar() + } + } + } + + project.android.buildTypes.all(addSingleVariants) +} + projectsEvaluated { assert rootProject.hasProperty("is-plugin") if (rootProject.property("is-plugin").toBoolean()) { diff --git a/packages/flutter_tools/templates/module/android/library_new_embedding/Flutter.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/module/android/library_new_embedding/Flutter.tmpl/build.gradle.tmpl index a128098b76..9a10bea781 100644 --- a/packages/flutter_tools/templates/module/android/library_new_embedding/Flutter.tmpl/build.gradle.tmpl +++ b/packages/flutter_tools/templates/module/android/library_new_embedding/Flutter.tmpl/build.gradle.tmpl @@ -47,21 +47,6 @@ android { } } -android.buildTypes.all {buildType -> - if (!android.productFlavors.isEmpty()) { - android.productFlavors.all{productFlavor -> - android.publishing.singleVariant(productFlavor.name + buildType.name.capitalize()) { - withSourcesJar() - withJavadocJar() - } - } - } else { - android.publishing.singleVariant(buildType.name) { - withSourcesJar() - withJavadocJar() - } - } -} flutter { source = "../.."