From e8254b202492343a321b9a34b226bc82641f1b44 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 10 Oct 2024 10:18:50 -0700 Subject: [PATCH] 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 --- .../android-java.tmpl/build.gradle.tmpl | 8 ++-- .../android-kotlin.tmpl/build.gradle.tmpl | 10 ++-- .../plugin_ffi/android.tmpl/build.gradle.tmpl | 8 ++-- .../commands.shard/permeable/create_test.dart | 48 +++++++++++++++++++ 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl index 767f5599acf..253a1e8828e 100644 --- a/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl +++ b/packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl @@ -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 { diff --git a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl index bd3c2d811bd..2b8ba38a8fa 100644 --- a/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl +++ b/packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl @@ -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 { diff --git a/packages/flutter_tools/templates/plugin_ffi/android.tmpl/build.gradle.tmpl b/packages/flutter_tools/templates/plugin_ffi/android.tmpl/build.gradle.tmpl index 140b8dfe6e5..0c4de872838 100644 --- a/packages/flutter_tools/templates/plugin_ffi/android.tmpl/build.gradle.tmpl +++ b/packages/flutter_tools/templates/plugin_ffi/android.tmpl/build.gradle.tmpl @@ -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 { diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index a330992c174..acfb0e6ff18 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -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 runner = createTestCommandRunner(command); + + await runner.run(['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 runner = createTestCommandRunner(command); + + await runner.run(['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';