Migrate template to Gradle 6.7 and AGP 4.1.0 (#70808)

This commit is contained in:
Emmanuel Garcia
2020-11-20 13:05:35 -08:00
committed by GitHub
parent 1e0628001e
commit 8e73bab94c
170 changed files with 353 additions and 746 deletions

View File

@@ -237,7 +237,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease',
],
@@ -266,7 +265,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Psplit-debug-info=${tempDir.path}',
'-Ptree-shake-icons=true',
'assembleRelease',
@@ -300,7 +298,6 @@ void main() {
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pextra-front-end-options=foo,bar',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease',
],
@@ -356,7 +353,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease',
],
@@ -424,7 +420,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease',
],
@@ -481,7 +476,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'assembleRelease',
],

View File

@@ -207,131 +207,6 @@ void main() {
});
});
testUsingContext('shrinking is enabled by default on release mode', () async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=app'],
);
await expectLater(() async {
await runBuildAppBundleCommand(projectPath);
}, throwsToolExit(message: 'Gradle task bundleRelease failed with exit code 1'));
verify(mockProcessManager.start(
<String>[
gradlew,
'-q',
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'bundleRelease',
],
workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'),
)).called(1);
},
overrides: <Type, Generator>{
AndroidSdk: () => mockAndroidSdk,
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
ProcessManager: () => mockProcessManager,
});
testUsingContext('shrinking is disabled when --no-shrink is passed', () async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--no-pub', '--template=app'],
);
await expectLater(() async {
await runBuildAppBundleCommand(
projectPath,
arguments: <String>['--no-shrink'],
);
}, throwsToolExit(message: 'Gradle task bundleRelease failed with exit code 1'));
verify(mockProcessManager.start(
<String>[
gradlew,
'-q',
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Ptree-shake-icons=true',
'bundleRelease',
],
workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'),
)).called(1);
},
overrides: <Type, Generator>{
AndroidSdk: () => mockAndroidSdk,
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
ProcessManager: () => mockProcessManager,
});
testUsingContext('guides the user when the shrinker fails', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=app']);
when(mockProcessManager.start(
<String>[
gradlew,
'-q',
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'-Ptree-shake-icons=true',
'bundleRelease',
],
workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'),
)).thenAnswer((_) {
const String r8StdoutWarning =
"Execution failed for task ':app:transformClassesAndResourcesWithR8ForStageInternal'.\n"
'> com.android.tools.r8.CompilationFailedException: Compilation failed to complete';
return Future<Process>.value(
createMockProcess(
exitCode: 1,
stdout: r8StdoutWarning,
),
);
});
await expectLater(() async {
await runBuildAppBundleCommand(
projectPath,
);
}, throwsToolExit(message: 'Gradle task bundleRelease failed with exit code 1'));
expect(
testLogger.statusText,
containsIgnoringWhitespace('The shrinker may have failed to optimize the Java bytecode.'),
);
expect(
testLogger.statusText,
containsIgnoringWhitespace('To disable the shrinker, pass the `--no-shrink` flag to this command.'),
);
expect(
testLogger.statusText,
containsIgnoringWhitespace('To learn more, see: https://developer.android.com/studio/build/shrink-code'),
);
verify(mockUsage.sendEvent(
'build',
'appbundle',
label: 'gradle-r8-failure',
parameters: anyNamed('parameters'),
)).called(1);
},
overrides: <Type, Generator>{
AndroidSdk: () => mockAndroidSdk,
FlutterProjectFactory: () => FakeFlutterProjectFactory(tempDir),
ProcessManager: () => mockProcessManager,
Usage: () => mockUsage,
});
testUsingContext("reports when the app isn't using AndroidX", () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=app']);
@@ -349,7 +224,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'assembleRelease',
],
workingDirectory: anyNamed('workingDirectory'),
@@ -405,7 +279,6 @@ void main() {
'-Ptarget-platform=android-arm,android-arm64,android-x64',
'-Ptarget=${globals.fs.path.join(tempDir.path, 'flutter_project', 'lib', 'main.dart')}',
'-Ptrack-widget-creation=true',
'-Pshrink=true',
'assembleRelease',
],
workingDirectory: anyNamed('workingDirectory'),

View File

@@ -2115,7 +2115,7 @@ void main() {
expect(env['flutter'].allows(Version(1, 19, 0)), false);
});
testUsingContext('default app uses Android sdk 29', () async {
testUsingContext('default app uses Android SDK 30', () async {
Cache.flutterRoot = '../..';
when(mockFlutterVersion.frameworkRevision).thenReturn(frameworkRevision);
when(mockFlutterVersion.channel).thenReturn(frameworkChannel);
@@ -2129,8 +2129,8 @@ void main() {
final String buildContent = await globals.fs.file(projectDir.path + '/android/app/build.gradle').readAsString();
expect(buildContent.contains('compileSdkVersion 29'), true);
expect(buildContent.contains('targetSdkVersion 29'), true);
expect(buildContent.contains('compileSdkVersion 30'), true);
expect(buildContent.contains('targetSdkVersion 30'), true);
});
testUsingContext('Linux plugins handle partially camel-case project names correctly', () async {

View File

@@ -441,7 +441,7 @@ Command: /home/android/gradlew assembleRelease
expect(testLogger.statusText,
contains(
'The built failed likely due to AndroidX incompatibilities in a plugin. '
'The build failed likely due to AndroidX incompatibilities in a plugin. '
'The tool is about to try using Jetfier to solve the incompatibility.'
)
);

View File

@@ -316,7 +316,7 @@ void main() {
any,
label: 'gradle-expected-file-not-found',
parameters: const <String, String> {
'cd37': 'androidGradlePluginVersion: 5.6.2, fileExtension: .aab',
'cd37': 'androidGradlePluginVersion: 6.7, fileExtension: .aab',
},
),
).called(1);
@@ -771,6 +771,9 @@ flutter:
expect(getGradleVersionFor('3.4.0'), '5.6.2');
expect(getGradleVersionFor('3.5.0'), '5.6.2');
expect(getGradleVersionFor('4.0.0'), '6.7');
expect(getGradleVersionFor('4.1.0'), '6.7');
});
testWithoutContext('throws on unsupported versions', () {

View File

@@ -70,7 +70,7 @@ void main() {
'distributionPath=wrapper/dists\n'
'zipStoreBase=GRADLE_USER_HOME\n'
'zipStorePath=wrapper/dists\n'
'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n');
'distributionUrl=https\\://services.gradle.org/distributions/gradle-6.7-all.zip\n');
}, overrides: <Type, Generator>{
Cache: () => Cache.test(rootOverride: tempDir, fileSystem: memoryFileSystem),
FileSystem: () => memoryFileSystem,
@@ -111,7 +111,7 @@ void main() {
'distributionPath=wrapper/dists\n'
'zipStoreBase=GRADLE_USER_HOME\n'
'zipStorePath=wrapper/dists\n'
'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n');
'distributionUrl=https\\://services.gradle.org/distributions/gradle-6.7-all.zip\n');
}, overrides: <Type, Generator>{
Cache: () => Cache.test(rootOverride: tempDir, fileSystem: memoryFileSystem),
FileSystem: () => memoryFileSystem,
@@ -119,108 +119,6 @@ void main() {
});
});
group('migrateToR8', () {
MemoryFileSystem memoryFileSystem;
setUp(() {
memoryFileSystem = MemoryFileSystem.test();
});
testUsingContext("throws ToolExit if gradle.properties doesn't exist", () {
final Directory sampleAppAndroid = globals.fs.directory('/sample-app/android');
sampleAppAndroid.createSync(recursive: true);
expect(() {
gradleUtils.migrateToR8(sampleAppAndroid);
}, throwsToolExit(message: 'Expected file ${sampleAppAndroid.path}'));
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('throws ToolExit if it cannot write gradle.properties', () {
final MockDirectory sampleAppAndroid = MockDirectory();
final MockFile gradleProperties = MockFile();
when(gradleProperties.path).thenReturn('foo/gradle.properties');
when(gradleProperties.existsSync()).thenReturn(true);
when(gradleProperties.readAsStringSync()).thenReturn('');
when(gradleProperties.writeAsStringSync('android.enableR8=true\n', mode: FileMode.append))
.thenThrow(const FileSystemException());
when(sampleAppAndroid.childFile('gradle.properties'))
.thenReturn(gradleProperties);
expect(() {
gradleUtils.migrateToR8(sampleAppAndroid);
},
throwsToolExit(message:
'The tool failed to add `android.enableR8=true` to foo/gradle.properties. '
'Please update the file manually and try this command again.'));
});
testUsingContext('does not update gradle.properties if it already uses R8', () {
final Directory sampleAppAndroid = globals.fs.directory('/sample-app/android');
sampleAppAndroid.createSync(recursive: true);
sampleAppAndroid.childFile('gradle.properties')
.writeAsStringSync('android.enableR8=true');
gradleUtils.migrateToR8(sampleAppAndroid);
expect(testLogger.traceText,
contains('gradle.properties already sets `android.enableR8`'));
expect(sampleAppAndroid.childFile('gradle.properties').readAsStringSync(),
equals('android.enableR8=true'));
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('sets android.enableR8=true', () {
final Directory sampleAppAndroid = globals.fs.directory('/sample-app/android');
sampleAppAndroid.createSync(recursive: true);
sampleAppAndroid.childFile('gradle.properties')
.writeAsStringSync('org.gradle.jvmargs=-Xmx1536M\n');
gradleUtils.migrateToR8(sampleAppAndroid);
expect(testLogger.traceText, contains('set `android.enableR8=true` in gradle.properties'));
expect(
sampleAppAndroid.childFile('gradle.properties').readAsStringSync(),
equals(
'org.gradle.jvmargs=-Xmx1536M\n'
'android.enableR8=true\n'
),
);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('appends android.enableR8=true to the new line', () {
final Directory sampleAppAndroid = globals.fs.directory('/sample-app/android');
sampleAppAndroid.createSync(recursive: true);
sampleAppAndroid.childFile('gradle.properties')
.writeAsStringSync('org.gradle.jvmargs=-Xmx1536M');
gradleUtils.migrateToR8(sampleAppAndroid);
expect(testLogger.traceText, contains('set `android.enableR8=true` in gradle.properties'));
expect(
sampleAppAndroid.childFile('gradle.properties').readAsStringSync(),
equals(
'org.gradle.jvmargs=-Xmx1536M\n'
'android.enableR8=true\n'
),
);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any()
});
});
group('GradleUtils.getExecutable', () {
final String gradlewFilename = globals.platform.isWindows ? 'gradlew.bat' : 'gradlew';
@@ -241,7 +139,6 @@ void main() {
androidDirectory.childFile('gradle.properties').createSync();
when(gradleUtils.injectGradleWrapperIfNeeded(any)).thenReturn(null);
when(gradleUtils.migrateToR8(any)).thenReturn(null);
expect(
GradleUtils().getExecutable(FlutterProject.current()),
@@ -273,7 +170,6 @@ void main() {
when(androidProject.hostAppGradleRoot).thenReturn(androidDirectory);
when(gradleUtils.injectGradleWrapperIfNeeded(any)).thenReturn(null);
when(gradleUtils.migrateToR8(any)).thenReturn(null);
GradleUtils().getExecutable(flutterProject);
@@ -304,7 +200,6 @@ void main() {
when(androidProject.hostAppGradleRoot).thenReturn(androidDirectory);
when(gradleUtils.injectGradleWrapperIfNeeded(any)).thenReturn(null);
when(gradleUtils.migrateToR8(any)).thenReturn(null);
GradleUtils().getExecutable(flutterProject);
@@ -335,7 +230,6 @@ void main() {
when(androidProject.hostAppGradleRoot).thenReturn(androidDirectory);
when(gradleUtils.injectGradleWrapperIfNeeded(any)).thenReturn(null);
when(gradleUtils.migrateToR8(any)).thenReturn(null);
GradleUtils().getExecutable(flutterProject);

View File

@@ -919,7 +919,7 @@ String gradleFileWithApplicationId(String id) {
return '''
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId '$id'
@@ -936,7 +936,7 @@ version '1.0-SNAPSHOT'
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
compileSdkVersion 30
}
''';
}

View File

@@ -22,7 +22,6 @@ void main() {
'apk',
'--debug',
'--target-platform', 'android-arm',
'--no-shrink',
'--verbose',
], workingDirectory: woringDirectory);
print(result.stdout);