Unverified Commit 935d1478 authored by Emmanuel Garcia's avatar Emmanuel Garcia Committed by GitHub

Apply the Kotlin plugin in a java project (#95225)

parent 22bd7246
......@@ -19,7 +19,17 @@ import '../project.dart';
import '../reporting/reporting.dart';
import 'android_sdk.dart';
const String _defaultGradleVersion = '6.7';
// These are the versions used in the project templates.
//
// In general, Flutter aims to default to the latest version.
// However, this currently requires to migrate existing integration tests to the latest supported values.
//
// For more information about the latest version, check:
// https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
// https://kotlinlang.org/docs/gradle.html#plugin-and-versions
const String templateDefaultGradleVersion = '6.7';
const String templateAndroidGradlePluginVersion = '4.1.0';
const String templateKotlinGradlePluginVersion = '1.4.32';
final RegExp _androidPluginRegExp = RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
......@@ -109,14 +119,14 @@ distributionUrl=https\\://services.gradle.org/distributions/gradle-$gradleVersio
String getGradleVersionForAndroidPlugin(Directory directory, Logger logger) {
final File buildFile = directory.childFile('build.gradle');
if (!buildFile.existsSync()) {
logger.printTrace("$buildFile doesn't exist, assuming AGP version: $_defaultGradleVersion");
return _defaultGradleVersion;
logger.printTrace("$buildFile doesn't exist, assuming Gradle version: $templateDefaultGradleVersion");
return templateDefaultGradleVersion;
}
final String buildFileContent = buildFile.readAsStringSync();
final Iterable<Match> pluginMatches = _androidPluginRegExp.allMatches(buildFileContent);
if (pluginMatches.isEmpty) {
logger.printTrace("$buildFile doesn't provide an AGP version, assuming AGP version: $_defaultGradleVersion");
return _defaultGradleVersion;
logger.printTrace("$buildFile doesn't provide an AGP version, assuming Gradle version: $templateDefaultGradleVersion");
return templateDefaultGradleVersion;
}
final String? androidPluginVersion = pluginMatches.first.group(1);
logger.printTrace('$buildFile provides AGP version: $androidPluginVersion');
......
......@@ -271,6 +271,9 @@ class CreateCommand extends CreateBase {
// Enable null safety everywhere.
dartSdkVersionBounds: '">=$dartSdk <3.0.0"',
implementationTests: boolArg('implementation-tests'),
agpVersion: gradle.templateAndroidGradlePluginVersion,
kotlinVersion: gradle.templateKotlinGradlePluginVersion,
gradleVersion: gradle.templateDefaultGradleVersion,
);
final String relativeDirPath = globals.fs.path.relative(projectDirPath);
......
......@@ -336,6 +336,9 @@ abstract class CreateBase extends FlutterCommand {
String iosLanguage,
String flutterRoot,
String dartSdkVersionBounds,
String agpVersion,
String kotlinVersion,
String gradleVersion,
bool withPluginHook = false,
bool ios = false,
bool android = false,
......@@ -398,6 +401,9 @@ abstract class CreateBase extends FlutterCommand {
'year': DateTime.now().year,
'dartSdkVersionBounds': dartSdkVersionBounds,
'implementationTests': implementationTests,
'agpVersion': agpVersion,
'kotlinVersion': kotlinVersion,
'gradleVersion': gradleVersion,
};
}
......
......@@ -543,6 +543,9 @@ class AndroidProject extends FlutterProjectPlatform {
'projectName': parent.manifest.appName,
'androidIdentifier': androidIdentifier,
'androidX': usesAndroidX,
'agpVersion': gradle.templateAndroidGradlePluginVersion,
'kotlinVersion': gradle.templateKotlinGradlePluginVersion,
'gradleVersion': gradle.templateDefaultGradleVersion,
},
printStatusWhenWriting: false,
);
......
......@@ -22,6 +22,7 @@ if (flutterVersionName == null) {
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
......
buildscript {
ext.kotlin_version = '1.4.32'
ext.kotlin_version = '{{kotlinVersion}}'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:{{agpVersion}}'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......
buildscript {
ext.kotlin_version = '{{kotlinVersion}}'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:{{agpVersion}}'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......
......@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-{{gradleVersion}}-all.zip
......@@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:{{agpVersion}}'
}
}
......
......@@ -2,14 +2,14 @@ group '{{androidIdentifier}}'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.4.32'
ext.kotlin_version = '{{kotlinVersion}}'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:{{agpVersion}}'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......
......@@ -18,11 +18,11 @@
"templates/app_shared/analysis_options.yaml.tmpl",
"templates/app_shared/android-java.tmpl/app/build.gradle.tmpl",
"templates/app_shared/android-java.tmpl/app/src/main/java/androidIdentifier/MainActivity.java.tmpl",
"templates/app_shared/android-java.tmpl/build.gradle",
"templates/app_shared/android-java.tmpl/build.gradle.tmpl",
"templates/app_shared/android-java.tmpl/projectName_android.iml.tmpl",
"templates/app_shared/android-kotlin.tmpl/app/build.gradle.tmpl",
"templates/app_shared/android-kotlin.tmpl/app/src/main/kotlin/androidIdentifier/MainActivity.kt.tmpl",
"templates/app_shared/android-kotlin.tmpl/build.gradle",
"templates/app_shared/android-kotlin.tmpl/build.gradle.tmpl",
"templates/app_shared/android-kotlin.tmpl/projectName_android.iml.tmpl",
"templates/app_shared/android.tmpl/.gitignore",
"templates/app_shared/android.tmpl/app/src/debug/AndroidManifest.xml.tmpl",
......@@ -38,7 +38,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/gradle/wrapper/gradle-wrapper.properties",
"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",
"templates/app_shared/ios-objc.tmpl/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme",
......@@ -207,7 +207,7 @@
"templates/module/android/deferred_component/build.gradle.tmpl",
"templates/module/android/deferred_component/src/main/AndroidManifest.xml.tmpl",
"templates/module/android/gradle/build.gradle.copy.tmpl",
"templates/module/android/gradle/build.gradle.tmpl",
"templates/module/android/gradle/gradle.properties.tmpl",
"templates/module/android/host_app_common/app.tmpl/build.gradle.tmpl",
"templates/module/android/host_app_common/app.tmpl/src/main/AndroidManifest.xml.tmpl",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment