[CP-stable] Cherry pick "Fix `flutter build aar` for modules that use a plugin" (#154944)

Sum of changes is:
cherry pick of https://github.com/flutter/flutter/pull/154757
Merge
manual application of of https://github.com/flutter/flutter/pull/154945
manual application of https://github.com/flutter/flutter/pull/154967

Copied form below from automatic label (this is a cherry pick of https://github.com/flutter/flutter/pull/154757).

### Issue Link:
What is the link to the issue this cherry-pick is addressing?

Fixes https://github.com/flutter/flutter/issues/154371.

### Changelog Description:
Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples

Fixes a crash when building Flutter modules for android with `flutter build aar`, when using Android Gradle Plugin 8.0+.

### Impact Description:
What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)

Fixes the mentioned crash when building a module as an aar with Android Gradle Plugin 8.0+.

### Workaround:
Is there a workaround for this issue?

No.

### Risk:
What is the risk level of this cherry-pick?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

### Validation Steps:
What are the steps to validate that this fix works?

Build a module:
1. `flutter create foobar --template=module`
2. `cd foobar`
3. Edit the `.android/build.gradle` to use AGP 8.1.
4. `flutter run`/`flutter build aar`
diff --git a/.ci.yaml b/.ci.yaml
index 4135d40..897e4e5 100644
--- a/.ci.yaml
+++ b/.ci.yaml
@@ -356,6 +356,27 @@
       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": "version:17"}
+        ]
+      tags: >
+        ["devicelab","hostonly"]
+      task_name: build_aar_module_test
+    runIf:
+      - dev/**
+      - packages/flutter_tools/**
+      - bin/**
+      - .ci.yaml
+
   - name: Linux build_tests_1_3
     recipe: flutter/flutter_drone
     timeout: 60
diff --git a/dev/devicelab/bin/tasks/build_aar_module_test.dart b/dev/devicelab/bin/tasks/build_aar_module_test.dart
index ed9988c..f493bd2 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 @@
           '    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 e6fa84c..aa6bac1 100644
--- a/packages/flutter_tools/gradle/aar_init_script.gradle
+++ b/packages/flutter_tools/gradle/aar_init_script.gradle
@@ -116,6 +116,39 @@
    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 c9e3dc7..3181f8a 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
@@ -51,6 +51,7 @@
     }
 }
 
+
 flutter {
     source = "../.."
 }