Update Android plugin templates for newer AGP (#156533)

Now that Flutter requires AGP 7+, we can use Java 11 as the compatibility version in the plugin template rather than 1.8, avoiding warnings with newer toolchains, and we can remove the check for 'namespace' existing that was only necessary to support AGP 4.1.

See also https://github.com/flutter/packages/pull/7795 which made this change in Flutter-team-owned plugins.

Part of https://github.com/flutter/flutter/issues/156111
This commit is contained in:
stuartmorgan 2024-10-10 10:18:50 -07:00 committed by GitHub
parent 225e4aabb5
commit e8254b2024
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 16 deletions

View File

@ -22,15 +22,13 @@ rootProject.allprojects {
apply plugin: "com.android.library"
android {
if (project.android.hasProperty("namespace")) {
namespace = "{{androidIdentifier}}"
}
namespace = "{{androidIdentifier}}"
compileSdk = {{compileSdkVersion}}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
defaultConfig {

View File

@ -25,19 +25,17 @@ apply plugin: "com.android.library"
apply plugin: "kotlin-android"
android {
if (project.android.hasProperty("namespace")) {
namespace = "{{androidIdentifier}}"
}
namespace = "{{androidIdentifier}}"
compileSdk = {{compileSdkVersion}}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
jvmTarget = JavaVersion.VERSION_11
}
sourceSets {

View File

@ -25,9 +25,7 @@ rootProject.allprojects {
apply plugin: "com.android.library"
android {
if (project.android.hasProperty("namespace")) {
namespace = "{{androidIdentifier}}"
}
namespace = "{{androidIdentifier}}"
// Bumping the plugin compileSdk version requires all clients of this plugin
// to bump the version in their app.
@ -55,8 +53,8 @@ android {
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
defaultConfig {

View File

@ -3231,6 +3231,54 @@ void main() {
expect(buildGradleContent.contains('namespace = "com.bar.foo.flutter_project"'), true);
});
testUsingContext('Android Java plugin sets explicit compatibility version', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub',
'-t', 'plugin',
'--org', 'com.bar.foo',
'-a', 'java',
'--platforms=android',
projectDir.path]);
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
expect(buildGradleFile.existsSync(), true);
final String buildGradleContent = await buildGradleFile.readAsString();
expect(buildGradleContent.contains('sourceCompatibility = JavaVersion.VERSION_11'), true);
expect(buildGradleContent.contains('targetCompatibility = JavaVersion.VERSION_11'), true);
});
testUsingContext('Android Kotlin plugin sets explicit compatibility version', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub',
'-t', 'plugin',
'--org', 'com.bar.foo',
'-a', 'kotlin',
'--platforms=android',
projectDir.path]);
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
expect(buildGradleFile.existsSync(), true);
final String buildGradleContent = await buildGradleFile.readAsString();
expect(buildGradleContent.contains('sourceCompatibility = JavaVersion.VERSION_11'), true);
expect(buildGradleContent.contains('targetCompatibility = JavaVersion.VERSION_11'), true);
// jvmTarget should be set to the same value.
expect(buildGradleContent.contains('jvmTarget = JavaVersion.VERSION_11'), true);
});
testUsingContext('Flutter module Android project contains namespace', () async {
const String moduleBuildGradleFilePath = '.android/build.gradle';
const String moduleAppBuildGradleFlePath = '.android/app/build.gradle';