Skip to content

Commit d3c54a1

Browse files
authored
Use Gradle KTS in new Android app projects by default (flutter#154061)
This PR resolves flutter#151166
1 parent 0549bd8 commit d3c54a1

File tree

18 files changed

+133
-112
lines changed

18 files changed

+133
-112
lines changed

dev/devicelab/bin/tasks/engine_dependency_proxy_test.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ Future<void> main() async {
2929
await runProjectTest((FlutterProject flutterProject) async {
3030
await inDirectory(path.join(flutterProject.rootPath, 'android'), () async {
3131
section('Insert gradle testing script');
32-
final File build = File(path.join(
33-
flutterProject.rootPath, 'android', 'app', 'build.gradle',
34-
));
35-
build.writeAsStringSync(
32+
final File buildFile = getAndroidBuildFile(path.join(flutterProject.rootPath, 'android'));
33+
buildFile.writeAsStringSync(
3634
'''
37-
task printEngineMavenUrl() {
35+
tasks.register("printEngineMavenUrl") {
3836
doLast {
39-
println project.repositories.find { it.name == 'maven' }.url
37+
project.repositories.forEach { repo ->
38+
if (repo.name == "maven") {
39+
repo as MavenArtifactRepository
40+
logger.quiet(repo.url.toString())
41+
}
42+
}
4043
}
4144
}
4245
''',

dev/devicelab/lib/framework/apk_utils.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,17 @@ class FlutterProject {
256256
String get rootPath => path.join(parent.path, name);
257257
String get androidPath => path.join(rootPath, 'android');
258258
String get iosPath => path.join(rootPath, 'ios');
259+
File get appBuildFile => getAndroidBuildFile(androidPath);
259260

260261
Future<void> addCustomBuildType(String name, {required String initWith}) async {
261-
final File buildScript = File(
262-
path.join(androidPath, 'app', 'build.gradle'),
263-
);
262+
final File buildScript = appBuildFile;
264263

265264
buildScript.openWrite(mode: FileMode.append).write('''
266265
267266
android {
268267
buildTypes {
269-
$name {
270-
initWith $initWith
268+
create("$name") {
269+
initWith(getByName("$initWith"))
271270
}
272271
}
273272
}
@@ -288,14 +287,12 @@ android {
288287
}
289288

290289
Future<void> setMinSdkVersion(int sdkVersion) async {
291-
final File buildScript = File(
292-
path.join(androidPath, 'app', 'build.gradle'),
293-
);
290+
final File buildScript = appBuildFile;
294291

295292
buildScript.openWrite(mode: FileMode.append).write('''
296293
android {
297294
defaultConfig {
298-
minSdkVersion $sdkVersion
295+
minSdk = $sdkVersion
299296
}
300297
}
301298
''');
@@ -308,22 +305,20 @@ android {
308305
}
309306

310307
Future<void> addProductFlavors(Iterable<String> flavors) async {
311-
final File buildScript = File(
312-
path.join(androidPath, 'app', 'build.gradle'),
313-
);
308+
final File buildScript = appBuildFile;
314309

315310
final String flavorConfig = flavors.map((String name) {
316311
return '''
317-
$name {
318-
applicationIdSuffix ".$name"
319-
versionNameSuffix "-$name"
312+
create("$name") {
313+
applicationIdSuffix = ".$name"
314+
versionNameSuffix = "-$name"
320315
}
321316
''';
322317
}).join('\n');
323318

324319
buildScript.openWrite(mode: FileMode.append).write('''
325320
android {
326-
flavorDimensions "mode"
321+
flavorDimensions.add("mode")
327322
productFlavors {
328323
$flavorConfig
329324
}
@@ -332,9 +327,7 @@ android {
332327
}
333328

334329
Future<void> introduceError() async {
335-
final File buildScript = File(
336-
path.join(androidPath, 'app', 'build.gradle'),
337-
);
330+
final File buildScript = appBuildFile;
338331
await buildScript.writeAsString((await buildScript.readAsString()).replaceAll('buildTypes', 'builTypes'));
339332
}
340333

@@ -477,3 +470,14 @@ String? validateSnapshotDependency(FlutterProject project, String expectedTarget
477470
return contentSnapshot.contains('$expectedTarget ')
478471
? null : 'Dependency file should have $expectedTarget as target. Instead found $contentSnapshot';
479472
}
473+
474+
File getAndroidBuildFile(String androidPath) {
475+
final File groovyFile = File(path.join(androidPath, 'app', 'build.gradle'));
476+
final File kotlinFile = File(path.join(androidPath, 'app', 'build.gradle.kts'));
477+
478+
if (groovyFile.existsSync()) {
479+
return groovyFile;
480+
}
481+
482+
return kotlinFile;
483+
}

packages/flutter_tools/lib/src/project.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,19 @@ class AndroidProject extends FlutterProjectPlatform {
467467
static final RegExp _androidNamespacePattern = RegExp('android {[\\S\\s]+namespace\\s*=?\\s*[\'"](.+)[\'"]');
468468
static final RegExp _applicationIdPattern = RegExp('^\\s*applicationId\\s*=?\\s*[\'"](.*)[\'"]\\s*\$');
469469
static final RegExp _imperativeKotlinPluginPattern = RegExp('^\\s*apply plugin\\:\\s+[\'"]kotlin-android[\'"]\\s*\$');
470-
static final RegExp _declarativeKotlinPluginPattern = RegExp('^\\s*id\\s+[\'"]kotlin-android[\'"]\\s*\$');
470+
471+
/// Examples of strings that this regex matches:
472+
/// - `id "kotlin-android"`
473+
/// - `id("kotlin-android")`
474+
/// - `id ( "kotlin-android" ) `
475+
/// - `id "org.jetbrains.kotlin.android"`
476+
/// - `id("org.jetbrains.kotlin.android")`
477+
/// - `id ( "org.jetbrains.kotlin.android" )`
478+
static final List<RegExp> _declarativeKotlinPluginPatterns = <RegExp>[
479+
RegExp('^\\s*id\\s*\\(?\\s*[\'"]kotlin-android[\'"]\\s*\\)?\\s*\$'),
480+
RegExp('^\\s*id\\s*\\(?\\s*[\'"]org.jetbrains.kotlin.android[\'"]\\s*\\)?\\s*\$'),
481+
];
482+
471483

472484
/// Pattern used to find the assignment of the "group" property in Gradle.
473485
/// Expected example: `group "dev.flutter.plugin"`
@@ -563,7 +575,9 @@ class AndroidProject extends FlutterProjectPlatform {
563575
/// True, if the app project is using Kotlin.
564576
bool get isKotlin {
565577
final bool imperativeMatch = firstMatchInFile(appGradleFile, _imperativeKotlinPluginPattern) != null;
566-
final bool declarativeMatch = firstMatchInFile(appGradleFile, _declarativeKotlinPluginPattern) != null;
578+
final bool declarativeMatch = _declarativeKotlinPluginPatterns.any((RegExp pattern) {
579+
return (firstMatchInFile(appGradleFile, pattern) != null);
580+
});
567581
return imperativeMatch || declarativeMatch;
568582
}
569583

packages/flutter_tools/templates/app_shared/android-java.tmpl/app/build.gradle.tmpl renamed to packages/flutter_tools/templates/app_shared/android-java.tmpl/app/build.gradle.kts.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
plugins {
2-
id "com.android.application"
3-
id "kotlin-android"
2+
id("com.android.application")
3+
id("kotlin-android")
44
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
5-
id "dev.flutter.flutter-gradle-plugin"
5+
id("dev.flutter.flutter-gradle-plugin")
66
}
77

88
android {
@@ -16,7 +16,7 @@ android {
1616
}
1717

1818
kotlinOptions {
19-
jvmTarget = JavaVersion.VERSION_1_8
19+
jvmTarget = JavaVersion.VERSION_1_8.toString()
2020
}
2121

2222
defaultConfig {
@@ -34,7 +34,7 @@ android {
3434
release {
3535
// TODO: Add your own signing config for the release build.
3636
// Signing with the debug keys for now, so `flutter run --release` works.
37-
signingConfig = signingConfigs.debug
37+
signingConfig = signingConfigs.getByName("debug")
3838
}
3939
}
4040
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
allprojects {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
}
7+
8+
rootProject.buildDir = file("../build")
9+
subprojects {
10+
project.buildDir = file("${rootProject.buildDir}/${project.name}")
11+
}
12+
subprojects {
13+
project.evaluationDependsOn(":app")
14+
}
15+
16+
tasks.register<Delete>("clean") {
17+
delete(rootProject.buildDir)
18+
}

packages/flutter_tools/templates/app_shared/android-java.tmpl/build.gradle.tmpl

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/app/build.gradle.tmpl renamed to packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/app/build.gradle.kts.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
plugins {
2-
id "com.android.application"
3-
id "kotlin-android"
2+
id("com.android.application")
3+
id("kotlin-android")
44
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
5-
id "dev.flutter.flutter-gradle-plugin"
5+
id("dev.flutter.flutter-gradle-plugin")
66
}
77

88
android {
@@ -16,7 +16,7 @@ android {
1616
}
1717

1818
kotlinOptions {
19-
jvmTarget = JavaVersion.VERSION_1_8
19+
jvmTarget = JavaVersion.VERSION_1_8.toString()
2020
}
2121

2222
defaultConfig {
@@ -34,7 +34,7 @@ android {
3434
release {
3535
// TODO: Add your own signing config for the release build.
3636
// Signing with the debug keys for now, so `flutter run --release` works.
37-
signingConfig = signingConfigs.debug
37+
signingConfig = signingConfigs.getByName("debug")
3838
}
3939
}
4040
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
allprojects {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
}
7+
8+
rootProject.buildDir = file("../build")
9+
subprojects {
10+
project.buildDir = file("${rootProject.buildDir}/${project.name}")
11+
}
12+
subprojects {
13+
project.evaluationDependsOn(":app")
14+
}
15+
16+
tasks.register<Delete>("clean") {
17+
delete(rootProject.buildDir)
18+
}

packages/flutter_tools/templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pluginManagement {
2+
val flutterSdkPath = run {
3+
val properties = java.util.Properties()
4+
file("local.properties").inputStream().use { properties.load(it) }
5+
val flutterSdkPath = properties.getProperty("flutter.sdk")
6+
require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" }
7+
flutterSdkPath
8+
}
9+
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
11+
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
17+
}
18+
19+
plugins {
20+
id("dev.flutter.flutter-plugin-loader") version "1.0.0"
21+
id("com.android.application") version "{{agpVersion}}" apply false
22+
id("org.jetbrains.kotlin.android") version "{{kotlinVersion}}" apply false
23+
}
24+
25+
include(":app")

0 commit comments

Comments
 (0)