Skip to content

Commit aa23014

Browse files
Add namespace to Android plugin templates (#126354)
Adds the `namespace` property necessary for AGP 8 compatibility to the plugin templates, with the conditional logic to ensure that it doesn't break AGP <4.2, so that new plugins will be maximally compatible. Part of flutter/flutter#125181
1 parent 9c72f5a commit aa23014

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ rootProject.allprojects {
2222
apply plugin: 'com.android.library'
2323

2424
android {
25+
if (project.android.hasProperty("namespace")) {
26+
namespace '{{androidIdentifier}}'
27+
}
28+
2529
compileSdkVersion {{compileSdkVersion}}
2630

2731
compileOptions {

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

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

2727
android {
28+
if (project.android.hasProperty("namespace")) {
29+
namespace '{{androidIdentifier}}'
30+
}
31+
2832
compileSdkVersion 31
2933

3034
compileOptions {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ rootProject.allprojects {
2525
apply plugin: 'com.android.library'
2626

2727
android {
28+
if (project.android.hasProperty("namespace")) {
29+
namespace '{{androidIdentifier}}'
30+
}
31+
2832
// Bumping the plugin compileSdkVersion requires all clients of this plugin
2933
// to bump the version in their app.
3034
compileSdkVersion {{compileSdkVersion}}

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,77 @@ void main() {
28482848
expect(buildContent.contains('targetSdkVersion flutter.targetSdkVersion'), true);
28492849
});
28502850

2851+
testUsingContext('Android Java plugin contains namespace', () async {
2852+
Cache.flutterRoot = '../..';
2853+
2854+
final CreateCommand command = CreateCommand();
2855+
final CommandRunner<void> runner = createTestCommandRunner(command);
2856+
2857+
await runner.run(<String>['create', '--no-pub',
2858+
'-t', 'plugin',
2859+
'--org', 'com.bar.foo',
2860+
'-a', 'java',
2861+
'--platforms=android',
2862+
projectDir.path]);
2863+
2864+
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
2865+
2866+
expect(buildGradleFile.existsSync(), true);
2867+
2868+
final String buildGradleContent = await buildGradleFile.readAsString();
2869+
2870+
expect(buildGradleContent.contains("namespace 'com.bar.foo.flutter_project'"), true);
2871+
// The namespace should be conditionalized for AGP <4.2.
2872+
expect(buildGradleContent.contains('if (project.android.hasProperty("namespace")) {'), true);
2873+
});
2874+
2875+
testUsingContext('Android FFI plugin contains namespace', () async {
2876+
Cache.flutterRoot = '../..';
2877+
2878+
final CreateCommand command = CreateCommand();
2879+
final CommandRunner<void> runner = createTestCommandRunner(command);
2880+
2881+
await runner.run(<String>['create', '--no-pub',
2882+
'-t', 'plugin_ffi',
2883+
'--org', 'com.bar.foo',
2884+
'--platforms=android',
2885+
projectDir.path]);
2886+
2887+
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
2888+
2889+
expect(buildGradleFile.existsSync(), true);
2890+
2891+
final String buildGradleContent = await buildGradleFile.readAsString();
2892+
2893+
expect(buildGradleContent.contains("namespace 'com.bar.foo.flutter_project'"), true);
2894+
// The namespace should be conditionalized for AGP <4.2.
2895+
expect(buildGradleContent.contains('if (project.android.hasProperty("namespace")) {'), true);
2896+
});
2897+
2898+
testUsingContext('Android Kotlin plugin contains namespace', () async {
2899+
Cache.flutterRoot = '../..';
2900+
2901+
final CreateCommand command = CreateCommand();
2902+
final CommandRunner<void> runner = createTestCommandRunner(command);
2903+
2904+
await runner.run(<String>['create', '--no-pub',
2905+
'-t', 'plugin',
2906+
'--org', 'com.bar.foo',
2907+
'-a', 'kotlin',
2908+
'--platforms=android',
2909+
projectDir.path]);
2910+
2911+
final File buildGradleFile = globals.fs.file('${projectDir.path}/android/build.gradle');
2912+
2913+
expect(buildGradleFile.existsSync(), true);
2914+
2915+
final String buildGradleContent = await buildGradleFile.readAsString();
2916+
2917+
expect(buildGradleContent.contains("namespace 'com.bar.foo.flutter_project'"), true);
2918+
// The namespace should be conditionalized for AGP <4.2.
2919+
expect(buildGradleContent.contains('if (project.android.hasProperty("namespace")) {'), true);
2920+
});
2921+
28512922
testUsingContext('Linux plugins handle partially camel-case project names correctly', () async {
28522923
Cache.flutterRoot = '../..';
28532924

0 commit comments

Comments
 (0)