Skip to content

Commit

Permalink
Update Android plugin templates for newer AGP (flutter#156533)
Browse files Browse the repository at this point in the history
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 flutter/packages#7795 which made this change in Flutter-team-owned plugins.

Part of flutter#156111
  • Loading branch information
stuartmorgan authored and GitHub Actions Bot committed Oct 17, 2024
1 parent ee624bc commit 0f37b4f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3163,6 +3163,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';
Expand Down

0 comments on commit 0f37b4f

Please sign in to comment.