Skip to content

Commit 4429073

Browse files
[CP-stable]Migrate module templates to declarative application of the Flutter Gradle Plugin (#160090)
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: What is the link to the issue this cherry-pick is addressing? flutter/flutter#159729 ### Changelog Description: Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples flutter/flutter#159729: Warning in logs after a fresh flutter module create when building an aar for android. ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch) Warning after flutter create of a module. ### Workaround: Is there a workaround for this issue? Manual migration after 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: What are the steps to validate that this fix works? create module then build as app or aar then look in logs for warnings about gradle imperative apply
1 parent d9d3889 commit 4429073

File tree

10 files changed

+110
-36
lines changed

10 files changed

+110
-36
lines changed

dev/devicelab/bin/tasks/build_android_host_app_with_module_aar.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class ModuleTest {
4141

4242
static const String buildTarget = 'module-gradle';
4343
final String gradleVersion;
44+
final StringBuffer stdout = StringBuffer();
45+
final StringBuffer stderr = StringBuffer();
4446

4547
Future<TaskResult> call() async {
4648
section('Running: $buildTarget-$gradleVersion');
@@ -61,6 +63,8 @@ class ModuleTest {
6163
await flutter(
6264
'create',
6365
options: <String>['--org', 'io.flutter.devicelab', '--template=module', 'hello'],
66+
output: stdout,
67+
stderr: stderr,
6468
);
6569
});
6670

@@ -69,6 +73,8 @@ class ModuleTest {
6973
await flutter(
7074
'config',
7175
options: <String>['--enable-native-assets'],
76+
output: stdout,
77+
stderr: stderr,
7278
);
7379

7480
const String ffiPackageName = 'ffi_package';
@@ -87,6 +93,8 @@ class ModuleTest {
8793
await flutter(
8894
'packages',
8995
options: <String>['get'],
96+
output: stdout,
97+
stderr: stderr,
9098
);
9199
});
92100

@@ -127,6 +135,8 @@ class ModuleTest {
127135
await flutter(
128136
'packages',
129137
options: <String>['get'],
138+
output: stdout,
139+
stderr: stderr,
130140
);
131141
});
132142

@@ -162,6 +172,8 @@ class ModuleTest {
162172
await flutter(
163173
'build',
164174
options: <String>['apk'],
175+
output: stdout,
176+
stderr: stderr,
165177
);
166178
});
167179

@@ -182,7 +194,11 @@ class ModuleTest {
182194
section('Clean build');
183195

184196
await inDirectory(projectDir, () async {
185-
await flutter('clean');
197+
await flutter(
198+
'clean',
199+
output: stdout,
200+
stderr: stderr,
201+
);
186202
});
187203

188204
section('Make Android host app editable');
@@ -191,6 +207,8 @@ class ModuleTest {
191207
await flutter(
192208
'make-host-app-editable',
193209
options: <String>['android'],
210+
output: stdout,
211+
stderr: stderr,
194212
);
195213
});
196214

@@ -200,6 +218,8 @@ class ModuleTest {
200218
await flutter(
201219
'build',
202220
options: <String>['apk'],
221+
output: stdout,
222+
stderr: stderr,
203223
);
204224
});
205225

@@ -435,6 +455,12 @@ class ModuleTest {
435455
return TaskResult.failure('Failed to make assets user-readable and writable');
436456
}
437457

458+
section('Check for specific log errors.');
459+
final String finalStderr = stderr.toString();
460+
if (finalStderr.contains("You are applying Flutter's main Gradle plugin imperatively")) {
461+
return TaskResult.failure('Applied the Flutter Gradle Plugin imperatively');
462+
}
463+
438464
return TaskResult.success(null);
439465
} on TaskResult catch (taskResult) {
440466
return taskResult;

dev/devicelab/bin/tasks/build_android_host_app_with_module_source.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class ModuleTest {
4141

4242
static const String buildTarget = 'module-gradle';
4343
final String gradleVersion;
44+
final StringBuffer stdout = StringBuffer();
45+
final StringBuffer stderr = StringBuffer();
4446

4547
Future<TaskResult> call() async {
4648
section('Running: $buildTarget-$gradleVersion');
@@ -61,6 +63,8 @@ class ModuleTest {
6163
await flutter(
6264
'create',
6365
options: <String>['--org', 'io.flutter.devicelab', '--template=module', 'hello'],
66+
output: stdout,
67+
stderr: stderr,
6468
);
6569
});
6670

@@ -69,6 +73,8 @@ class ModuleTest {
6973
await flutter(
7074
'config',
7175
options: <String>['--enable-native-assets'],
76+
output: stdout,
77+
stderr: stderr,
7278
);
7379

7480
const String ffiPackageName = 'ffi_package';
@@ -87,6 +93,8 @@ class ModuleTest {
8793
await flutter(
8894
'packages',
8995
options: <String>['get'],
96+
output: stdout,
97+
stderr: stderr,
9098
);
9199
});
92100

@@ -127,6 +135,8 @@ class ModuleTest {
127135
await flutter(
128136
'packages',
129137
options: <String>['get'],
138+
output: stdout,
139+
stderr: stderr,
130140
);
131141
});
132142

@@ -136,6 +146,8 @@ class ModuleTest {
136146
await flutter(
137147
'build',
138148
options: <String>['apk'],
149+
output: stdout,
150+
stderr: stderr,
139151
);
140152
});
141153

@@ -156,7 +168,11 @@ class ModuleTest {
156168
section('Clean build');
157169

158170
await inDirectory(projectDir, () async {
159-
await flutter('clean');
171+
await flutter(
172+
'clean',
173+
output: stdout,
174+
stderr: stderr,
175+
);
160176
});
161177

162178
section('Make Android host app editable');
@@ -165,6 +181,8 @@ class ModuleTest {
165181
await flutter(
166182
'make-host-app-editable',
167183
options: <String>['android'],
184+
output: stdout,
185+
stderr: stderr,
168186
);
169187
});
170188

@@ -174,6 +192,8 @@ class ModuleTest {
174192
await flutter(
175193
'build',
176194
options: <String>['apk'],
195+
output: stdout,
196+
stderr: stderr,
177197
);
178198
});
179199

@@ -411,6 +431,12 @@ class ModuleTest {
411431
return TaskResult.failure('Failed to make assets user-readable and writable');
412432
}
413433

434+
section('Check for specific log errors.');
435+
final String finalStderr = stderr.toString();
436+
if (finalStderr.contains("You are applying Flutter's main Gradle plugin imperatively")) {
437+
return TaskResult.failure('Applied the Flutter Gradle Plugin imperatively');
438+
}
439+
414440
return TaskResult.success(null);
415441
} on TaskResult catch (taskResult) {
416442
return taskResult;

dev/devicelab/lib/framework/utils.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,21 @@ Future<int> flutter(String command, {
514514
// DevToolsMemoryTest in perf_tests.dart.
515515
Map<String, String>? environment,
516516
String? workingDirectory,
517+
StringBuffer? output, // if not null, the stdout will be written here
518+
StringBuffer? stderr, // if not null, the stderr will be written here
517519
}) async {
518520
final List<String> args = _flutterCommandArgs(
519521
command, options, driveWithDds: driveWithDds,
520522
);
521-
final int exitCode = await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args,
522-
canFail: canFail, environment: environment, workingDirectory: workingDirectory);
523+
final int exitCode = await exec(
524+
path.join(flutterDirectory.path, 'bin', 'flutter'),
525+
args,
526+
canFail: canFail,
527+
environment: environment,
528+
workingDirectory: workingDirectory,
529+
output: output,
530+
stderr: stderr,
531+
);
523532

524533
if (exitCode != 0 && !canFail) {
525534
await _flutterScreenshot(workingDirectory: workingDirectory);

packages/flutter_tools/gradle/aar_init_script.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ projectsEvaluated {
157157
configureProject(rootProject, rootProject.property("output-dir"))
158158
return
159159
}
160+
if (rootProject.name == "gradle") {
161+
// Skip the "gradle" project, we are looking only for the "android_generated" project.
162+
return
163+
}
160164
// The module project is the `:flutter` subproject.
161165
Project moduleProject = rootProject.subprojects.find { it.name == "flutter" }
162166
assert moduleProject != null
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
// Generated file. Do not edit.
22

3-
buildscript {
4-
ext.kotlin_version = "{{kotlinVersion}}"
5-
repositories {
6-
google()
7-
mavenCentral()
8-
}
9-
10-
dependencies {
11-
classpath("com.android.tools.build:gradle:{{agpVersionForModule}}")
12-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
13-
}
14-
}
15-
163
allprojects {
174
repositories {
185
google()
@@ -31,7 +18,3 @@ android {
3118
targetSdk = {{targetSdkVersion}}
3219
}
3320
}
34-
35-
dependencies {
36-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
37-
}

packages/flutter_tools/templates/module/android/host_app_ephemeral/settings.gradle.copy.tmpl

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generated file. Do not edit.
2+
3+
pluginManagement {
4+
def flutterSdkPath = {
5+
def properties = new Properties()
6+
file("local.properties").withInputStream { properties.load(it) }
7+
def flutterSdkPath = properties.getProperty("flutter.sdk")
8+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
9+
return flutterSdkPath
10+
}()
11+
12+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
13+
14+
repositories {
15+
google()
16+
mavenCentral()
17+
gradlePluginPortal()
18+
}
19+
}
20+
21+
plugins {
22+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
23+
id "com.android.library" version "{{agpVersionForModule}}" apply false
24+
id "org.jetbrains.kotlin.android" version "{{kotlinVersion}}" apply false
25+
}
26+
27+
include ':app'
28+
29+
rootProject.name = 'android_generated'
30+
setBinding(new Binding([gradle: this]))
31+
evaluate(new File(settingsDir, 'include_flutter.groovy'))

packages/flutter_tools/templates/module/android/library_new_embedding/Flutter.tmpl/build.gradle.tmpl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Generated file. Do not edit.
22

3+
plugins {
4+
id "com.android.library"
5+
id "dev.flutter.flutter-gradle-plugin"
6+
}
7+
38
def localProperties = new Properties()
49
def localPropertiesFile = new File(buildscript.sourceFile.parentFile.parentFile, "local.properties")
510
if (localPropertiesFile.exists()) {
@@ -8,11 +13,6 @@ if (localPropertiesFile.exists()) {
813
}
914
}
1015

11-
def flutterRoot = localProperties.getProperty("flutter.sdk")
12-
if (flutterRoot == null) {
13-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
14-
}
15-
1616
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
1717
if (flutterVersionCode == null) {
1818
flutterVersionCode = "1"
@@ -23,9 +23,6 @@ if (flutterVersionName == null) {
2323
flutterVersionName = "1.0"
2424
}
2525

26-
apply plugin: "com.android.library"
27-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
28-
2926
group = "{{androidIdentifier}}"
3027
version = "1.0"
3128

packages/flutter_tools/templates/module/android/library_new_embedding/include_flutter.groovy.copy.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
2626
def flutterSdkPath = properties.getProperty("flutter.sdk")
2727
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
2828
gradle.apply from: "$flutterSdkPath/packages/flutter_tools/gradle/module_plugin_loader.gradle"
29+
30+
gradle.pluginManagement{
31+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
32+
}

packages/flutter_tools/templates/template_manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"templates/module/android/host_app_common/app.tmpl/src/main/res/mipmap-hdpi/ic_launcher.png",
165165
"templates/module/android/host_app_common/app.tmpl/src/main/res/values/styles.xml",
166166
"templates/module/android/host_app_editable/settings.gradle.copy.tmpl",
167-
"templates/module/android/host_app_ephemeral/settings.gradle.copy.tmpl",
167+
"templates/module/android/host_app_ephemeral/settings.gradle.tmpl",
168168
"templates/module/android/library/Flutter.tmpl/build.gradle.tmpl",
169169
"templates/module/android/library/Flutter.tmpl/flutter.iml.copy.tmpl",
170170
"templates/module/android/library/Flutter.tmpl/src/main/AndroidManifest.xml.tmpl",

0 commit comments

Comments
 (0)