Use Gradle KTS in new Android app projects by default (2nd try) (#157195)
This PR resolves #151166 This PR relands #154061
This commit is contained in:
@@ -29,14 +29,17 @@ Future<void> main() async {
|
||||
await runProjectTest((FlutterProject flutterProject) async {
|
||||
await inDirectory(path.join(flutterProject.rootPath, 'android'), () async {
|
||||
section('Insert gradle testing script');
|
||||
final File build = File(path.join(
|
||||
flutterProject.rootPath, 'android', 'app', 'build.gradle',
|
||||
));
|
||||
build.writeAsStringSync(
|
||||
final File buildFile = getAndroidBuildFile(path.join(flutterProject.rootPath, 'android', 'app'));
|
||||
buildFile.writeAsStringSync(
|
||||
'''
|
||||
task printEngineMavenUrl() {
|
||||
tasks.register("printEngineMavenUrl") {
|
||||
doLast {
|
||||
println project.repositories.find { it.name == 'maven' }.url
|
||||
project.repositories.forEach { repo ->
|
||||
if (repo.name == "maven") {
|
||||
repo as MavenArtifactRepository
|
||||
logger.quiet(repo.url.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
''',
|
||||
|
||||
@@ -256,18 +256,17 @@ class FlutterProject {
|
||||
String get rootPath => path.join(parent.path, name);
|
||||
String get androidPath => path.join(rootPath, 'android');
|
||||
String get iosPath => path.join(rootPath, 'ios');
|
||||
File get appBuildFile => getAndroidBuildFile(path.join(androidPath, 'app'));
|
||||
|
||||
Future<void> addCustomBuildType(String name, {required String initWith}) async {
|
||||
final File buildScript = File(
|
||||
path.join(androidPath, 'app', 'build.gradle'),
|
||||
);
|
||||
final File buildScript = appBuildFile;
|
||||
|
||||
buildScript.openWrite(mode: FileMode.append).write('''
|
||||
|
||||
android {
|
||||
buildTypes {
|
||||
$name {
|
||||
initWith $initWith
|
||||
create("$name") {
|
||||
initWith(getByName("$initWith"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,14 +287,12 @@ android {
|
||||
}
|
||||
|
||||
Future<void> setMinSdkVersion(int sdkVersion) async {
|
||||
final File buildScript = File(
|
||||
path.join(androidPath, 'app', 'build.gradle'),
|
||||
);
|
||||
final File buildScript = appBuildFile;
|
||||
|
||||
buildScript.openWrite(mode: FileMode.append).write('''
|
||||
android {
|
||||
defaultConfig {
|
||||
minSdkVersion $sdkVersion
|
||||
minSdk = $sdkVersion
|
||||
}
|
||||
}
|
||||
''');
|
||||
@@ -308,22 +305,20 @@ android {
|
||||
}
|
||||
|
||||
Future<void> addProductFlavors(Iterable<String> flavors) async {
|
||||
final File buildScript = File(
|
||||
path.join(androidPath, 'app', 'build.gradle'),
|
||||
);
|
||||
final File buildScript = appBuildFile;
|
||||
|
||||
final String flavorConfig = flavors.map((String name) {
|
||||
return '''
|
||||
$name {
|
||||
applicationIdSuffix ".$name"
|
||||
versionNameSuffix "-$name"
|
||||
create("$name") {
|
||||
applicationIdSuffix = ".$name"
|
||||
versionNameSuffix = "-$name"
|
||||
}
|
||||
''';
|
||||
}).join('\n');
|
||||
|
||||
buildScript.openWrite(mode: FileMode.append).write('''
|
||||
android {
|
||||
flavorDimensions "mode"
|
||||
flavorDimensions.add("mode")
|
||||
productFlavors {
|
||||
$flavorConfig
|
||||
}
|
||||
@@ -332,9 +327,7 @@ android {
|
||||
}
|
||||
|
||||
Future<void> introduceError() async {
|
||||
final File buildScript = File(
|
||||
path.join(androidPath, 'app', 'build.gradle'),
|
||||
);
|
||||
final File buildScript = appBuildFile;
|
||||
await buildScript.writeAsString((await buildScript.readAsString()).replaceAll('buildTypes', 'builTypes'));
|
||||
}
|
||||
|
||||
@@ -477,3 +470,14 @@ String? validateSnapshotDependency(FlutterProject project, String expectedTarget
|
||||
return contentSnapshot.contains('$expectedTarget ')
|
||||
? null : 'Dependency file should have $expectedTarget as target. Instead found $contentSnapshot';
|
||||
}
|
||||
|
||||
File getAndroidBuildFile(String androidAppPath, {bool settings = false}) {
|
||||
final File groovyFile = File(path.join(androidAppPath, settings ? 'settings.gradle' : 'build.gradle'));
|
||||
final File kotlinFile = File(path.join(androidAppPath, settings ? 'settings.gradle.kts' : 'build.gradle.kts'));
|
||||
|
||||
if (groovyFile.existsSync()) {
|
||||
return groovyFile;
|
||||
}
|
||||
|
||||
return kotlinFile;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file/local.dart';
|
||||
|
||||
import 'apk_utils.dart';
|
||||
import 'task_result.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
@@ -16,13 +18,13 @@ import 'utils.dart';
|
||||
/// by an easily find/replaceable string.
|
||||
const String gradleSettingsFileContent = r'''
|
||||
pluginManagement {
|
||||
def flutterSdkPath = {
|
||||
def properties = new Properties()
|
||||
file("local.properties").withInputStream { properties.load(it) }
|
||||
def flutterSdkPath = properties.getProperty("flutter.sdk")
|
||||
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
|
||||
return flutterSdkPath
|
||||
}()
|
||||
val flutterSdkPath = run {
|
||||
val properties = java.util.Properties()
|
||||
file("local.properties").inputStream().use { properties.load(it) }
|
||||
val flutterSdkPath = properties.getProperty("flutter.sdk")
|
||||
require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" }
|
||||
flutterSdkPath
|
||||
}
|
||||
|
||||
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
|
||||
|
||||
@@ -34,12 +36,12 @@ pluginManagement {
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
|
||||
id "com.android.application" version "AGP_REPLACE_ME" apply false
|
||||
id "org.jetbrains.kotlin.android" version "KGP_REPLACE_ME" apply false
|
||||
id("dev.flutter.flutter-plugin-loader") version "1.0.0"
|
||||
id("com.android.application") version "AGP_REPLACE_ME" apply false
|
||||
id("org.jetbrains.kotlin.android") version "KGP_REPLACE_ME" apply false
|
||||
}
|
||||
|
||||
include ":app"
|
||||
include(":app")
|
||||
|
||||
''';
|
||||
|
||||
@@ -58,7 +60,7 @@ distributionUrl=https\://services.gradle.org/distributions/gradle-GRADLE_REPLACE
|
||||
''';
|
||||
|
||||
const String gradleReplacementString = 'GRADLE_REPLACE_ME';
|
||||
const String flutterCompileSdkString = 'flutter.compileSdkVersion';
|
||||
final RegExp flutterCompileSdkString = RegExp(r'flutter\.compileSdkVersion|flutter\.compileSdk');
|
||||
|
||||
/// A simple class containing a Kotlin, Gradle, and AGP version.
|
||||
class VersionTuple {
|
||||
@@ -110,8 +112,7 @@ Future<TaskResult> buildFlutterApkWithSpecifiedDependencyVersions({
|
||||
final String appPath = '${innerTempDir.absolute.path}/dependency_checker_app';
|
||||
|
||||
if (versions.compileSdkVersion != null) {
|
||||
final File appGradleBuild = localFileSystem.file(localFileSystem.path.join(
|
||||
appPath, 'android', 'app', 'build.gradle'));
|
||||
final File appGradleBuild = getAndroidBuildFile(localFileSystem.path.join(appPath, 'android', 'app'));
|
||||
final String appBuildContent = appGradleBuild.readAsStringSync()
|
||||
.replaceFirst(flutterCompileSdkString, versions.compileSdkVersion!);
|
||||
appGradleBuild.writeAsStringSync(appBuildContent);
|
||||
@@ -126,12 +127,14 @@ Future<TaskResult> buildFlutterApkWithSpecifiedDependencyVersions({
|
||||
);
|
||||
await gradleWrapperProperties.writeAsString(propertyContent, flush: true);
|
||||
|
||||
final File gradleSettings = localFileSystem.file(localFileSystem.path.join(
|
||||
appPath, 'android', 'settings.gradle'));
|
||||
final File gradleSettingsFile = getAndroidBuildFile(
|
||||
localFileSystem.path.join(appPath, 'android'),
|
||||
settings: true,
|
||||
);
|
||||
final String settingsContent = gradleSettingsFileContent
|
||||
.replaceFirst(agpReplacementString, versions.agpVersion)
|
||||
.replaceFirst(kgpReplacementString, versions.kotlinVersion);
|
||||
await gradleSettings.writeAsString(settingsContent, flush: true);
|
||||
await gradleSettingsFile.writeAsString(settingsContent, flush: true);
|
||||
|
||||
|
||||
// Ensure that gradle files exists from templates.
|
||||
|
||||
Reference in New Issue
Block a user