Unverified Commit f172c827 authored by Emmanuel Garcia's avatar Emmanuel Garcia Committed by GitHub

Ensure apps can build while using AGP 3.3.0 (#71964)

parent 8cd03a2e
...@@ -15,7 +15,6 @@ final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradle ...@@ -15,7 +15,6 @@ final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradle
/// Tests that a plugin example app can be built using the current Flutter Gradle plugin. /// Tests that a plugin example app can be built using the current Flutter Gradle plugin.
Future<void> main() async { Future<void> main() async {
await task(() async { await task(() async {
section('Find Java'); section('Find Java');
final String javaHome = await findJavaHome(); final String javaHome = await findJavaHome();
...@@ -32,22 +31,47 @@ Future<void> main() async { ...@@ -32,22 +31,47 @@ Future<void> main() async {
options: <String>['--android', '--no-ios'], options: <String>['--android', '--no-ios'],
); );
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_plugin_test.'); final Directory tempDir =
final Directory projectDir = Directory(path.join(tempDir.path, 'plugin_test')); Directory.systemTemp.createTempSync('flutter_plugin_test.');
final Directory projectDir =
Directory(path.join(tempDir.path, 'plugin_test'));
try { try {
await inDirectory(tempDir, () async { await inDirectory(tempDir, () async {
await flutter( await flutter(
'create', 'create',
options: <String>['--template=plugin', '--platforms=android', 'plugin_test'], options: <String>[
'--template=plugin',
'--platforms=android',
'plugin_test',
],
); );
}); });
final Directory exampleAppDir = Directory(path.join(projectDir.path, 'example')); final Directory exampleAppDir =
Directory(path.join(projectDir.path, 'example'));
if (!exists(exampleAppDir)) { if (!exists(exampleAppDir)) {
return TaskResult.failure('Example app directory doesn\'t exist'); return TaskResult.failure('Example app directory doesn\'t exist');
} }
section('Run flutter build apk'); final File buildGradleFile =
File(path.join(exampleAppDir.path, 'android', 'build.gradle'));
if (!exists(buildGradleFile)) {
return TaskResult.failure('$buildGradleFile doesn\'t exist');
}
final String buildGradle = buildGradleFile.readAsStringSync();
final RegExp androidPluginRegExp =
RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
section('Use AGP 4.1.0');
String newBuildGradle = buildGradle.replaceAll(
androidPluginRegExp, 'com.android.tools.build:gradle:4.1.0');
print(newBuildGradle);
buildGradleFile.writeAsString(newBuildGradle);
section('Run flutter build apk using AGP 4.1.0');
await inDirectory(exampleAppDir, () async { await inDirectory(exampleAppDir, () async {
await flutter( await flutter(
...@@ -72,6 +96,55 @@ Future<void> main() async { ...@@ -72,6 +96,55 @@ Future<void> main() async {
return TaskResult.failure('Failed to build app-release.apk'); return TaskResult.failure('Failed to build app-release.apk');
} }
section('Clean');
await inDirectory(exampleAppDir, () async {
await flutter('clean');
});
section('Remove Gradle wrapper');
Directory(path.join(exampleAppDir.path, 'android', 'gradle', 'wrapper'))
.deleteSync(recursive: true);
section('Use AGP 3.3.0');
newBuildGradle = buildGradle.replaceAll(
androidPluginRegExp, 'com.android.tools.build:gradle:3.3.0');
print(newBuildGradle);
buildGradleFile.writeAsString(newBuildGradle);
section('Enable R8 in gradle.properties');
final File gradleProperties =
File(path.join(exampleAppDir.path, 'android', 'gradle.properties'));
if (!exists(gradleProperties)) {
return TaskResult.failure('$gradleProperties doesn\'t exist');
}
gradleProperties.writeAsString('''
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true''');
section('Run flutter build apk using AGP 3.3.0');
await inDirectory(exampleAppDir, () async {
await flutter(
'build',
options: <String>[
'apk',
'--target-platform=android-arm',
],
);
});
if (!exists(File(exampleApk))) {
return TaskResult.failure('Failed to build app-release.apk');
}
return TaskResult.success(null); return TaskResult.success(null);
} on TaskResult catch (taskResult) { } on TaskResult catch (taskResult) {
return taskResult; return taskResult;
......
...@@ -20,6 +20,7 @@ import org.gradle.api.tasks.InputFiles ...@@ -20,6 +20,7 @@ import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.bundling.Jar
import org.gradle.util.VersionNumber
buildscript { buildscript {
repositories { repositories {
...@@ -304,7 +305,7 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -304,7 +305,7 @@ class FlutterPlugin implements Plugin<Project> {
project.dependencies { project.dependencies {
implementation pluginProject implementation pluginProject
} }
Closure addEmbeddingCompileOnlyDependency = { buildType -> Closure addEmbeddingDependencyToPlugin = { buildType ->
String flutterBuildMode = buildModeFor(buildType) String flutterBuildMode = buildModeFor(buildType)
// In AGP 3.5, the embedding must be added as an API implementation, // In AGP 3.5, the embedding must be added as an API implementation,
// so java8 features are desugared against the runtime classpath. // so java8 features are desugared against the runtime classpath.
...@@ -317,17 +318,39 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -317,17 +318,39 @@ class FlutterPlugin implements Plugin<Project> {
pluginProject.android.buildTypes { pluginProject.android.buildTypes {
"${buildType.name}" {} "${buildType.name}" {}
} }
pluginProject.dependencies.add( // The embedding is a compileOnly dependency of a Flutter plugin,
"${buildType.name}CompileOnly", // however prior to Gradle 6.0.0, it must be an API dependency.
"io.flutter:flutter_embedding_$flutterBuildMode:$engineVersion") //
// Not doing so, causes transitive dependency resolution conflicts.
// That is, the embedding dependencies resolved in the plugin are
// different than the ones resolved in the app.
if (isGradleVersionGraterOrEqualThan('6.0.0')) {
addCompileOnlyDependency(
pluginProject,
buildType.name,
"io.flutter:flutter_embedding_$flutterBuildMode:$engineVersion"
)
} else {
addApiDependencies(
pluginProject,
buildType.name,
"io.flutter:flutter_embedding_$flutterBuildMode:$engineVersion"
)
}
} }
// Wait until the Android plugin loaded. // Wait until the Android plugin loaded.
pluginProject.afterEvaluate { pluginProject.afterEvaluate {
project.android.buildTypes.each addEmbeddingCompileOnlyDependency project.android.buildTypes.each addEmbeddingDependencyToPlugin
project.android.buildTypes.whenObjectAdded addEmbeddingCompileOnlyDependency project.android.buildTypes.whenObjectAdded addEmbeddingDependencyToPlugin
} }
} }
// Returns `true` if the current Gradle version is greater or equal to the given version.
private isGradleVersionGraterOrEqualThan(String version) {
return VersionNumber.parse(project.gradle.gradleVersion)
.compareTo(VersionNumber.parse(version)) >= 0
}
// Returns `true` if the given path contains an `android/build.gradle` file. // Returns `true` if the given path contains an `android/build.gradle` file.
// //
// TODO(egarciad): Fix https://github.com/flutter/flutter/issues/39657. // TODO(egarciad): Fix https://github.com/flutter/flutter/issues/39657.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment