Migrate `packages/flutter_tools/templates/plugin` templates to Kotlin DSL (#173993)
- Replaced `build.gradle.tmpl` files with `build.gradle.kts.tmpl` for
both Java and Kotlin Android plugin templates, as well as the shared
plugin template.
- Update `template_manifest.json` to reflect the new Kotlin DSL files.
Fixes #161703 and #142685.
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
---------
Co-authored-by: Gray Mackall <34871572+gmackall@users.noreply.github.com>
diff --git a/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.kts.tmpl
similarity index 62%
rename from packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl
rename to packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.kts.tmpl
index d969602..b6c0fa0 100644
--- a/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl
+++ b/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.kts.tmpl
@@ -12,14 +12,16 @@
}
}
-rootProject.allprojects {
+allprojects {
repositories {
google()
mavenCentral()
}
}
-apply plugin: "com.android.library"
+plugins {
+ id("com.android.library")
+}
android {
namespace = "{{androidIdentifier}}"
@@ -35,18 +37,19 @@
minSdk = {{minSdkVersion}}
}
- dependencies {
- testImplementation("junit:junit:4.13.2")
- testImplementation("org.mockito:mockito-core:5.0.0")
- }
-
testOptions {
unitTests.all {
- testLogging {
- events "passed", "skipped", "failed", "standardOut", "standardError"
- outputs.upToDateWhen {false}
- showStandardStreams = true
+ it.outputs.upToDateWhen { false }
+
+ it.testLogging {
+ events("passed", "skipped", "failed", "standardOut", "standardError")
+ showStandardStreams = true
}
}
}
}
+
+dependencies {
+ testImplementation("junit:junit:4.13.2")
+ testImplementation("org.mockito:mockito-core:5.0.0")
+}
diff --git a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.kts.tmpl b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.kts.tmpl
new file mode 100644
index 0000000..b030c04
--- /dev/null
+++ b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.kts.tmpl
@@ -0,0 +1,76 @@
+group = "{{androidIdentifier}}"
+version = "1.0-SNAPSHOT"
+
+buildscript {
+ val kotlinVersion = "{{kotlinVersion}}"
+ repositories {
+ google()
+ mavenCentral()
+ }
+
+ dependencies {
+ classpath("com.android.tools.build:gradle:{{agpVersion}}")
+ classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
+ }
+}
+
+allprojects {
+ repositories {
+ google()
+ mavenCentral()
+ }
+}
+
+plugins {
+ id("com.android.library")
+ id("kotlin-android")
+}
+
+android {
+ namespace = "{{androidIdentifier}}"
+
+ compileSdk = {{compileSdkVersion}}
+
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_17
+ targetCompatibility = JavaVersion.VERSION_17
+ }
+
+ kotlinOptions {
+ jvmTarget = JavaVersion.VERSION_17.toString()
+ }
+
+ sourceSets {
+ getByName("main") {
+ java.srcDirs("src/main/kotlin")
+ }
+ getByName("test") {
+ java.srcDirs("src/test/kotlin")
+ }
+ }
+
+ defaultConfig {
+ minSdk = {{minSdkVersion}}
+ }
+
+ testOptions {
+ unitTests {
+ isIncludeAndroidResources = true
+ all {
+ it.useJUnitPlatform()
+
+ it.outputs.upToDateWhen { false }
+
+ it.testLogging {
+ events("passed", "skipped", "failed", "standardOut", "standardError")
+ showStandardStreams = true
+ }
+ }
+ }
+ }
+}
+
+dependencies {
+ testImplementation("org.jetbrains.kotlin:kotlin-test")
+ testImplementation("org.mockito:mockito-core:5.0.0")
+}
diff --git a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl
deleted file mode 100644
index 898a04a..0000000
--- a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl
+++ /dev/null
@@ -1,66 +0,0 @@
-group = "{{androidIdentifier}}"
-version = "1.0-SNAPSHOT"
-
-buildscript {
- ext.kotlin_version = "{{kotlinVersion}}"
- repositories {
- google()
- mavenCentral()
- }
-
- dependencies {
- classpath("com.android.tools.build:gradle:{{agpVersion}}")
- classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
- }
-}
-
-allprojects {
- repositories {
- google()
- mavenCentral()
- }
-}
-
-apply plugin: "com.android.library"
-apply plugin: "kotlin-android"
-
-android {
- namespace = "{{androidIdentifier}}"
-
- compileSdk = {{compileSdkVersion}}
-
- compileOptions {
- sourceCompatibility = JavaVersion.VERSION_17
- targetCompatibility = JavaVersion.VERSION_17
- }
-
- kotlinOptions {
- jvmTarget = JavaVersion.VERSION_17
- }
-
- sourceSets {
- main.java.srcDirs += "src/main/kotlin"
- test.java.srcDirs += "src/test/kotlin"
- }
-
- defaultConfig {
- minSdk = {{minSdkVersion}}
- }
-
- dependencies {
- testImplementation("org.jetbrains.kotlin:kotlin-test")
- testImplementation("org.mockito:mockito-core:5.0.0")
- }
-
- testOptions {
- unitTests.all {
- useJUnitPlatform()
-
- testLogging {
- events "passed", "skipped", "failed", "standardOut", "standardError"
- outputs.upToDateWhen {false}
- showStandardStreams = true
- }
- }
- }
-}
diff --git a/packages/flutter_tools/templates/plugin/android.tmpl/settings.gradle.tmpl b/packages/flutter_tools/templates/plugin/android.tmpl/settings.gradle.kts.tmpl
similarity index 100%
rename from packages/flutter_tools/templates/plugin/android.tmpl/settings.gradle.tmpl
rename to packages/flutter_tools/templates/plugin/android.tmpl/settings.gradle.kts.tmpl
diff --git a/packages/flutter_tools/templates/template_manifest.json b/packages/flutter_tools/templates/template_manifest.json
index f470979..6b806fd 100644
--- a/packages/flutter_tools/templates/template_manifest.json
+++ b/packages/flutter_tools/templates/template_manifest.json
@@ -256,18 +256,18 @@
"templates/package_ffi/src.tmpl/projectName.h.tmpl",
"templates/package_ffi/test/projectName_test.dart.tmpl",
- "templates/plugin/android-java.tmpl/build.gradle.tmpl",
+ "templates/plugin/android-java.tmpl/build.gradle.kts.tmpl",
"templates/plugin/android-java.tmpl/projectName_android.iml.tmpl",
"templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl",
"templates/plugin/android-java.tmpl/src/test/java/androidIdentifier/pluginClassTest.java.tmpl",
- "templates/plugin/android-kotlin.tmpl/build.gradle.tmpl",
+ "templates/plugin/android-kotlin.tmpl/build.gradle.kts.tmpl",
"templates/plugin/android-kotlin.tmpl/projectName_android.iml.tmpl",
"templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl",
"templates/plugin/android-kotlin.tmpl/src/test/kotlin/androidIdentifier/pluginClassTest.kt.tmpl",
"templates/plugin/android.tmpl/.gitignore",
"templates/plugin/android.tmpl/gradle/wrapper/gradle-wrapper.properties",
"templates/plugin/android.tmpl/gradle.properties.tmpl",
- "templates/plugin/android.tmpl/settings.gradle.tmpl",
+ "templates/plugin/android.tmpl/settings.gradle.kts.tmpl",
"templates/plugin/android.tmpl/src/main/AndroidManifest.xml.tmpl",
"templates/plugin/ios.tmpl/projectName.podspec.tmpl",
"templates/plugin/ios.tmpl/.gitignore",
diff --git a/packages/flutter_tools/test/android_preview_integration.shard/flutter_build_preview_sdk_test.dart b/packages/flutter_tools/test/android_preview_integration.shard/flutter_build_preview_sdk_test.dart
index aa89521..4b0b99d 100644
--- a/packages/flutter_tools/test/android_preview_integration.shard/flutter_build_preview_sdk_test.dart
+++ b/packages/flutter_tools/test/android_preview_integration.shard/flutter_build_preview_sdk_test.dart
@@ -136,18 +136,18 @@
final File pluginBuildGradleFile = pluginDir
.childDirectory('android')
- .childFile('build.gradle');
+ .childFile('build.gradle.kts');
// change the plugin build.gradle to use a preview compile sdk version
pluginBuildGradleFile.writeAsStringSync(
pluginBuildGradleFile.readAsStringSync().replaceFirst(
compileSdkVersionMatch,
- 'compileSdkPreview "UpsideDownCake"',
+ 'compileSdkPreview = "UpsideDownCake"',
),
flush: true,
);
expect(
pluginBuildGradleFile.readAsStringSync(),
- contains('compileSdkPreview "UpsideDownCake"'),
+ contains('compileSdkPreview = "UpsideDownCake"'),
);
final ProcessResult result = await processManager.run(<String>[
diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart
index d64808c..3fcea4a 100644
--- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart
+++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart
@@ -3389,7 +3389,7 @@
projectDir.path,
]);
- final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
+ final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle.kts');
expect(buildGradleFile.existsSync(), true);
@@ -3466,7 +3466,7 @@
projectDir.path,
]);
- final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
+ final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle.kts');
expect(buildGradleFile.existsSync(), true);
@@ -3492,7 +3492,7 @@
projectDir.path,
]);
- final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
+ final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle.kts');
expect(buildGradleFile.existsSync(), true);
@@ -3519,7 +3519,7 @@
projectDir.path,
]);
- final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
+ final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle.kts');
expect(buildGradleFile.existsSync(), true);
diff --git a/packages/flutter_tools/test/integration.shard/android_plugin_compilesdkversion_mismatch_test.dart b/packages/flutter_tools/test/integration.shard/android_plugin_compilesdkversion_mismatch_test.dart
index 128dc93..d366308 100644
--- a/packages/flutter_tools/test/integration.shard/android_plugin_compilesdkversion_mismatch_test.dart
+++ b/packages/flutter_tools/test/integration.shard/android_plugin_compilesdkversion_mismatch_test.dart
@@ -34,7 +34,9 @@
], workingDirectory: tempDir.path);
final Directory pluginAppDir = tempDir.childDirectory('test_plugin');
- final File pluginGradleFile = pluginAppDir.childDirectory('android').childFile('build.gradle');
+ final File pluginGradleFile = pluginAppDir
+ .childDirectory('android')
+ .childFile('build.gradle.kts');
expect(pluginGradleFile, exists);
final String pluginBuildGradle = pluginGradleFile.readAsStringSync();