mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

`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 } }
204 lines
5.8 KiB
Dart
204 lines
5.8 KiB
Dart
// Copyright (c) 2018 The Chromium 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:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter_devicelab/framework/framework.dart';
|
|
import 'package:flutter_devicelab/framework/utils.dart';
|
|
import 'package:path/path.dart' as path;
|
|
|
|
final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew';
|
|
final String gradlewExecutable = Platform.isWindows ? gradlew : './$gradlew';
|
|
|
|
/// Tests that the Flutter module project template works and supports
|
|
/// adding Flutter to an existing Android app.
|
|
Future<void> main() async {
|
|
await task(() async {
|
|
|
|
section('Find Java');
|
|
|
|
final String javaHome = await findJavaHome();
|
|
if (javaHome == null)
|
|
return TaskResult.failure('Could not find Java');
|
|
print('\nUsing JAVA_HOME=$javaHome');
|
|
|
|
section('Create Flutter module project');
|
|
|
|
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.');
|
|
final Directory projectDir = Directory(path.join(tempDir.path, 'hello'));
|
|
try {
|
|
await inDirectory(tempDir, () async {
|
|
await flutter(
|
|
'create',
|
|
options: <String>['--org', 'io.flutter.devicelab', '--template=module', 'hello'],
|
|
);
|
|
});
|
|
|
|
section('Add plugins');
|
|
|
|
final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
|
|
String content = await pubspec.readAsString();
|
|
content = content.replaceFirst(
|
|
'\ndependencies:\n',
|
|
'\ndependencies:\n device_info:\n package_info:\n',
|
|
);
|
|
await pubspec.writeAsString(content, flush: true);
|
|
await inDirectory(projectDir, () async {
|
|
await flutter(
|
|
'packages',
|
|
options: <String>['get'],
|
|
);
|
|
});
|
|
|
|
section('Build Flutter module library archive');
|
|
|
|
await inDirectory(Directory(path.join(projectDir.path, '.android')), () async {
|
|
await exec(
|
|
gradlewExecutable,
|
|
<String>['flutter:assembleDebug'],
|
|
environment: <String, String>{ 'JAVA_HOME': javaHome },
|
|
);
|
|
});
|
|
|
|
final bool aarBuilt = exists(File(path.join(
|
|
projectDir.path,
|
|
'.android',
|
|
'Flutter',
|
|
'build',
|
|
'outputs',
|
|
'aar',
|
|
'flutter-debug.aar',
|
|
)));
|
|
|
|
if (!aarBuilt) {
|
|
return TaskResult.failure('Failed to build .aar');
|
|
}
|
|
|
|
section('Build ephemeral host app');
|
|
|
|
await inDirectory(projectDir, () async {
|
|
await flutter(
|
|
'build',
|
|
options: <String>['apk'],
|
|
);
|
|
});
|
|
|
|
final bool ephemeralHostApkBuilt = exists(File(path.join(
|
|
projectDir.path,
|
|
'build',
|
|
'host',
|
|
'outputs',
|
|
'apk',
|
|
'release',
|
|
'app-release.apk',
|
|
)));
|
|
|
|
if (!ephemeralHostApkBuilt) {
|
|
return TaskResult.failure('Failed to build ephemeral host .apk');
|
|
}
|
|
|
|
section('Clean build');
|
|
|
|
await inDirectory(projectDir, () async {
|
|
await flutter('clean');
|
|
});
|
|
|
|
section('Make Android host app editable');
|
|
|
|
await inDirectory(projectDir, () async {
|
|
await flutter(
|
|
'make-host-app-editable',
|
|
options: <String>['android'],
|
|
);
|
|
});
|
|
|
|
section('Build editable host app');
|
|
|
|
await inDirectory(projectDir, () async {
|
|
await flutter(
|
|
'build',
|
|
options: <String>['apk'],
|
|
);
|
|
});
|
|
|
|
final bool editableHostApkBuilt = exists(File(path.join(
|
|
projectDir.path,
|
|
'build',
|
|
'host',
|
|
'outputs',
|
|
'apk',
|
|
'release',
|
|
'app-release.apk',
|
|
)));
|
|
|
|
if (!editableHostApkBuilt) {
|
|
return TaskResult.failure('Failed to build editable host .apk');
|
|
}
|
|
|
|
section('Add to existing Android app');
|
|
|
|
final Directory hostApp = Directory(path.join(tempDir.path, 'hello_host_app'));
|
|
mkdir(hostApp);
|
|
recursiveCopy(
|
|
Directory(path.join(flutterDirectory.path, 'dev', 'integration_tests', 'android_host_app')),
|
|
hostApp,
|
|
);
|
|
copy(
|
|
File(path.join(projectDir.path, '.android', gradlew)),
|
|
hostApp,
|
|
);
|
|
copy(
|
|
File(path.join(projectDir.path, '.android', 'gradle', 'wrapper', 'gradle-wrapper.jar')),
|
|
Directory(path.join(hostApp.path, 'gradle', 'wrapper')),
|
|
);
|
|
|
|
final File analyticsOutputFile = File(path.join(tempDir.path, 'analytics.log'));
|
|
|
|
await inDirectory(hostApp, () async {
|
|
if (!Platform.isWindows) {
|
|
await exec('chmod', <String>['+x', 'gradlew']);
|
|
}
|
|
await exec(gradlewExecutable,
|
|
<String>['app:assembleDebug'],
|
|
environment: <String, String>{
|
|
'JAVA_HOME': javaHome,
|
|
'FLUTTER_ANALYTICS_LOG_FILE': analyticsOutputFile.path,
|
|
},
|
|
);
|
|
});
|
|
|
|
final bool existingAppBuilt = exists(File(path.join(
|
|
hostApp.path,
|
|
'app',
|
|
'build',
|
|
'outputs',
|
|
'apk',
|
|
'debug',
|
|
'app-debug.apk',
|
|
)));
|
|
if (!existingAppBuilt) {
|
|
return TaskResult.failure('Failed to build existing app .apk');
|
|
}
|
|
|
|
final String analyticsOutput = analyticsOutputFile.readAsStringSync();
|
|
if (!analyticsOutput.contains('cd24: android-arm64')
|
|
|| !analyticsOutput.contains('cd25: true')
|
|
|| !analyticsOutput.contains('viewName: build/bundle')) {
|
|
return TaskResult.failure(
|
|
'Building outer app produced the following analytics: "$analyticsOutput"'
|
|
'but not the expected strings: "cd24: android-arm64", "cd25: true" and '
|
|
'"viewName: build/bundle"'
|
|
);
|
|
}
|
|
|
|
return TaskResult.success(null);
|
|
} catch (e) {
|
|
return TaskResult.failure(e.toString());
|
|
} finally {
|
|
rmTree(tempDir);
|
|
}
|
|
});
|
|
}
|