Refactor "app plugin loader" Gradle Plugin so it can be applied using the declarative `plugins {}` block (#127897)

This PR fixes #125009.
diff --git a/examples/hello_world/android/build.gradle b/examples/hello_world/android/build.gradle
index 2de2062..a38857e 100644
--- a/examples/hello_world/android/build.gradle
+++ b/examples/hello_world/android/build.gradle
@@ -7,14 +7,13 @@
 // See dev/tools/bin/generate_gradle_lockfiles.dart.
 
 buildscript {
-    ext.kotlin_version = '1.5.31'
+    ext.kotlin_version = '1.9.0'
     repositories {
         google()
         mavenCentral()
     }
 
     dependencies {
-        classpath 'com.android.tools.build:gradle:7.2.0'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
     }
 
diff --git a/examples/hello_world/android/buildscript-gradle.lockfile b/examples/hello_world/android/buildscript-gradle.lockfile
index efe1327..14350ad 100644
--- a/examples/hello_world/android/buildscript-gradle.lockfile
+++ b/examples/hello_world/android/buildscript-gradle.lockfile
@@ -1,163 +1,29 @@
 # This is a Gradle generated file for dependency locking.
 # Manual edits can break the build and are not advised.
 # This file is expected to be part of source control.
-androidx.databinding:databinding-common:7.2.0=classpath
-androidx.databinding:databinding-compiler-common:7.2.0=classpath
-com.android.databinding:baseLibrary:7.2.0=classpath
-com.android.tools.analytics-library:crash:30.2.0=classpath
-com.android.tools.analytics-library:protos:30.2.0=classpath
-com.android.tools.analytics-library:shared:30.2.0=classpath
-com.android.tools.analytics-library:tracker:30.2.0=classpath
-com.android.tools.build.jetifier:jetifier-core:1.0.0-beta09=classpath
-com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta09=classpath
-com.android.tools.build:aapt2-proto:7.2.0-7984345=classpath
-com.android.tools.build:aaptcompiler:7.2.0=classpath
-com.android.tools.build:apksig:7.2.0=classpath
-com.android.tools.build:apkzlib:7.2.0=classpath
-com.android.tools.build:builder-model:7.2.0=classpath
-com.android.tools.build:builder-test-api:7.2.0=classpath
-com.android.tools.build:builder:7.2.0=classpath
-com.android.tools.build:bundletool:1.8.2=classpath
-com.android.tools.build:gradle-api:7.2.0=classpath
-com.android.tools.build:gradle:7.2.0=classpath
-com.android.tools.build:manifest-merger:30.2.0=classpath
-com.android.tools.build:transform-api:2.0.0-deprecated-use-gradle-api=classpath
-com.android.tools.ddms:ddmlib:30.2.0=classpath
-com.android.tools.layoutlib:layoutlib-api:30.2.0=classpath
-com.android.tools.lint:lint-model:30.2.0=classpath
-com.android.tools.lint:lint-typedef-remover:30.2.0=classpath
-com.android.tools.utp:android-device-provider-ddmlib-proto:30.2.0=classpath
-com.android.tools.utp:android-device-provider-gradle-proto:30.2.0=classpath
-com.android.tools.utp:android-test-plugin-host-additional-test-output-proto:30.2.0=classpath
-com.android.tools.utp:android-test-plugin-host-coverage-proto:30.2.0=classpath
-com.android.tools.utp:android-test-plugin-host-retention-proto:30.2.0=classpath
-com.android.tools.utp:android-test-plugin-result-listener-gradle-proto:30.2.0=classpath
-com.android.tools:annotations:30.2.0=classpath
-com.android.tools:common:30.2.0=classpath
-com.android.tools:dvlib:30.2.0=classpath
-com.android.tools:repository:30.2.0=classpath
-com.android.tools:sdk-common:30.2.0=classpath
-com.android.tools:sdklib:30.2.0=classpath
-com.android:signflinger:7.2.0=classpath
-com.android:zipflinger:7.2.0=classpath
-com.fasterxml.jackson.core:jackson-annotations:2.11.1=classpath
-com.fasterxml.jackson.core:jackson-core:2.11.1=classpath
-com.fasterxml.jackson.core:jackson-databind:2.11.1=classpath
-com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.1=classpath
-com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.11.1=classpath
-com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1=classpath
-com.fasterxml.woodstox:woodstox-core:6.2.1=classpath
-com.github.gundy:semver4j:0.16.4=classpath
-com.google.android:annotations:4.1.1.4=classpath
-com.google.api.grpc:proto-google-common-protos:1.12.0=classpath
-com.google.auto.value:auto-value-annotations:1.6.2=classpath
-com.google.code.findbugs:jsr305:3.0.2=classpath
-com.google.code.gson:gson:2.8.6=classpath
-com.google.crypto.tink:tink:1.3.0-rc2=classpath
-com.google.dagger:dagger:2.28.3=classpath
-com.google.errorprone:error_prone_annotations:2.3.4=classpath
-com.google.flatbuffers:flatbuffers-java:1.12.0=classpath
-com.google.guava:failureaccess:1.0.1=classpath
-com.google.guava:guava:30.1-jre=classpath
-com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=classpath
-com.google.j2objc:j2objc-annotations:1.3=classpath
-com.google.jimfs:jimfs:1.1=classpath
-com.google.protobuf:protobuf-java-util:3.10.0=classpath
-com.google.protobuf:protobuf-java:3.10.0=classpath
-com.google.testing.platform:core-proto:0.0.8-alpha07=classpath
-com.googlecode.json-simple:json-simple:1.1=classpath
-com.googlecode.juniversalchardet:juniversalchardet:1.0.3=classpath
-com.squareup:javapoet:1.10.0=classpath
-com.squareup:javawriter:2.5.0=classpath
-com.sun.activation:javax.activation:1.2.0=classpath
-com.sun.istack:istack-commons-runtime:3.0.8=classpath
-com.sun.xml.fastinfoset:FastInfoset:1.2.16=classpath
-commons-codec:commons-codec:1.11=classpath
-commons-io:commons-io:2.4=classpath
-commons-logging:commons-logging:1.2=classpath
-de.undercouch:gradle-download-task:4.1.1=classpath
-io.grpc:grpc-api:1.21.1=classpath
-io.grpc:grpc-context:1.21.1=classpath
-io.grpc:grpc-core:1.21.1=classpath
-io.grpc:grpc-netty:1.21.1=classpath
-io.grpc:grpc-protobuf-lite:1.21.1=classpath
-io.grpc:grpc-protobuf:1.21.1=classpath
-io.grpc:grpc-stub:1.21.1=classpath
-io.netty:netty-buffer:4.1.34.Final=classpath
-io.netty:netty-codec-http2:4.1.34.Final=classpath
-io.netty:netty-codec-http:4.1.34.Final=classpath
-io.netty:netty-codec-socks:4.1.34.Final=classpath
-io.netty:netty-codec:4.1.34.Final=classpath
-io.netty:netty-common:4.1.34.Final=classpath
-io.netty:netty-handler-proxy:4.1.34.Final=classpath
-io.netty:netty-handler:4.1.34.Final=classpath
-io.netty:netty-resolver:4.1.34.Final=classpath
-io.netty:netty-transport:4.1.34.Final=classpath
-io.opencensus:opencensus-api:0.21.0=classpath
-io.opencensus:opencensus-contrib-grpc-metrics:0.21.0=classpath
-it.unimi.dsi:fastutil:8.4.0=classpath
-jakarta.activation:jakarta.activation-api:1.2.1=classpath
-jakarta.xml.bind:jakarta.xml.bind-api:2.3.2=classpath
-javax.inject:javax.inject:1=classpath
-net.java.dev.jna:jna-platform:5.6.0=classpath
-net.java.dev.jna:jna:5.6.0=classpath
-net.sf.jopt-simple:jopt-simple:4.9=classpath
-net.sf.kxml:kxml2:2.3.0=classpath
-org.apache.commons:commons-compress:1.20=classpath
-org.apache.httpcomponents:httpclient:4.5.9=classpath
-org.apache.httpcomponents:httpcore:4.4.11=classpath
-org.apache.httpcomponents:httpmime:4.5.6=classpath
-org.bitbucket.b_c:jose4j:0.7.0=classpath
-org.bouncycastle:bcpkix-jdk15on:1.56=classpath
-org.bouncycastle:bcprov-jdk15on:1.56=classpath
-org.checkerframework:checker-qual:3.5.0=classpath
-org.codehaus.mojo:animal-sniffer-annotations:1.17=classpath
-org.codehaus.woodstox:stax2-api:4.2.1=classpath
-org.glassfish.jaxb:jaxb-runtime:2.3.2=classpath
-org.glassfish.jaxb:txw2:2.3.2=classpath
-org.jdom:jdom2:2.0.6=classpath
-org.jetbrains.dokka:dokka-core:1.4.32=classpath
-org.jetbrains.intellij.deps:trove4j:1.0.20181211=classpath
-org.jetbrains.kotlin:kotlin-android-extensions:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-annotation-processing-gradle:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-build-common:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-compiler-embeddable:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-compiler-runner:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-daemon-client:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-daemon-embeddable:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-gradle-plugin-model:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-klib-commonizer-api:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-native-utils:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-project-model:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-reflect:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-scripting-common:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-scripting-jvm:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-stdlib-common:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-stdlib:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-tooling-metadata:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-util-io:1.5.31=classpath
-org.jetbrains.kotlin:kotlin-util-klib:1.5.31=classpath
+org.jetbrains.intellij.deps:trove4j:1.0.20200330=classpath
+org.jetbrains.kotlin:kotlin-android-extensions:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-build-tools-api:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-compiler-embeddable:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-compiler-runner:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-daemon-client:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-daemon-embeddable:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin-annotations:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin-idea-proto:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin-idea:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin-model:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-gradle-plugins-bom:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-klib-commonizer-api:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-native-utils:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-project-model:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-scripting-common:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-scripting-jvm:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-tooling-core:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-util-io:1.9.0=classpath
+org.jetbrains.kotlin:kotlin-util-klib:1.9.0=classpath
 org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.0=classpath
-org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0=classpath
-org.jetbrains:annotations:13.0=classpath
-org.jetbrains:markdown-jvm:0.2.1=classpath
-org.jetbrains:markdown:0.2.1=classpath
-org.json:json:20180813=classpath
-org.jsoup:jsoup:1.13.1=classpath
-org.jvnet.staxex:stax-ex:1.8.1=classpath
-org.ow2.asm:asm-analysis:9.1=classpath
-org.ow2.asm:asm-commons:9.1=classpath
-org.ow2.asm:asm-tree:9.1=classpath
-org.ow2.asm:asm-util:9.1=classpath
-org.ow2.asm:asm:9.1=classpath
-org.slf4j:slf4j-api:1.7.30=classpath
-org.tensorflow:tensorflow-lite-metadata:0.1.0-rc2=classpath
-xerces:xercesImpl:2.12.0=classpath
-xml-apis:xml-apis:1.4.01=classpath
 empty=
diff --git a/examples/hello_world/android/gradle/wrapper/gradle-wrapper.properties b/examples/hello_world/android/gradle/wrapper/gradle-wrapper.properties
index 3c472b9..ec915a8 100644
--- a/examples/hello_world/android/gradle/wrapper/gradle-wrapper.properties
+++ b/examples/hello_world/android/gradle/wrapper/gradle-wrapper.properties
@@ -2,4 +2,4 @@
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip
diff --git a/examples/hello_world/android/project-app.lockfile b/examples/hello_world/android/project-app.lockfile
index e160a81..0d87887 100644
--- a/examples/hello_world/android/project-app.lockfile
+++ b/examples/hello_world/android/project-app.lockfile
@@ -87,10 +87,10 @@
 org.codehaus.mojo:animal-sniffer-annotations:1.18=lintClassPath
 org.glassfish.jaxb:jaxb-runtime:2.3.1=lintClassPath
 org.glassfish.jaxb:txw2:2.3.1=lintClassPath
-org.jacoco:org.jacoco.agent:0.8.7=androidJacocoAnt
-org.jacoco:org.jacoco.ant:0.8.7=androidJacocoAnt
-org.jacoco:org.jacoco.core:0.8.7=androidJacocoAnt
-org.jacoco:org.jacoco.report:0.8.7=androidJacocoAnt
+org.jacoco:org.jacoco.agent:0.8.8=androidJacocoAnt
+org.jacoco:org.jacoco.ant:0.8.8=androidJacocoAnt
+org.jacoco:org.jacoco.core:0.8.8=androidJacocoAnt
+org.jacoco:org.jacoco.report:0.8.8=androidJacocoAnt
 org.jetbrains.kotlin:kotlin-reflect:1.3.72=lintClassPath
 org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72=lintClassPath
 org.jetbrains.kotlin:kotlin-stdlib-common:1.5.31=debugAndroidTestCompileClasspath,debugCompileClasspath,debugRuntimeClasspath,debugUnitTestCompileClasspath,debugUnitTestRuntimeClasspath,profileCompileClasspath,profileRuntimeClasspath,profileUnitTestCompileClasspath,profileUnitTestRuntimeClasspath,releaseCompileClasspath,releaseRuntimeClasspath,releaseUnitTestCompileClasspath,releaseUnitTestRuntimeClasspath
@@ -107,12 +107,12 @@
 org.jetbrains:annotations:13.0=debugAndroidTestCompileClasspath,debugCompileClasspath,debugRuntimeClasspath,debugUnitTestCompileClasspath,debugUnitTestRuntimeClasspath,lintClassPath,profileCompileClasspath,profileRuntimeClasspath,profileUnitTestCompileClasspath,profileUnitTestRuntimeClasspath,releaseCompileClasspath,releaseRuntimeClasspath,releaseUnitTestCompileClasspath,releaseUnitTestRuntimeClasspath
 org.jvnet.staxex:stax-ex:1.8=lintClassPath
 org.ow2.asm:asm-analysis:7.0=lintClassPath
-org.ow2.asm:asm-analysis:9.1=androidJacocoAnt
+org.ow2.asm:asm-analysis:9.2=androidJacocoAnt
 org.ow2.asm:asm-commons:7.0=lintClassPath
-org.ow2.asm:asm-commons:9.1=androidJacocoAnt
+org.ow2.asm:asm-commons:9.2=androidJacocoAnt
 org.ow2.asm:asm-tree:7.0=lintClassPath
-org.ow2.asm:asm-tree:9.1=androidJacocoAnt
+org.ow2.asm:asm-tree:9.2=androidJacocoAnt
 org.ow2.asm:asm-util:7.0=lintClassPath
 org.ow2.asm:asm:7.0=lintClassPath
-org.ow2.asm:asm:9.1=androidJacocoAnt
+org.ow2.asm:asm:9.2=androidJacocoAnt
 empty=androidApis,androidJdkImage,androidTestUtil,compile,coreLibraryDesugaring,debugAndroidTestAnnotationProcessorClasspath,debugAndroidTestRuntimeClasspath,debugAnnotationProcessorClasspath,debugReverseMetadataValues,debugUnitTestAnnotationProcessorClasspath,debugWearBundling,lintChecks,lintPublish,profileAnnotationProcessorClasspath,profileReverseMetadataValues,profileUnitTestAnnotationProcessorClasspath,profileWearBundling,releaseAnnotationProcessorClasspath,releaseReverseMetadataValues,releaseUnitTestAnnotationProcessorClasspath,releaseWearBundling,testCompile
diff --git a/examples/hello_world/android/settings.gradle b/examples/hello_world/android/settings.gradle
index 4b92919..47426cc 100644
--- a/examples/hello_world/android/settings.gradle
+++ b/examples/hello_world/android/settings.gradle
@@ -19,6 +19,12 @@
     // Flutter Gradle Plugin ships together with the Flutter SDK
     includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")
 
+    repositories {
+        google()
+        mavenCentral()
+        gradlePluginPortal()
+    }
+
     plugins {
         // Flutter Gradle Plugin's ID is defined in /packages/flutter_tools/gradle/build.gradle.kts.
         // We set `apply false`, because we don't want to apply the plugin to
@@ -27,6 +33,9 @@
     }
 }
 
-include ':app'
+plugins {
+    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
+    id "com.android.application" version "7.4.2" apply false
+}
 
-apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"
+include ':app'
diff --git a/packages/flutter_tools/gradle/app_plugin_loader.gradle b/packages/flutter_tools/gradle/app_plugin_loader.gradle
index e5cfe9c..fd450e6 100644
--- a/packages/flutter_tools/gradle/app_plugin_loader.gradle
+++ b/packages/flutter_tools/gradle/app_plugin_loader.gradle
@@ -2,35 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-// This file is included from `<app>/android/settings.gradle`,
-// so it can be versioned with the Flutter SDK.
+// This file exists solely for the compatibility with projects that have
+// not migrated to the declarative apply of the Flutter App Plugin Loader Gradle Plugin.
 
-import groovy.json.JsonSlurper
-
-def flutterProjectRoot = rootProject.projectDir.parentFile
-
-// If this logic is changed, also change the logic in module_plugin_loader.gradle.
-def pluginsFile = new File(flutterProjectRoot, '.flutter-plugins-dependencies')
-if (!pluginsFile.exists()) {
-  return
-}
-
-def object = new JsonSlurper().parseText(pluginsFile.text)
-assert object instanceof Map
-assert object.plugins instanceof Map
-assert object.plugins.android instanceof List
-// Includes the Flutter plugins that support the Android platform.
-object.plugins.android.each { androidPlugin ->
-  assert androidPlugin.name instanceof String
-  assert androidPlugin.path instanceof String
-  // Skip plugins that have no native build (such as a Dart-only implementation
-  // of a federated plugin).
-  def needsBuild = androidPlugin.containsKey('native_build') ? androidPlugin['native_build'] : true
-  if (!needsBuild) {
-    return
-  }
-  def pluginDirectory = new File(androidPlugin.path, 'android')
-  assert pluginDirectory.exists()
-  include ":${androidPlugin.name}"
-  project(":${androidPlugin.name}").projectDir = pluginDirectory
-}
+def pathToThisDirectory = buildscript.sourceFile.parentFile
+apply from: "$pathToThisDirectory/src/main/groovy/app_plugin_loader.groovy"
diff --git a/packages/flutter_tools/gradle/build.gradle.kts b/packages/flutter_tools/gradle/build.gradle.kts
index 289693f..9c6cfdd 100644
--- a/packages/flutter_tools/gradle/build.gradle.kts
+++ b/packages/flutter_tools/gradle/build.gradle.kts
@@ -3,12 +3,8 @@
 // found in the LICENSE file.
 
 plugins {
-    `groovy-gradle-plugin`
-}
-
-repositories {
-    google()
-    mavenCentral()
+    `java-gradle-plugin`
+    `groovy`
 }
 
 
@@ -22,6 +18,10 @@
             id = "dev.flutter.flutter-gradle-plugin"
             implementationClass = "FlutterPlugin"
         }
+        create("flutterAppPluginLoaderPlugin") {
+            id = "dev.flutter.flutter-plugin-loader"
+            implementationClass = "FlutterAppPluginLoaderPlugin"
+        }
     }
 }
 
diff --git a/packages/flutter_tools/gradle/settings.gradle.kts b/packages/flutter_tools/gradle/settings.gradle.kts
new file mode 100644
index 0000000..f8d3e87
--- /dev/null
+++ b/packages/flutter_tools/gradle/settings.gradle.kts
@@ -0,0 +1,7 @@
+dependencyResolutionManagement {
+  repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+  repositories {
+    google()
+    mavenCentral()
+  }
+}
diff --git a/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy b/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy
new file mode 100644
index 0000000..402ab64
--- /dev/null
+++ b/packages/flutter_tools/gradle/src/main/groovy/app_plugin_loader.groovy
@@ -0,0 +1,42 @@
+import groovy.json.JsonSlurper
+import org.gradle.api.Plugin
+import org.gradle.api.initialization.Settings
+
+apply plugin: FlutterAppPluginLoaderPlugin
+
+class FlutterAppPluginLoaderPlugin implements Plugin<Settings> {
+    // This string must match _kFlutterPluginsHasNativeBuildKey defined in
+    // packages/flutter_tools/lib/src/flutter_plugins.dart.
+    private final String nativeBuildKey = 'native_build'
+
+    @Override
+    void apply(Settings settings) {
+        def flutterProjectRoot = settings.settingsDir.parentFile
+
+        // If this logic is changed, also change the logic in module_plugin_loader.gradle.
+        def pluginsFile = new File(flutterProjectRoot, '.flutter-plugins-dependencies')
+        if (!pluginsFile.exists()) {
+            return
+        }
+
+        def object = new JsonSlurper().parseText(pluginsFile.text)
+        assert object instanceof Map
+        assert object.plugins instanceof Map
+        assert object.plugins.android instanceof List
+        // Includes the Flutter plugins that support the Android platform.
+        object.plugins.android.each { androidPlugin ->
+            assert androidPlugin.name instanceof String
+            assert androidPlugin.path instanceof String
+            // Skip plugins that have no native build (such as a Dart-only implementation
+            // of a federated plugin).
+            def needsBuild = androidPlugin.containsKey(nativeBuildKey) ? androidPlugin[nativeBuildKey] : true
+            if (!needsBuild) {
+                return
+            }
+            def pluginDirectory = new File(androidPlugin.path, 'android')
+            assert pluginDirectory.exists()
+            settings.include(":${androidPlugin.name}")
+            settings.project(":${androidPlugin.name}").projectDir = pluginDirectory
+        }
+    }
+}
diff --git a/packages/flutter_tools/templates/app_shared/android-java.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/app_shared/android-java.tmpl/build.gradle.tmpl
index 1411541..40c56e3 100644
--- a/packages/flutter_tools/templates/app_shared/android-java.tmpl/build.gradle.tmpl
+++ b/packages/flutter_tools/templates/app_shared/android-java.tmpl/build.gradle.tmpl
@@ -6,7 +6,6 @@
     }
 
     dependencies {
-        classpath 'com.android.tools.build:gradle:{{agpVersion}}'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
     }
 }
diff --git a/packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl
index 1411541..40c56e3 100644
--- a/packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl
+++ b/packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl
@@ -6,7 +6,6 @@
     }
 
     dependencies {
-        classpath 'com.android.tools.build:gradle:{{agpVersion}}'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
     }
 }
diff --git a/packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle b/packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle.tmpl
similarity index 71%
rename from packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle
rename to packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle.tmpl
index 55c4ca8..56f8773 100644
--- a/packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle
+++ b/packages/flutter_tools/templates/app_shared/android.tmpl/settings.gradle.tmpl
@@ -10,11 +10,20 @@
 
     includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")
 
+    repositories {
+        google()
+        mavenCentral()
+        gradlePluginPortal()
+    }
+
     plugins {
         id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
     }
 }
 
-include ":app"
+plugins {
+    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
+    id "com.android.application" version "{{agpVersion}}" apply false
+}
 
-apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"
+include ":app"
diff --git a/packages/flutter_tools/templates/template_manifest.json b/packages/flutter_tools/templates/template_manifest.json
index f7cc14a..b43e75d 100644
--- a/packages/flutter_tools/templates/template_manifest.json
+++ b/packages/flutter_tools/templates/template_manifest.json
@@ -36,6 +36,7 @@
         "templates/app_shared/android.tmpl/app/src/main/res/values/styles.xml",
         "templates/app_shared/android.tmpl/app/src/profile/AndroidManifest.xml.tmpl",
         "templates/app_shared/android.tmpl/gradle.properties.tmpl",
+        "templates/app_shared/android.tmpl/settings.gradle.tmpl",
         "templates/app_shared/android.tmpl/gradle/wrapper/gradle-wrapper.properties.tmpl",
         "templates/app_shared/android.tmpl/settings.gradle",
         "templates/app_shared/ios-objc.tmpl/Runner.xcodeproj/project.pbxproj.tmpl",