Fix plugin java class desugar (#73758)

This commit is contained in:
Emmanuel Garcia
2021-01-13 14:09:04 -08:00
committed by GitHub
parent 49667ba2f5
commit da90156f8c
4 changed files with 117 additions and 63 deletions

View File

@@ -0,0 +1,44 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:flutter_devicelab/framework/apk_utils.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/task_result.dart';
import 'package:flutter_devicelab/framework/utils.dart';
Future<void> main() async {
await task(() async {
try {
await runProjectTest((FlutterProject flutterProject) async {
section('APK contains plugin classes');
flutterProject.addPlugin('google_maps_flutter', value: '^1.0.10');
await inDirectory(flutterProject.rootPath, () async {
await flutter('build', options: <String>[
'apk',
'--debug',
'--target-platform=android-arm',
]);
final File apk = File('${flutterProject.rootPath}/build/app/outputs/flutter-apk/app-debug.apk');
if (!apk.existsSync()) {
throw TaskResult.failure('Expected ${apk.path} to exist, but it doesn\'t');
}
// https://github.com/flutter/flutter/issues/72185
await checkApkContainsMethods(apk, <String>[
'io.flutter.plugins.googlemaps.GoogleMapController void onFlutterViewAttached(android.view.View)',
'io.flutter.plugins.googlemaps.GoogleMapController void onFlutterViewDetached()',
]);
});
});
return TaskResult.success(null);
} on TaskResult catch (taskResult) {
return taskResult;
} catch (e) {
return TaskResult.failure(e.toString());
}
});
}

View File

@@ -115,12 +115,14 @@ String get _androidHome {
/// Executes an APK analyzer subcommand.
Future<String> _evalApkAnalyzer(
List<String> args, {
bool printStdout = true,
bool printStdout = false,
String workingDirectory,
}) async {
final String javaHome = await findJavaHome();
final String apkAnalyzer = path
if (javaHome == null || javaHome.isEmpty) {
throw Exception('No JAVA_HOME set.');
}
final String apkAnalyzer = path
.join(_androidHome, 'cmdline-tools', 'latest', 'bin', Platform.isWindows ? 'apkanalyzer.bat' : 'apkanalyzer');
if (canRun(apkAnalyzer)) {
return eval(
@@ -165,6 +167,7 @@ class ApkExtractor {
bool _extracted = false;
Set<String> _classes = const <String>{};
Set<String> _methods = const <String>{};
Future<void> _extractDex() async {
if (_extracted) {
@@ -177,22 +180,17 @@ class ApkExtractor {
apkFile.path,
],
);
final List<String> lines = packages.split('\n');
_classes = Set<String>.from(
packages
.split('\n')
.where((String line) => line.startsWith('C'))
.map<String>((String line) => line.split('\t').last),
lines.where((String line) => line.startsWith('C'))
.map<String>((String line) => line.split('\t').last),
);
assert(_classes.isNotEmpty);
_extracted = true;
}
// Removes any temporary directory.
void dispose() {
if (!_extracted) {
return;
}
_classes = const <String>{};
_methods = Set<String>.from(
lines.where((String line) => line.startsWith('M'))
.map<String>((String line) => line.split('\t').last)
);
assert(_methods.isNotEmpty);
_extracted = true;
}
@@ -201,6 +199,13 @@ class ApkExtractor {
await _extractDex();
return _classes.contains(className);
}
/// Returns true if the APK contains a given method.
/// For example: io.flutter.plugins.googlemaps.GoogleMapController void onFlutterViewAttached(android.view.View)
Future<bool> containsMethod(String methodName) async {
await _extractDex();
return _methods.contains(methodName);
}
}
/// Gets the content of the `AndroidManifest.xml`.
@@ -223,7 +228,16 @@ Future<void> checkApkContainsClasses(File apk, List<String> classes) async {
throw Exception("APK doesn't contain class `$className`.");
}
}
extractor.dispose();
}
/// Checks that the methods are defined in the APK, throws otherwise.
Future<void> checkApkContainsMethods(File apk, List<String> methods) async {
final ApkExtractor extractor = ApkExtractor(apk);
for (final String method in methods) {
if (!(await extractor.containsMethod(method))) {
throw Exception("APK doesn't contain method `$method`.");
}
}
}
class FlutterProject {
@@ -288,7 +302,7 @@ subprojects {
String content = await pubspec.readAsString();
content = content.replaceFirst(
'${platformLineSep}dependencies:$platformLineSep',
'${platformLineSep}dependencies:$platformLineSep $plugin:$value$platformLineSep',
'${platformLineSep}dependencies:$platformLineSep $plugin: $value$platformLineSep',
);
await pubspec.writeAsString(content, flush: true);
}

View File

@@ -456,6 +456,8 @@ List<String> flutterCommandArgs(String command, List<String> options) {
];
}
/// Runs the flutter `command`, and returns the exit code.
/// If `canFail` is `false`, the future completes with an error.
Future<int> flutter(String command, {
List<String> options = const <String>[],
bool canFail = false, // as in, whether failures are ok. False means that they are fatal.