Unverified Commit 7851a751 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Fix NPE in flutter tools (#34725)

parent 0d4f4bdd
......@@ -509,7 +509,7 @@ Future<void> _buildGradleProjectV2(
flutterUsage.sendTiming('build', 'gradle-v2', Duration(milliseconds: sw.elapsedMilliseconds));
if (!isBuildingBundle) {
final Iterable<File> apkFiles = _findApkFiles(project, androidBuildInfo);
final Iterable<File> apkFiles = findApkFiles(project, androidBuildInfo);
if (apkFiles.isEmpty)
throwToolExit('Gradle build failed to produce an Android package.');
// Copy the first APK to app.apk, so `flutter run`, `flutter install`, etc. can find it.
......@@ -546,22 +546,23 @@ Future<void> _buildGradleProjectV2(
}
}
Iterable<File> _findApkFiles(GradleProject project, AndroidBuildInfo androidBuildInfo) {
@visibleForTesting
Iterable<File> findApkFiles(GradleProject project, AndroidBuildInfo androidBuildInfo) {
final Iterable<String> apkFileNames = project.apkFilesFor(androidBuildInfo);
if (apkFileNames.isEmpty)
return const <File>[];
return apkFileNames.map<File>((String apkFileName) {
return apkFileNames.expand<File>((String apkFileName) {
File apkFile = project.apkDirectory.childFile(apkFileName);
if (apkFile.existsSync())
return apkFile;
return <File>[apkFile];
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
final String modeName = camelCase(buildInfo.modeName);
apkFile = project.apkDirectory
.childDirectory(modeName)
.childFile(apkFileName);
if (apkFile.existsSync())
return apkFile;
return <File>[apkFile];
if (buildInfo.flavor != null) {
// Android Studio Gradle plugin v3 adds flavor to path.
apkFile = project.apkDirectory
......@@ -569,9 +570,9 @@ Iterable<File> _findApkFiles(GradleProject project, AndroidBuildInfo androidBuil
.childDirectory(modeName)
.childFile(apkFileName);
if (apkFile.existsSync())
return apkFile;
return <File>[apkFile];
}
return null;
return const <File>[];
});
}
......
......@@ -41,6 +41,18 @@ void main() {
expect(shouldBeToolExit, isToolExit);
});
// Regression test for https://github.com/flutter/flutter/issues/34700
testUsingContext('Does not return nulls in apk list', () {
final GradleProject gradleProject = MockGradleProject();
const AndroidBuildInfo buildInfo = AndroidBuildInfo(BuildInfo.debug);
when(gradleProject.apkFilesFor(buildInfo)).thenReturn(<String>['not_real']);
when(gradleProject.apkDirectory).thenReturn(fs.currentDirectory);
expect(findApkFiles(gradleProject, buildInfo), <File>[]);
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(),
});
test('androidXFailureRegex should match lines with likely AndroidX errors', () {
final List<String> nonMatchingLines = <String>[
':app:preBuild UP-TO-DATE',
......@@ -537,3 +549,4 @@ Platform fakePlatform(String name) {
class MockLocalEngineArtifacts extends Mock implements LocalEngineArtifacts {}
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}
class MockGradleProject extends Mock implements GradleProject {}
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