Flutter build aar (#36732)
`flutter build aar`
This new build command works just like `flutter build apk` or `flutter build appbundle`, but for plugin and module projects.
This PR also refactors how plugins are included in app or module projects. By building the plugins as AARs, the Android Gradle plugin is able to use Jetifier to translate support libraries into AndroidX libraries for all the plugin's native code. Thus, reducing the error rate when using AndroidX in apps.
This change also allows to build modules as AARs, so developers can take these artifacts and distribute them along with the native host app without the need of the Flutter tool. This is a requirement for add to app.
`flutter build aar` generates POM artifacts (XML files) which contain metadata about the native dependencies used by the plugin. This allows Gradle to resolve dependencies at the app level. The result of this new build command is a single build/outputs/repo, the local repository that contains all the generated AARs and POM files.
In a Flutter app project, this local repo is used by the Flutter Gradle plugin to resolve the plugin dependencies. In add to app case, the developer needs to configure the local repo and the dependency manually in `build.gradle`:
repositories {
maven {
url "<path-to-flutter-module>build/host/outputs/repo"
}
}
dependencies {
implementation("<package-name>:flutter_<build-mode>:1.0@aar") {
transitive = true
}
}
This commit is contained in:
@@ -83,6 +83,93 @@ bool hasMultipleOccurrences(String text, Pattern pattern) {
|
||||
return text.indexOf(pattern) != text.lastIndexOf(pattern);
|
||||
}
|
||||
|
||||
/// Utility class to analyze the content inside an APK using dexdump,
|
||||
/// which is provided by the Android SDK.
|
||||
/// https://android.googlesource.com/platform/art/+/master/dexdump/dexdump.cc
|
||||
class ApkExtractor {
|
||||
ApkExtractor(this.apkFile);
|
||||
|
||||
/// The APK.
|
||||
final File apkFile;
|
||||
|
||||
bool _extracted = false;
|
||||
|
||||
Directory _outputDir;
|
||||
|
||||
Future<void> _extractApk() async {
|
||||
if (_extracted) {
|
||||
return;
|
||||
}
|
||||
_outputDir = apkFile.parent.createTempSync('apk');
|
||||
if (Platform.isWindows) {
|
||||
await eval('7za', <String>['x', apkFile.path], workingDirectory: _outputDir.path);
|
||||
} else {
|
||||
await eval('unzip', <String>[apkFile.path], workingDirectory: _outputDir.path);
|
||||
}
|
||||
_extracted = true;
|
||||
}
|
||||
|
||||
/// Returns the full path to the [dexdump] tool.
|
||||
Future<String> _findDexDump() async {
|
||||
final String androidHome = Platform.environment['ANDROID_HOME'] ??
|
||||
Platform.environment['ANDROID_SDK_ROOT'];
|
||||
|
||||
if (androidHome == null || androidHome.isEmpty) {
|
||||
throw Exception('Unset env flag: `ANDROID_HOME` or `ANDROID_SDK_ROOT`.');
|
||||
}
|
||||
String dexdumps;
|
||||
if (Platform.isWindows) {
|
||||
dexdumps = await eval('dir', <String>['/s/b', 'dexdump.exe'],
|
||||
workingDirectory: androidHome);
|
||||
} else {
|
||||
dexdumps = await eval('find', <String>[androidHome, '-name', 'dexdump']);
|
||||
}
|
||||
if (dexdumps.isEmpty) {
|
||||
throw Exception('Couldn\'t find a dexdump executable.');
|
||||
}
|
||||
return dexdumps.split('\n').first;
|
||||
}
|
||||
|
||||
// Removes any temporary directory.
|
||||
void dispose() {
|
||||
if (!_extracted) {
|
||||
return;
|
||||
}
|
||||
rmTree(_outputDir);
|
||||
_extracted = true;
|
||||
}
|
||||
|
||||
/// Returns true if the APK contains a given class.
|
||||
Future<bool> containsClass(String className) async {
|
||||
await _extractApk();
|
||||
|
||||
final String dexDump = await _findDexDump();
|
||||
final String classesDex = path.join(_outputDir.path, 'classes.dex');
|
||||
|
||||
if (!File(classesDex).existsSync()) {
|
||||
throw Exception('Couldn\'t find classes.dex in the APK.');
|
||||
}
|
||||
final String classDescriptors = await eval(dexDump,
|
||||
<String>[classesDex], printStdout: false);
|
||||
|
||||
if (classDescriptors.isEmpty) {
|
||||
throw Exception('No descriptors found in classes.dex.');
|
||||
}
|
||||
return classDescriptors.contains(className.replaceAll('.', '/'));
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that the classes are contained in the APK, throws otherwise.
|
||||
Future<void> checkApkContainsClasses(File apk, List<String> classes) async {
|
||||
final ApkExtractor extractor = ApkExtractor(apk);
|
||||
for (String className in classes) {
|
||||
if (!(await extractor.containsClass(className))) {
|
||||
throw Exception('APK doesn\'t contain class `$className`.');
|
||||
}
|
||||
}
|
||||
extractor.dispose();
|
||||
}
|
||||
|
||||
class FlutterProject {
|
||||
FlutterProject(this.parent, this.name);
|
||||
|
||||
|
||||
@@ -303,7 +303,7 @@ Future<int> exec(
|
||||
|
||||
/// Executes a command and returns its standard output as a String.
|
||||
///
|
||||
/// For logging purposes, the command's output is also printed out.
|
||||
/// For logging purposes, the command's output is also printed out by default.
|
||||
Future<String> eval(
|
||||
String executable,
|
||||
List<String> arguments, {
|
||||
@@ -311,6 +311,8 @@ Future<String> eval(
|
||||
bool canFail = false, // as in, whether failures are ok. False means that they are fatal.
|
||||
String workingDirectory,
|
||||
StringBuffer stderr, // if not null, the stderr will be written here
|
||||
bool printStdout = true,
|
||||
bool printStderr = true,
|
||||
}) async {
|
||||
final Process process = await startProcess(executable, arguments, environment: environment, workingDirectory: workingDirectory);
|
||||
|
||||
@@ -321,14 +323,18 @@ Future<String> eval(
|
||||
.transform<String>(utf8.decoder)
|
||||
.transform<String>(const LineSplitter())
|
||||
.listen((String line) {
|
||||
print('stdout: $line');
|
||||
if (printStdout) {
|
||||
print('stdout: $line');
|
||||
}
|
||||
output.writeln(line);
|
||||
}, onDone: () { stdoutDone.complete(); });
|
||||
process.stderr
|
||||
.transform<String>(utf8.decoder)
|
||||
.transform<String>(const LineSplitter())
|
||||
.listen((String line) {
|
||||
print('stderr: $line');
|
||||
if (printStderr) {
|
||||
print('stderr: $line');
|
||||
}
|
||||
stderr?.writeln(line);
|
||||
}, onDone: () { stderrDone.complete(); });
|
||||
|
||||
@@ -619,3 +625,10 @@ void setLocalEngineOptionIfNecessary(List<String> options, [String flavor]) {
|
||||
options.add('--local-engine=${osNames[deviceOperatingSystem]}_$flavor');
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that the file exists, otherwise throws a [FileSystemException].
|
||||
void checkFileExists(String file) {
|
||||
if (!exists(File(file))) {
|
||||
throw FileSystemException('Expected file to exit.', file);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user