Skip to content

Commit e466195

Browse files
[CP-beta]Update Android plugin templates for newer AGP (flutter#157100)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: Part of flutter#156111 ### Changelog Description: Updates the Android plugin templates to use newer configurations that have better compatibility with recent versions of AGP. ### Impact Description: Without this, plugins created by `flutter create` will have deprecation warnings out of the box for developers whose projects use new versions of AGP, and will eventually fail to compile without manual updates. ### Workaround: Manually change the Java version after running `flutter create` ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. `flutter create -t plugin some_plugin --platforms=android` 2. Update the example project to the latest version of AGP 3. Build There should not be warnings about `source value 8 is obsolete` or `target value 8 is obsolete`.
1 parent 72ccdd2 commit e466195

4 files changed

Lines changed: 58 additions & 16 deletions

File tree

packages/flutter_tools/templates/plugin/android-java.tmpl/build.gradle.tmpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ rootProject.allprojects {
2222
apply plugin: "com.android.library"
2323

2424
android {
25-
if (project.android.hasProperty("namespace")) {
26-
namespace = "{{androidIdentifier}}"
27-
}
25+
namespace = "{{androidIdentifier}}"
2826

2927
compileSdk = {{compileSdkVersion}}
3028

3129
compileOptions {
32-
sourceCompatibility = JavaVersion.VERSION_1_8
33-
targetCompatibility = JavaVersion.VERSION_1_8
30+
sourceCompatibility = JavaVersion.VERSION_11
31+
targetCompatibility = JavaVersion.VERSION_11
3432
}
3533

3634
defaultConfig {

packages/flutter_tools/templates/plugin/android-kotlin.tmpl/build.gradle.tmpl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ apply plugin: "com.android.library"
2525
apply plugin: "kotlin-android"
2626

2727
android {
28-
if (project.android.hasProperty("namespace")) {
29-
namespace = "{{androidIdentifier}}"
30-
}
28+
namespace = "{{androidIdentifier}}"
3129

3230
compileSdk = {{compileSdkVersion}}
3331

3432
compileOptions {
35-
sourceCompatibility = JavaVersion.VERSION_1_8
36-
targetCompatibility = JavaVersion.VERSION_1_8
33+
sourceCompatibility = JavaVersion.VERSION_11
34+
targetCompatibility = JavaVersion.VERSION_11
3735
}
3836

3937
kotlinOptions {
40-
jvmTarget = JavaVersion.VERSION_1_8
38+
jvmTarget = JavaVersion.VERSION_11
4139
}
4240

4341
sourceSets {

packages/flutter_tools/templates/plugin_ffi/android.tmpl/build.gradle.tmpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ rootProject.allprojects {
2525
apply plugin: "com.android.library"
2626

2727
android {
28-
if (project.android.hasProperty("namespace")) {
29-
namespace = "{{androidIdentifier}}"
30-
}
28+
namespace = "{{androidIdentifier}}"
3129

3230
// Bumping the plugin compileSdk version requires all clients of this plugin
3331
// to bump the version in their app.
@@ -55,8 +53,8 @@ android {
5553
}
5654

5755
compileOptions {
58-
sourceCompatibility = JavaVersion.VERSION_1_8
59-
targetCompatibility = JavaVersion.VERSION_1_8
56+
sourceCompatibility = JavaVersion.VERSION_11
57+
targetCompatibility = JavaVersion.VERSION_11
6058
}
6159

6260
defaultConfig {

packages/flutter_tools/test/commands.shard/permeable/create_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,54 @@ void main() {
32313231
expect(buildGradleContent.contains('namespace = "com.bar.foo.flutter_project"'), true);
32323232
});
32333233

3234+
testUsingContext('Android Java plugin sets explicit compatibility version', () async {
3235+
Cache.flutterRoot = '../..';
3236+
3237+
final CreateCommand command = CreateCommand();
3238+
final CommandRunner<void> runner = createTestCommandRunner(command);
3239+
3240+
await runner.run(<String>['create', '--no-pub',
3241+
'-t', 'plugin',
3242+
'--org', 'com.bar.foo',
3243+
'-a', 'java',
3244+
'--platforms=android',
3245+
projectDir.path]);
3246+
3247+
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
3248+
3249+
expect(buildGradleFile.existsSync(), true);
3250+
3251+
final String buildGradleContent = await buildGradleFile.readAsString();
3252+
3253+
expect(buildGradleContent.contains('sourceCompatibility = JavaVersion.VERSION_11'), true);
3254+
expect(buildGradleContent.contains('targetCompatibility = JavaVersion.VERSION_11'), true);
3255+
});
3256+
3257+
testUsingContext('Android Kotlin plugin sets explicit compatibility version', () async {
3258+
Cache.flutterRoot = '../..';
3259+
3260+
final CreateCommand command = CreateCommand();
3261+
final CommandRunner<void> runner = createTestCommandRunner(command);
3262+
3263+
await runner.run(<String>['create', '--no-pub',
3264+
'-t', 'plugin',
3265+
'--org', 'com.bar.foo',
3266+
'-a', 'kotlin',
3267+
'--platforms=android',
3268+
projectDir.path]);
3269+
3270+
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
3271+
3272+
expect(buildGradleFile.existsSync(), true);
3273+
3274+
final String buildGradleContent = await buildGradleFile.readAsString();
3275+
3276+
expect(buildGradleContent.contains('sourceCompatibility = JavaVersion.VERSION_11'), true);
3277+
expect(buildGradleContent.contains('targetCompatibility = JavaVersion.VERSION_11'), true);
3278+
// jvmTarget should be set to the same value.
3279+
expect(buildGradleContent.contains('jvmTarget = JavaVersion.VERSION_11'), true);
3280+
});
3281+
32343282
testUsingContext('Flutter module Android project contains namespace', () async {
32353283
const String moduleBuildGradleFilePath = '.android/build.gradle';
32363284
const String moduleAppBuildGradleFlePath = '.android/app/build.gradle';

0 commit comments

Comments
 (0)