Skip to content

Commit c4b0322

Browse files
authored
Android Gradle file templates: make it easier to convert them to Kotlin DSL in the future (#142146)
This PR will make it easier for future Flutter-Android apps/plugins/modules etc. to migrate to Gradle Kotlin DSL. This PR is similar to #140452 but concerns public Gradle templates instead of Flutter's internal Gradle code. It should be a no-op change. **before** ![before](https://github.com/flutter/flutter/assets/40357511/5d0cb2bb-a693-43bc-aa10-b8f431e0c68c) **after** ![after](https://github.com/flutter/flutter/assets/40357511/e4a945a5-866f-42f7-813b-b08b26bb89dc)
1 parent 77c6a86 commit c4b0322

File tree

16 files changed

+162
-172
lines changed

16 files changed

+162
-172
lines changed

packages/flutter_tools/lib/src/project.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class AndroidProject extends FlutterProjectPlatform {
463463
/// Pattern used to find the assignment of the "group" property in Gradle.
464464
/// Expected example: `group "dev.flutter.plugin"`
465465
/// Regex is used in both Groovy and Kotlin Gradle files.
466-
static final RegExp _groupPattern = RegExp('^\\s*group\\s+[\'"](.*)[\'"]\\s*\$');
466+
static final RegExp _groupPattern = RegExp('^\\s*group\\s*=?\\s*[\'"](.*)[\'"]\\s*\$');
467467

468468
/// The Gradle root directory of the Android host app. This is the directory
469469
/// containing the `app/` subdirectory and the `settings.gradle` file that
@@ -538,7 +538,7 @@ class AndroidProject extends FlutterProjectPlatform {
538538

539539
// This case allows for flutter run/build to work for modules. It does
540540
// not guarantee the Flutter Gradle Plugin is applied.
541-
final bool managed = line.contains("def flutterPluginVersion = 'managed'");
541+
final bool managed = line.contains(RegExp('def flutterPluginVersion = [\'"]managed[\'"]'));
542542
if (fileBasedApply || declarativeApply || managed) {
543543
return true;
544544
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@ plugins {
66
}
77

88
def localProperties = new Properties()
9-
def localPropertiesFile = rootProject.file('local.properties')
9+
def localPropertiesFile = rootProject.file("local.properties")
1010
if (localPropertiesFile.exists()) {
11-
localPropertiesFile.withReader('UTF-8') { reader ->
11+
localPropertiesFile.withReader("UTF-8") { reader ->
1212
localProperties.load(reader)
1313
}
1414
}
1515

16-
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
16+
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
1717
if (flutterVersionCode == null) {
18-
flutterVersionCode = '1'
18+
flutterVersionCode = "1"
1919
}
2020

21-
def flutterVersionName = localProperties.getProperty('flutter.versionName')
21+
def flutterVersionName = localProperties.getProperty("flutter.versionName")
2222
if (flutterVersionName == null) {
23-
flutterVersionName = '1.0'
23+
flutterVersionName = "1.0"
2424
}
2525

2626
android {
27-
namespace "{{androidIdentifier}}"
28-
compileSdk flutter.compileSdkVersion
29-
ndkVersion flutter.ndkVersion
27+
namespace = "{{androidIdentifier}}"
28+
compileSdk = flutter.compileSdkVersion
29+
ndkVersion = flutter.ndkVersion
3030

3131
compileOptions {
32-
sourceCompatibility JavaVersion.VERSION_1_8
33-
targetCompatibility JavaVersion.VERSION_1_8
32+
sourceCompatibility = JavaVersion.VERSION_1_8
33+
targetCompatibility = JavaVersion.VERSION_1_8
3434
}
3535

3636
defaultConfig {
3737
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
38-
applicationId "{{androidIdentifier}}"
38+
applicationId = "{{androidIdentifier}}"
3939
// You can update the following values to match your application needs.
4040
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
41-
minSdk flutter.minSdkVersion
42-
targetSdk flutter.targetSdkVersion
43-
versionCode flutterVersionCode.toInteger()
44-
versionName flutterVersionName
41+
minSdk = flutter.minSdkVersion
42+
targetSdk = flutter.targetSdkVersion
43+
versionCode = flutterVersionCode.toInteger()
44+
versionName = flutterVersionName
4545
}
4646

4747
buildTypes {
4848
release {
4949
// TODO: Add your own signing config for the release build.
5050
// Signing with the debug keys for now, so `flutter run --release` works.
51-
signingConfig signingConfigs.debug
51+
signingConfig = signingConfigs.debug
5252
}
5353
}
5454
}
5555

5656
flutter {
57-
source '../..'
57+
source = "../.."
5858
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ allprojects {
55
}
66
}
77

8-
rootProject.buildDir = '../build'
8+
rootProject.buildDir = "../build"
99
subprojects {
1010
project.buildDir = "${rootProject.buildDir}/${project.name}"
1111
}
1212
subprojects {
13-
project.evaluationDependsOn(':app')
13+
project.evaluationDependsOn(":app")
1414
}
1515

1616
tasks.register("clean", Delete) {

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,53 @@ plugins {
66
}
77

88
def localProperties = new Properties()
9-
def localPropertiesFile = rootProject.file('local.properties')
9+
def localPropertiesFile = rootProject.file("local.properties")
1010
if (localPropertiesFile.exists()) {
11-
localPropertiesFile.withReader('UTF-8') { reader ->
11+
localPropertiesFile.withReader("UTF-8") { reader ->
1212
localProperties.load(reader)
1313
}
1414
}
1515

16-
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
16+
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
1717
if (flutterVersionCode == null) {
18-
flutterVersionCode = '1'
18+
flutterVersionCode = "1"
1919
}
2020

21-
def flutterVersionName = localProperties.getProperty('flutter.versionName')
21+
def flutterVersionName = localProperties.getProperty("flutter.versionName")
2222
if (flutterVersionName == null) {
23-
flutterVersionName = '1.0'
23+
flutterVersionName = "1.0"
2424
}
2525

2626
android {
27-
namespace "{{androidIdentifier}}"
28-
compileSdk flutter.compileSdkVersion
29-
ndkVersion flutter.ndkVersion
27+
namespace = "{{androidIdentifier}}"
28+
compileSdk = flutter.compileSdkVersion
29+
ndkVersion = flutter.ndkVersion
3030

3131
compileOptions {
32-
sourceCompatibility JavaVersion.VERSION_1_8
33-
targetCompatibility JavaVersion.VERSION_1_8
34-
}
35-
36-
kotlinOptions {
37-
jvmTarget = '1.8'
38-
}
39-
40-
sourceSets {
41-
main.java.srcDirs += 'src/main/kotlin'
32+
sourceCompatibility = JavaVersion.VERSION_1_8
33+
targetCompatibility = JavaVersion.VERSION_1_8
4234
}
4335

4436
defaultConfig {
4537
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
46-
applicationId "{{androidIdentifier}}"
38+
applicationId = "{{androidIdentifier}}"
4739
// You can update the following values to match your application needs.
4840
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
49-
minSdk flutter.minSdkVersion
50-
targetSdk flutter.targetSdkVersion
51-
versionCode flutterVersionCode.toInteger()
52-
versionName flutterVersionName
41+
minSdk = flutter.minSdkVersion
42+
targetSdk = flutter.targetSdkVersion
43+
versionCode = flutterVersionCode.toInteger()
44+
versionName = flutterVersionName
5345
}
5446

5547
buildTypes {
5648
release {
5749
// TODO: Add your own signing config for the release build.
5850
// Signing with the debug keys for now, so `flutter run --release` works.
59-
signingConfig signingConfigs.debug
51+
signingConfig = signingConfigs.debug
6052
}
6153
}
6254
}
6355

6456
flutter {
65-
source '../..'
57+
source = "../.."
6658
}
67-
68-
dependencies {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ allprojects {
55
}
66
}
77

8-
rootProject.buildDir = '../build'
8+
rootProject.buildDir = "../build"
99
subprojects {
1010
project.buildDir = "${rootProject.buildDir}/${project.name}"
1111
}
1212
subprojects {
13-
project.evaluationDependsOn(':app')
13+
project.evaluationDependsOn(":app")
1414
}
1515

1616
tasks.register("clean", Delete) {
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
def localProperties = new Properties()
2-
def localPropertiesFile = rootProject.file('local.properties')
2+
def localPropertiesFile = rootProject.file("local.properties")
33
if (localPropertiesFile.exists()) {
4-
localPropertiesFile.withReader('UTF-8') { reader ->
4+
localPropertiesFile.withReader("UTF-8") { reader ->
55
localProperties.load(reader)
66
}
77
}
88

9-
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
9+
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
1010
if (flutterVersionCode == null) {
11-
flutterVersionCode = '1'
11+
flutterVersionCode = "1"
1212
}
1313

14-
def flutterVersionName = localProperties.getProperty('flutter.versionName')
14+
def flutterVersionName = localProperties.getProperty("flutter.versionName")
1515
if (flutterVersionName == null) {
16-
flutterVersionName = '1.0'
16+
flutterVersionName = "1.0"
1717
}
1818

1919
apply plugin: "com.android.dynamic-feature"
2020

2121
android {
22-
compileSdk flutter.compileSdkVersion
22+
compileSdk = flutter.compileSdkVersion
2323

2424
compileOptions {
25-
sourceCompatibility JavaVersion.VERSION_1_8
26-
targetCompatibility JavaVersion.VERSION_1_8
25+
sourceCompatibility = JavaVersion.VERSION_1_8
26+
targetCompatibility = JavaVersion.VERSION_1_8
2727
}
2828

2929
sourceSets {
@@ -34,13 +34,13 @@ android {
3434
}
3535

3636
defaultConfig {
37-
minSdk flutter.minSdkVersion
38-
targetSdk flutter.targetSdkVersion
39-
versionCode flutterVersionCode.toInteger()
40-
versionName flutterVersionName
37+
minSdk = flutter.minSdkVersion
38+
targetSdk = flutter.targetSdkVersion
39+
versionCode = flutterVersionCode.toInteger()
40+
versionName = flutterVersionName
4141
}
4242
}
4343

4444
dependencies {
45-
implementation project(":app")
45+
implementation(project(":app"))
4646
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Generated file. Do not edit.
22

33
buildscript {
4-
ext.kotlin_version = '{{kotlinVersion}}'
4+
ext.kotlin_version = "{{kotlinVersion}}"
55
repositories {
66
google()
77
mavenCentral()
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:{{agpVersionForModule}}'
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11+
classpath("com.android.tools.build:gradle:{{agpVersionForModule}}")
12+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
1313
}
1414
}
1515

@@ -20,21 +20,21 @@ allprojects {
2020
}
2121
}
2222

23-
apply plugin: 'com.android.library'
24-
apply plugin: 'kotlin-android'
23+
apply plugin: "com.android.library"
24+
apply plugin: "kotlin-android"
2525

2626
android {
2727
// Conditional for compatibility with AGP <4.2.
2828
if (project.android.hasProperty("namespace")) {
29-
namespace '{{androidIdentifier}}'
29+
namespace = "{{androidIdentifier}}"
3030
}
3131

32-
compileSdk {{compileSdkVersion}}
32+
compileSdk = {{compileSdkVersion}}
3333
defaultConfig {
34-
minSdk {{minSdkVersion}}
34+
minSdk = {{minSdkVersion}}
3535
}
3636
}
3737

3838
dependencies {
39-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
39+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
4040
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
def flutterPluginVersion = 'managed'
1+
def flutterPluginVersion = "managed"
22

3-
apply plugin: 'com.android.application'
3+
apply plugin: "com.android.application"
44

55
android {
66
// Conditional for compatibility with AGP <4.2.
77
if (project.android.hasProperty("namespace")) {
8-
namespace "{{androidIdentifier}}.host"
8+
namespace = "{{androidIdentifier}}.host"
99
}
1010

11-
compileSdk {{compileSdkVersion}}
11+
compileSdk = {{compileSdkVersion}}
1212

1313
compileOptions {
14-
sourceCompatibility JavaVersion.VERSION_1_8
15-
targetCompatibility JavaVersion.VERSION_1_8
14+
sourceCompatibility = JavaVersion.VERSION_1_8
15+
targetCompatibility = JavaVersion.VERSION_1_8
1616
}
1717

1818
defaultConfig {
19-
applicationId "{{androidIdentifier}}.host"
20-
minSdk {{minSdkVersion}}
21-
targetSdk {{targetSdkVersion}}
22-
versionCode 1
23-
versionName "1.0"
19+
applicationId = "{{androidIdentifier}}.host"
20+
minSdk = {{minSdkVersion}}
21+
targetSdk = {{targetSdkVersion}}
22+
versionCode = 1
23+
versionName = "1.0"
2424
}
2525

2626
buildTypes {
@@ -30,15 +30,15 @@ android {
3030
release {
3131
// TODO: Add your own signing config for the release build.
3232
// Signing with the debug keys for now, so `flutter run --release` works.
33-
signingConfig signingConfigs.debug
33+
signingConfig = signingConfigs.debug
3434
}
3535
}
3636

3737
}
3838
buildDir = new File(rootProject.projectDir, "../build/host")
3939
dependencies {
40-
implementation project(':flutter')
41-
implementation fileTree(dir: 'libs', include: ['*.jar'])
42-
implementation 'androidx.appcompat:appcompat:1.0.2'
43-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
40+
implementation(project(":flutter"))
41+
implementation(fileTree(dir: "libs", include: ["*.jar"]))
42+
implementation("androidx.appcompat:appcompat:1.0.2")
43+
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
4444
}

0 commit comments

Comments
 (0)