Unverified Commit 8627ff43 authored by Emmanuel Garcia's avatar Emmanuel Garcia Committed by GitHub

Allow multi-abi shared libraries in APKs and App bundles (#34123)

* Gradle generates ELF shared libraries instead of AOT snapshots.
* `flutter build apk/appbundle` supports multiple `--target-platform` and defaults to `android-arm` and `android-arm64`.
* `flutter build apk` now has a flag called `--split-per-abi`.
parent 72aff8ef
...@@ -351,6 +351,7 @@ class CompileTest { ...@@ -351,6 +351,7 @@ class CompileTest {
break; break;
case DeviceOperatingSystem.android: case DeviceOperatingSystem.android:
options.insert(0, 'apk'); options.insert(0, 'apk');
options.add('--target-platform=android-arm');
watch.start(); watch.start();
await flutter('build', options: options); await flutter('build', options: options);
watch.stop(); watch.stop();
...@@ -387,6 +388,7 @@ class CompileTest { ...@@ -387,6 +388,7 @@ class CompileTest {
break; break;
case DeviceOperatingSystem.android: case DeviceOperatingSystem.android:
options.insert(0, 'apk'); options.insert(0, 'apk');
options.add('--target-platform=android-arm');
break; break;
} }
watch.start(); watch.start();
...@@ -445,23 +447,14 @@ class CompileTest { ...@@ -445,23 +447,14 @@ class CompileTest {
} }
final _UnzipListEntry libflutter = fileToMetadata['lib/armeabi-v7a/libflutter.so']; final _UnzipListEntry libflutter = fileToMetadata['lib/armeabi-v7a/libflutter.so'];
final _UnzipListEntry isolateSnapshotData = fileToMetadata['assets/isolate_snapshot_data']; final _UnzipListEntry libapp = fileToMetadata['lib/armeabi-v7a/libapp.so'];
final _UnzipListEntry isolateSnapshotInstr = fileToMetadata['assets/isolate_snapshot_instr'];
final _UnzipListEntry vmSnapshotData = fileToMetadata['assets/vm_snapshot_data'];
final _UnzipListEntry vmSnapshotInstr = fileToMetadata['assets/vm_snapshot_instr'];
final _UnzipListEntry license = fileToMetadata['assets/flutter_assets/LICENSE']; final _UnzipListEntry license = fileToMetadata['assets/flutter_assets/LICENSE'];
return <String, dynamic>{ return <String, dynamic>{
'libflutter_uncompressed_bytes': libflutter.uncompressedSize, 'libflutter_uncompressed_bytes': libflutter.uncompressedSize,
'libflutter_compressed_bytes': libflutter.compressedSize, 'libflutter_compressed_bytes': libflutter.compressedSize,
'snapshot_uncompressed_bytes': isolateSnapshotData.uncompressedSize + 'libapp_uncompressed_bytes': libapp.uncompressedSize,
isolateSnapshotInstr.uncompressedSize + 'libapp_compressed_bytes': libapp.compressedSize,
vmSnapshotData.uncompressedSize +
vmSnapshotInstr.uncompressedSize,
'snapshot_compressed_bytes': isolateSnapshotData.compressedSize +
isolateSnapshotInstr.compressedSize +
vmSnapshotData.compressedSize +
vmSnapshotInstr.compressedSize,
'license_uncompressed_bytes': license.uncompressedSize, 'license_uncompressed_bytes': license.uncompressedSize,
'license_compressed_bytes': license.compressedSize, 'license_compressed_bytes': license.compressedSize,
}; };
......
...@@ -5,7 +5,7 @@ homepage: https://github.com/flutter/flutter ...@@ -5,7 +5,7 @@ homepage: https://github.com/flutter/flutter
environment: environment:
# The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite. # The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite.
sdk: ">=2.2.2 <3.0.0" sdk: ">=2.3.0 <3.0.0"
dependencies: dependencies:
args: 1.5.2 args: 1.5.2
......
...@@ -397,9 +397,24 @@ class AndroidDevice extends Device { ...@@ -397,9 +397,24 @@ class AndroidDevice extends Device {
return LaunchResult.failed(); return LaunchResult.failed();
} }
BuildInfo buildInfo = debuggingOptions.buildInfo; AndroidArch androidArch;
if (buildInfo.targetPlatform == null && devicePlatform == TargetPlatform.android_arm64) switch (devicePlatform) {
buildInfo = buildInfo.withTargetPlatform(TargetPlatform.android_arm64); case TargetPlatform.android_arm:
androidArch = AndroidArch.armeabi_v7a;
break;
case TargetPlatform.android_arm64:
androidArch = AndroidArch.arm64_v8a;
break;
case TargetPlatform.android_x64:
androidArch = AndroidArch.x86_64;
break;
case TargetPlatform.android_x86:
androidArch = AndroidArch.x86;
break;
default:
printError('Android platforms are only supported.');
return LaunchResult.failed();
}
if (!prebuiltApplication || androidSdk.licensesAvailable && androidSdk.latestVersion == null) { if (!prebuiltApplication || androidSdk.licensesAvailable && androidSdk.latestVersion == null) {
printTrace('Building APK'); printTrace('Building APK');
...@@ -407,7 +422,9 @@ class AndroidDevice extends Device { ...@@ -407,7 +422,9 @@ class AndroidDevice extends Device {
await buildApk( await buildApk(
project: project, project: project,
target: mainPath, target: mainPath,
buildInfo: buildInfo, androidBuildInfo: AndroidBuildInfo(debuggingOptions.buildInfo,
targetArchs: <AndroidArch>[androidArch]
),
); );
// Package has been built, so we can get the updated application ID and // Package has been built, so we can get the updated application ID and
// activity name from the .apk. // activity name from the .apk.
......
...@@ -16,7 +16,7 @@ import 'gradle.dart'; ...@@ -16,7 +16,7 @@ import 'gradle.dart';
Future<void> buildApk({ Future<void> buildApk({
@required FlutterProject project, @required FlutterProject project,
@required String target, @required String target,
BuildInfo buildInfo = BuildInfo.debug, @required AndroidBuildInfo androidBuildInfo,
}) async { }) async {
if (!project.android.isUsingGradle) { if (!project.android.isUsingGradle) {
throwToolExit( throwToolExit(
...@@ -33,7 +33,7 @@ Future<void> buildApk({ ...@@ -33,7 +33,7 @@ Future<void> buildApk({
await buildGradleProject( await buildGradleProject(
project: project, project: project,
buildInfo: buildInfo, androidBuildInfo: androidBuildInfo,
target: target, target: target,
isBuildingBundle: false, isBuildingBundle: false,
); );
......
...@@ -17,7 +17,7 @@ import 'gradle.dart'; ...@@ -17,7 +17,7 @@ import 'gradle.dart';
Future<void> buildAppBundle({ Future<void> buildAppBundle({
@required FlutterProject project, @required FlutterProject project,
@required String target, @required String target,
BuildInfo buildInfo = BuildInfo.debug, @required AndroidBuildInfo androidBuildInfo,
}) async { }) async {
if (!project.android.isUsingGradle) { if (!project.android.isUsingGradle) {
throwToolExit( throwToolExit(
...@@ -42,7 +42,7 @@ Future<void> buildAppBundle({ ...@@ -42,7 +42,7 @@ Future<void> buildAppBundle({
return buildGradleProject( return buildGradleProject(
project: project, project: project,
buildInfo: buildInfo, androidBuildInfo: androidBuildInfo,
target: target, target: target,
isBuildingBundle: true, isBuildingBundle: true,
); );
......
...@@ -15,6 +15,7 @@ import '../base/logger.dart'; ...@@ -15,6 +15,7 @@ import '../base/logger.dart';
import '../base/os.dart'; import '../base/os.dart';
import '../base/platform.dart'; import '../base/platform.dart';
import '../base/process.dart'; import '../base/process.dart';
import '../base/terminal.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../build_info.dart'; import '../build_info.dart';
import '../cache.dart'; import '../cache.dart';
...@@ -318,7 +319,7 @@ void _exitIfNoAndroidSdk() { ...@@ -318,7 +319,7 @@ void _exitIfNoAndroidSdk() {
Future<void> buildGradleProject({ Future<void> buildGradleProject({
@required FlutterProject project, @required FlutterProject project,
@required BuildInfo buildInfo, @required AndroidBuildInfo androidBuildInfo,
@required String target, @required String target,
@required bool isBuildingBundle, @required bool isBuildingBundle,
}) async { }) async {
...@@ -330,7 +331,7 @@ Future<void> buildGradleProject({ ...@@ -330,7 +331,7 @@ Future<void> buildGradleProject({
// and can be overwritten with flutter build command. // and can be overwritten with flutter build command.
// The default Gradle script reads the version name and number // The default Gradle script reads the version name and number
// from the local.properties file. // from the local.properties file.
updateLocalProperties(project: project, buildInfo: buildInfo); updateLocalProperties(project: project, buildInfo: androidBuildInfo.buildInfo);
final String gradle = await _ensureGradle(project); final String gradle = await _ensureGradle(project);
...@@ -342,7 +343,7 @@ Future<void> buildGradleProject({ ...@@ -342,7 +343,7 @@ Future<void> buildGradleProject({
case FlutterPluginVersion.managed: case FlutterPluginVersion.managed:
// Fall through. Managed plugin builds the same way as plugin v2. // Fall through. Managed plugin builds the same way as plugin v2.
case FlutterPluginVersion.v2: case FlutterPluginVersion.v2:
return _buildGradleProjectV2(project, gradle, buildInfo, target, isBuildingBundle); return _buildGradleProjectV2(project, gradle, androidBuildInfo, target, isBuildingBundle);
} }
} }
...@@ -391,11 +392,12 @@ String _calculateSha(File file) { ...@@ -391,11 +392,12 @@ String _calculateSha(File file) {
Future<void> _buildGradleProjectV2( Future<void> _buildGradleProjectV2(
FlutterProject flutterProject, FlutterProject flutterProject,
String gradle, String gradle,
BuildInfo buildInfo, AndroidBuildInfo androidBuildInfo,
String target, String target,
bool isBuildingBundle, bool isBuildingBundle,
) async { ) async {
final GradleProject project = await _gradleProject(); final GradleProject project = await _gradleProject();
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
String assembleTask; String assembleTask;
...@@ -453,12 +455,13 @@ Future<void> _buildGradleProjectV2( ...@@ -453,12 +455,13 @@ Future<void> _buildGradleProjectV2(
command.add('-Pfilesystem-roots=${buildInfo.fileSystemRoots.join('|')}'); command.add('-Pfilesystem-roots=${buildInfo.fileSystemRoots.join('|')}');
if (buildInfo.fileSystemScheme != null) if (buildInfo.fileSystemScheme != null)
command.add('-Pfilesystem-scheme=${buildInfo.fileSystemScheme}'); command.add('-Pfilesystem-scheme=${buildInfo.fileSystemScheme}');
if (buildInfo.buildSharedLibrary) { if (androidBuildInfo.splitPerAbi)
command.add('-Pbuild-shared-library=true'); command.add('-Psplit-per-abi=true');
if (androidBuildInfo.targetArchs.isNotEmpty) {
final String targetPlatforms = androidBuildInfo.targetArchs
.map(getPlatformNameForAndroidArch).join(',');
command.add('-Ptarget-platform=$targetPlatforms');
} }
if (buildInfo.targetPlatform != null)
command.add('-Ptarget-platform=${getNameForTargetPlatform(buildInfo.targetPlatform)}');
command.add(assembleTask); command.add(assembleTask);
bool potentialAndroidXFailure = false; bool potentialAndroidXFailure = false;
final Stopwatch sw = Stopwatch()..start(); final Stopwatch sw = Stopwatch()..start();
...@@ -508,24 +511,27 @@ Future<void> _buildGradleProjectV2( ...@@ -508,24 +511,27 @@ Future<void> _buildGradleProjectV2(
flutterUsage.sendTiming('build', 'gradle-v2', Duration(milliseconds: sw.elapsedMilliseconds)); flutterUsage.sendTiming('build', 'gradle-v2', Duration(milliseconds: sw.elapsedMilliseconds));
if (!isBuildingBundle) { if (!isBuildingBundle) {
final File apkFile = _findApkFile(project, buildInfo); final Iterable<File> apkFiles = _findApkFiles(project, androidBuildInfo);
if (apkFile == null) if (apkFiles.isEmpty)
throwToolExit('Gradle build failed to produce an Android package.'); throwToolExit('Gradle build failed to produce an Android package.');
// Copy the APK to app.apk, so `flutter run`, `flutter install`, etc. can find it. // Copy the first APK to app.apk, so `flutter run`, `flutter install`, etc. can find it.
apkFile.copySync(project.apkDirectory.childFile('app.apk').path); // TODO(blasten): Handle multiple APKs.
apkFiles.first.copySync(project.apkDirectory.childFile('app.apk').path);
printTrace('calculateSha: ${project.apkDirectory}/app.apk'); printTrace('calculateSha: ${project.apkDirectory}/app.apk');
final File apkShaFile = project.apkDirectory.childFile('app.apk.sha1'); final File apkShaFile = project.apkDirectory.childFile('app.apk.sha1');
apkShaFile.writeAsStringSync(_calculateSha(apkFile)); apkShaFile.writeAsStringSync(_calculateSha(apkFiles.first));
String appSize; for (File apkFile in apkFiles) {
if (buildInfo.mode == BuildMode.debug) { String appSize;
appSize = ''; if (buildInfo.mode == BuildMode.debug) {
} else { appSize = '';
appSize = ' (${getSizeAsMB(apkFile.lengthSync())})'; } else {
appSize = ' (${getSizeAsMB(apkFile.lengthSync())})';
}
printStatus('Built ${fs.path.relative(apkFile.path)}$appSize.',
color: TerminalColor.green);
} }
printStatus('Built ${fs.path.relative(apkFile.path)}$appSize.');
} else { } else {
final File bundleFile = _findBundleFile(project, buildInfo); final File bundleFile = _findBundleFile(project, buildInfo);
if (bundleFile == null) if (bundleFile == null)
...@@ -537,28 +543,38 @@ Future<void> _buildGradleProjectV2( ...@@ -537,28 +543,38 @@ Future<void> _buildGradleProjectV2(
} else { } else {
appSize = ' (${getSizeAsMB(bundleFile.lengthSync())})'; appSize = ' (${getSizeAsMB(bundleFile.lengthSync())})';
} }
printStatus('Built ${fs.path.relative(bundleFile.path)}$appSize.'); printStatus('Built ${fs.path.relative(bundleFile.path)}$appSize.',
color: TerminalColor.green);
} }
} }
File _findApkFile(GradleProject project, BuildInfo buildInfo) { Iterable<File> _findApkFiles(GradleProject project, AndroidBuildInfo androidBuildInfo) {
final String apkFileName = project.apkFileFor(buildInfo); final Iterable<String> apkFileNames = project.apkFilesFor(androidBuildInfo);
if (apkFileName == null) if (apkFileNames.isEmpty)
return null; return const <File>[];
File apkFile = fs.file(fs.path.join(project.apkDirectory.path, apkFileName));
if (apkFile.existsSync()) return apkFileNames.map<File>((String apkFileName) {
return apkFile; File apkFile = project.apkDirectory.childFile(apkFileName);
final String modeName = camelCase(buildInfo.modeName);
apkFile = fs.file(fs.path.join(project.apkDirectory.path, modeName, apkFileName));
if (apkFile.existsSync())
return apkFile;
if (buildInfo.flavor != null) {
// Android Studio Gradle plugin v3 adds flavor to path.
apkFile = fs.file(fs.path.join(project.apkDirectory.path, buildInfo.flavor, modeName, apkFileName));
if (apkFile.existsSync()) if (apkFile.existsSync())
return apkFile; return apkFile;
} final BuildInfo buildInfo = androidBuildInfo.buildInfo;
return null; final String modeName = camelCase(buildInfo.modeName);
apkFile = project.apkDirectory
.childDirectory(modeName)
.childFile(apkFileName);
if (apkFile.existsSync())
return apkFile;
if (buildInfo.flavor != null) {
// Android Studio Gradle plugin v3 adds flavor to path.
apkFile = project.apkDirectory
.childDirectory(buildInfo.flavor)
.childDirectory(modeName)
.childFile(apkFileName);
if (apkFile.existsSync())
return apkFile;
}
return null;
});
} }
File _findBundleFile(GradleProject project, BuildInfo buildInfo) { File _findBundleFile(GradleProject project, BuildInfo buildInfo) {
...@@ -567,12 +583,12 @@ File _findBundleFile(GradleProject project, BuildInfo buildInfo) { ...@@ -567,12 +583,12 @@ File _findBundleFile(GradleProject project, BuildInfo buildInfo) {
if (bundleFileName == null) if (bundleFileName == null)
return null; return null;
final String modeName = camelCase(buildInfo.modeName); final String modeName = camelCase(buildInfo.modeName);
File bundleFile = fs.file(fs.path.join(project.bundleDirectory.path, modeName, bundleFileName)); File bundleFile = project.bundleDirectory.childDirectory(modeName).childFile(bundleFileName);
if (bundleFile.existsSync()) if (bundleFile.existsSync())
return bundleFile; return bundleFile;
if (buildInfo.flavor != null) { if (buildInfo.flavor != null) {
// Android Studio Gradle plugin v3 adds the flavor to the path. For the bundle the folder name is the flavor plus the mode name. // Android Studio Gradle plugin v3 adds the flavor to the path. For the bundle the folder name is the flavor plus the mode name.
bundleFile = fs.file(fs.path.join(project.bundleDirectory.path, buildInfo.flavor + modeName, bundleFileName)); bundleFile = project.bundleDirectory.childDirectory(buildInfo.flavor + modeName).childFile(bundleFileName);
if (bundleFile.existsSync()) if (bundleFile.existsSync())
return bundleFile; return bundleFile;
} }
...@@ -661,13 +677,20 @@ class GradleProject { ...@@ -661,13 +677,20 @@ class GradleProject {
return 'assemble${toTitleCase(productFlavor)}${toTitleCase(buildType)}'; return 'assemble${toTitleCase(productFlavor)}${toTitleCase(buildType)}';
} }
String apkFileFor(BuildInfo buildInfo) { Iterable<String> apkFilesFor(AndroidBuildInfo androidBuildInfo) {
final String buildType = _buildTypeFor(buildInfo); final String buildType = _buildTypeFor(androidBuildInfo.buildInfo);
final String productFlavor = _productFlavorFor(buildInfo); final String productFlavor = _productFlavorFor(androidBuildInfo.buildInfo);
if (buildType == null || productFlavor == null) if (buildType == null || productFlavor == null)
return null; return const <String>[];
final String flavorString = productFlavor.isEmpty ? '' : '-' + productFlavor; final String flavorString = productFlavor.isEmpty ? '' : '-' + productFlavor;
return 'app$flavorString-$buildType.apk'; if (androidBuildInfo.splitPerAbi) {
return androidBuildInfo.targetArchs.map<String>((AndroidArch arch) {
final String abi = getNameForAndroidArch(arch);
return 'app$flavorString-$abi-$buildType.apk';
});
}
return <String>['app$flavorString-$buildType.apk'];
} }
String bundleTaskFor(BuildInfo buildInfo) { String bundleTaskFor(BuildInfo buildInfo) {
......
...@@ -137,7 +137,7 @@ class AOTSnapshotter { ...@@ -137,7 +137,7 @@ class AOTSnapshotter {
final String aotSharedLibrary = fs.path.join(outputDir.path, 'app.so'); final String aotSharedLibrary = fs.path.join(outputDir.path, 'app.so');
outputPaths.add(aotSharedLibrary); outputPaths.add(aotSharedLibrary);
genSnapshotArgs.add('--snapshot_kind=app-aot-elf'); genSnapshotArgs.add('--snapshot_kind=app-aot-elf');
genSnapshotArgs.add('--assembly=$aotSharedLibrary'); genSnapshotArgs.add('--elf=$aotSharedLibrary');
} else { } else {
// Blob AOT snapshot. // Blob AOT snapshot.
final String vmSnapshotData = fs.path.join(outputDir.path, 'vm_snapshot_data'); final String vmSnapshotData = fs.path.join(outputDir.path, 'vm_snapshot_data');
......
...@@ -17,8 +17,6 @@ class BuildInfo { ...@@ -17,8 +17,6 @@ class BuildInfo {
this.compilationTraceFilePath, this.compilationTraceFilePath,
this.extraFrontEndOptions, this.extraFrontEndOptions,
this.extraGenSnapshotOptions, this.extraGenSnapshotOptions,
this.buildSharedLibrary,
this.targetPlatform,
this.fileSystemRoots, this.fileSystemRoots,
this.fileSystemScheme, this.fileSystemScheme,
this.buildNumber, this.buildNumber,
...@@ -50,12 +48,6 @@ class BuildInfo { ...@@ -50,12 +48,6 @@ class BuildInfo {
/// Extra command-line options for gen_snapshot. /// Extra command-line options for gen_snapshot.
final String extraGenSnapshotOptions; final String extraGenSnapshotOptions;
/// Whether to prefer AOT compiling to a *so file.
final bool buildSharedLibrary;
/// Target platform for the build (e.g. android_arm versus android_arm64).
final TargetPlatform targetPlatform;
/// Internal version number (not displayed to users). /// Internal version number (not displayed to users).
/// Each build must have a unique number to differentiate it from previous builds. /// Each build must have a unique number to differentiate it from previous builds.
/// It is used to determine whether one build is more recent than another, with higher numbers indicating more recent build. /// It is used to determine whether one build is more recent than another, with higher numbers indicating more recent build.
...@@ -98,15 +90,31 @@ class BuildInfo { ...@@ -98,15 +90,31 @@ class BuildInfo {
bool get supportsSimulator => isEmulatorBuildMode(mode); bool get supportsSimulator => isEmulatorBuildMode(mode);
String get modeName => getModeName(mode); String get modeName => getModeName(mode);
String get friendlyModeName => getFriendlyModeName(mode); String get friendlyModeName => getFriendlyModeName(mode);
}
/// Information about an Android build to be performed or used.
class AndroidBuildInfo {
const AndroidBuildInfo(
this.buildInfo, {
this.targetArchs = const <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
],
this.splitPerAbi = false,
});
BuildInfo withTargetPlatform(TargetPlatform targetPlatform) => // The build info containing the mode and flavor.
BuildInfo(mode, flavor, final BuildInfo buildInfo;
trackWidgetCreation: trackWidgetCreation,
compilationTraceFilePath: compilationTraceFilePath, /// Whether to split the shared library per ABI.
extraFrontEndOptions: extraFrontEndOptions, ///
extraGenSnapshotOptions: extraGenSnapshotOptions, /// When this is false, multiple ABIs will be contained within one primary
buildSharedLibrary: buildSharedLibrary, /// build artifact. When this is true, multiple build artifacts (one per ABI)
targetPlatform: targetPlatform); /// will be produced.
final bool splitPerAbi;
/// The target platforms for the build.
final Iterable<AndroidArch> targetArchs;
} }
/// The type of build. /// The type of build.
...@@ -254,6 +262,13 @@ enum IOSArch { ...@@ -254,6 +262,13 @@ enum IOSArch {
arm64, arm64,
} }
enum AndroidArch {
armeabi_v7a,
arm64_v8a,
x86,
x86_64,
}
/// The default set of iOS device architectures to build for. /// The default set of iOS device architectures to build for.
const List<IOSArch> defaultIOSArchs = <IOSArch>[ const List<IOSArch> defaultIOSArchs = <IOSArch>[
IOSArch.arm64, IOSArch.arm64,
...@@ -335,6 +350,51 @@ TargetPlatform getTargetPlatformForName(String platform) { ...@@ -335,6 +350,51 @@ TargetPlatform getTargetPlatformForName(String platform) {
return null; return null;
} }
AndroidArch getAndroidArchForName(String platform) {
switch (platform) {
case 'android-arm':
return AndroidArch.armeabi_v7a;
case 'android-arm64':
return AndroidArch.arm64_v8a;
case 'android-x64':
return AndroidArch.x86_64;
case 'android-x86':
return AndroidArch.x86;
}
assert(false);
return null;
}
String getNameForAndroidArch(AndroidArch arch) {
switch (arch) {
case AndroidArch.armeabi_v7a:
return 'armeabi-v7a';
case AndroidArch.arm64_v8a:
return 'arm64-v8a';
case AndroidArch.x86_64:
return 'x86_64';
case AndroidArch.x86:
return 'x86';
}
assert(false);
return null;
}
String getPlatformNameForAndroidArch(AndroidArch arch) {
switch (arch) {
case AndroidArch.armeabi_v7a:
return 'android-arm';
case AndroidArch.arm64_v8a:
return 'android-arm64';
case AndroidArch.x86_64:
return 'android-x64';
case AndroidArch.x86:
return 'android-x86';
}
assert(false);
return null;
}
HostPlatform getCurrentHostPlatform() { HostPlatform getCurrentHostPlatform() {
if (platform.isMacOS) if (platform.isMacOS)
return HostPlatform.darwin_x64; return HostPlatform.darwin_x64;
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
import 'dart:async'; import 'dart:async';
import '../android/apk.dart'; import '../android/apk.dart';
import '../base/terminal.dart';
import '../build_info.dart';
import '../globals.dart';
import '../project.dart'; import '../project.dart';
import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult; import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult;
import 'build.dart'; import 'build.dart';
...@@ -21,12 +24,14 @@ class BuildApkCommand extends BuildSubCommand { ...@@ -21,12 +24,14 @@ class BuildApkCommand extends BuildSubCommand {
argParser argParser
..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp) ..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp)
..addFlag('build-shared-library', ..addFlag('split-per-abi',
negatable: false, negatable: false,
help: 'Whether to prefer compiling to a *.so file (android only).', help: 'Whether to split the APKs per ABIs.'
'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
) )
..addOption('target-platform', ..addMultiOption('target-platform',
defaultsTo: 'android-arm', splitCommas: true,
defaultsTo: <String>['android-arm', 'android-arm64'],
allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'], allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
help: 'The target platform for which the app is compiled.', help: 'The target platform for which the app is compiled.',
); );
...@@ -49,10 +54,33 @@ class BuildApkCommand extends BuildSubCommand { ...@@ -49,10 +54,33 @@ class BuildApkCommand extends BuildSubCommand {
@override @override
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> runCommand() async {
final BuildInfo buildInfo = getBuildInfo();
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo,
splitPerAbi: argResults['split-per-abi'],
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName)
);
if (buildInfo.isRelease && !androidBuildInfo.splitPerAbi && androidBuildInfo.targetArchs.length > 1) {
final String targetPlatforms = argResults['target-platform'].join(', ');
printStatus('You are building a fat APK that includes binaries for '
'$targetPlatforms.', emphasis: true, color: TerminalColor.green);
printStatus('If you are deploying the app to the Play Store, '
'it\'s recommended to use app bundles or split the APK to reduce the APK size.', emphasis: true);
printStatus('To generate an app bundle, run:', emphasis: true, indent: 4);
printStatus('flutter build appbundle '
'--target-platform ${targetPlatforms.replaceAll(' ', '')}',indent: 8);
printStatus('Learn more on: https://developer.android.com/guide/app-bundle',indent: 8);
printStatus('To split the APKs per ABI, run:', emphasis: true, indent: 4);
printStatus('flutter build apk '
'--target-platform ${targetPlatforms.replaceAll(' ', '')} '
'--split-per-abi', indent: 8);
printStatus('Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',indent: 8);
}
await buildApk( await buildApk(
project: FlutterProject.current(), project: FlutterProject.current(),
target: targetFile, target: targetFile,
buildInfo: getBuildInfo(), androidBuildInfo: androidBuildInfo,
); );
return null; return null;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'dart:async'; import 'dart:async';
import '../android/app_bundle.dart'; import '../android/app_bundle.dart';
import '../build_info.dart';
import '../project.dart'; import '../project.dart';
import '../runner/flutter_command.dart' show FlutterCommandResult; import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart'; import 'build.dart';
...@@ -20,18 +21,11 @@ class BuildAppBundleCommand extends BuildSubCommand { ...@@ -20,18 +21,11 @@ class BuildAppBundleCommand extends BuildSubCommand {
argParser argParser
..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp) ..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp)
..addFlag( ..addMultiOption('target-platform',
'build-shared-library', splitCommas: true,
negatable: false, defaultsTo: <String>['android-arm', 'android-arm64'],
help: 'Whether to prefer compiling to a *.so file (android only).',
)
..addOption(
'target-platform',
allowed: <String>['android-arm', 'android-arm64'], allowed: <String>['android-arm', 'android-arm64'],
help: 'The target platform for which the app is compiled.\n' help: 'The target platform for which the app is compiled.',
'By default, the bundle will include \'arm\' and \'arm64\', '
'which is the recommended configuration for app bundles.\n'
'For more, see https://developer.android.com/distribute/best-practices/develop/64-bit',
); );
} }
...@@ -47,10 +41,13 @@ class BuildAppBundleCommand extends BuildSubCommand { ...@@ -47,10 +41,13 @@ class BuildAppBundleCommand extends BuildSubCommand {
@override @override
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> runCommand() async {
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(),
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName)
);
await buildAppBundle( await buildAppBundle(
project: FlutterProject.current(), project: FlutterProject.current(),
target: targetFile, target: targetFile,
buildInfo: getBuildInfo(), androidBuildInfo: androidBuildInfo,
); );
return null; return null;
} }
......
...@@ -387,8 +387,7 @@ class IOSSimulator extends Device { ...@@ -387,8 +387,7 @@ class IOSSimulator extends Device {
final BuildInfo debugBuildInfo = BuildInfo(BuildMode.debug, buildInfo.flavor, final BuildInfo debugBuildInfo = BuildInfo(BuildMode.debug, buildInfo.flavor,
trackWidgetCreation: buildInfo.trackWidgetCreation, trackWidgetCreation: buildInfo.trackWidgetCreation,
extraFrontEndOptions: buildInfo.extraFrontEndOptions, extraFrontEndOptions: buildInfo.extraFrontEndOptions,
extraGenSnapshotOptions: buildInfo.extraGenSnapshotOptions, extraGenSnapshotOptions: buildInfo.extraGenSnapshotOptions);
buildSharedLibrary: buildInfo.buildSharedLibrary);
final XcodeBuildResult buildResult = await buildXcodeProject( final XcodeBuildResult buildResult = await buildXcodeProject(
app: app, app: app,
......
...@@ -320,12 +320,6 @@ abstract class FlutterCommand extends Command<void> { ...@@ -320,12 +320,6 @@ abstract class FlutterCommand extends Command<void> {
} }
BuildInfo getBuildInfo() { BuildInfo getBuildInfo() {
TargetPlatform targetPlatform;
if (argParser.options.containsKey('target-platform') &&
argResults['target-platform'] != 'default') {
targetPlatform = getTargetPlatformForName(argResults['target-platform']);
}
final bool trackWidgetCreation = argParser.options.containsKey('track-widget-creation') final bool trackWidgetCreation = argParser.options.containsKey('track-widget-creation')
? argResults['track-widget-creation'] ? argResults['track-widget-creation']
: false; : false;
...@@ -362,10 +356,6 @@ abstract class FlutterCommand extends Command<void> { ...@@ -362,10 +356,6 @@ abstract class FlutterCommand extends Command<void> {
extraGenSnapshotOptions: argParser.options.containsKey(FlutterOptions.kExtraGenSnapshotOptions) extraGenSnapshotOptions: argParser.options.containsKey(FlutterOptions.kExtraGenSnapshotOptions)
? argResults[FlutterOptions.kExtraGenSnapshotOptions] ? argResults[FlutterOptions.kExtraGenSnapshotOptions]
: null, : null,
buildSharedLibrary: argParser.options.containsKey('build-shared-library')
? argResults['build-shared-library']
: false,
targetPlatform: targetPlatform,
fileSystemRoots: argParser.options.containsKey(FlutterOptions.kFileSystemRoot) fileSystemRoots: argParser.options.containsKey(FlutterOptions.kFileSystemRoot)
? argResults[FlutterOptions.kFileSystemRoot] : null, ? argResults[FlutterOptions.kFileSystemRoot] : null,
fileSystemScheme: argParser.options.containsKey(FlutterOptions.kFileSystemScheme) fileSystemScheme: argParser.options.containsKey(FlutterOptions.kFileSystemScheme)
......
...@@ -157,16 +157,102 @@ someOtherTask ...@@ -157,16 +157,102 @@ someOtherTask
}); });
test('should provide apk file name for default build types', () { test('should provide apk file name for default build types', () {
final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>[], fs.directory('/some/dir'),fs.directory('/some/dir')); final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>[], fs.directory('/some/dir'),fs.directory('/some/dir'));
expect(project.apkFileFor(BuildInfo.debug), 'app-debug.apk'); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo.debug)).first, 'app-debug.apk');
expect(project.apkFileFor(BuildInfo.profile), 'app-profile.apk'); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo.profile)).first, 'app-profile.apk');
expect(project.apkFileFor(BuildInfo.release), 'app-release.apk'); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo.release)).first, 'app-release.apk');
expect(project.apkFileFor(const BuildInfo(BuildMode.release, 'unknown')), isNull); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo(BuildMode.release, 'unknown'))).isEmpty, isTrue);
}); });
test('should provide apk file name for flavored build types', () { test('should provide apk file name for flavored build types', () {
final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>['free', 'paid'], fs.directory('/some/dir'),fs.directory('/some/dir')); final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>['free', 'paid'], fs.directory('/some/dir'),fs.directory('/some/dir'));
expect(project.apkFileFor(const BuildInfo(BuildMode.debug, 'free')), 'app-free-debug.apk'); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo(BuildMode.debug, 'free'))).first, 'app-free-debug.apk');
expect(project.apkFileFor(const BuildInfo(BuildMode.release, 'paid')), 'app-paid-release.apk'); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo(BuildMode.release, 'paid'))).first, 'app-paid-release.apk');
expect(project.apkFileFor(const BuildInfo(BuildMode.release, 'unknown')), isNull); expect(project.apkFilesFor(const AndroidBuildInfo(BuildInfo(BuildMode.release, 'unknown'))).isEmpty, isTrue);
});
test('should provide apks for default build types and each ABI', () {
final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>[], fs.directory('/some/dir'),fs.directory('/some/dir'));
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo.debug,
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
),
<String>[
'app-armeabi-v7a-debug.apk',
'app-arm64-v8a-debug.apk',
]);
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo.release,
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
),
<String>[
'app-armeabi-v7a-release.apk',
'app-arm64-v8a-release.apk',
]);
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo(BuildMode.release, 'unknown'),
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
).isEmpty, isTrue);
});
test('should provide apks for each ABI and flavored build types', () {
final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>['free', 'paid'], fs.directory('/some/dir'),fs.directory('/some/dir'));
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo(BuildMode.debug, 'free'),
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
),
<String>[
'app-free-armeabi-v7a-debug.apk',
'app-free-arm64-v8a-debug.apk',
]);
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo(BuildMode.release, 'paid'),
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
),
<String>[
'app-paid-armeabi-v7a-release.apk',
'app-paid-arm64-v8a-release.apk',
]);
expect(project.apkFilesFor(
const AndroidBuildInfo(
BuildInfo(BuildMode.release, 'unknown'),
splitPerAbi: true,
targetArchs: <AndroidArch>[
AndroidArch.armeabi_v7a,
AndroidArch.arm64_v8a,
]
)
).isEmpty, isTrue);
}); });
test('should provide bundle file name for default build types', () { test('should provide bundle file name for default build types', () {
final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>[], fs.directory('/some/dir'),fs.directory('/some/dir')); final GradleProject project = GradleProject(<String>['debug', 'profile', 'release'], <String>[], fs.directory('/some/dir'),fs.directory('/some/dir'));
......
...@@ -80,7 +80,7 @@ void main() { ...@@ -80,7 +80,7 @@ void main() {
}); });
}); });
group('Snapshotter - iOS AOT', () { group('Snapshotter - AOT', () {
const String kSnapshotDart = 'snapshot.dart'; const String kSnapshotDart = 'snapshot.dart';
String skyEnginePath; String skyEnginePath;
...@@ -395,10 +395,11 @@ void main() { ...@@ -395,10 +395,11 @@ void main() {
]); ]);
}, overrides: contextOverrides); }, overrides: contextOverrides);
testUsingContext('returns failure if buildSharedLibrary is true but no NDK is found', () async { testUsingContext('builds shared library for android-arm', () async {
final String outputPath = fs.path.join('build', 'foo'); fs.file('main.dill').writeAsStringSync('binary magic');
when(mockAndroidSdk.ndk).thenReturn(null); final String outputPath = fs.path.join('build', 'foo');
fs.directory(outputPath).createSync(recursive: true);
final int genSnapshotExitCode = await snapshotter.build( final int genSnapshotExitCode = await snapshotter.build(
platform: TargetPlatform.android_arm, platform: TargetPlatform.android_arm,
...@@ -409,8 +410,45 @@ void main() { ...@@ -409,8 +410,45 @@ void main() {
buildSharedLibrary: true, buildSharedLibrary: true,
); );
expect(genSnapshotExitCode, isNot(0)); expect(genSnapshotExitCode, 0);
expect(genSnapshot.callCount, 0); expect(genSnapshot.callCount, 1);
expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
expect(genSnapshot.snapshotType.mode, BuildMode.release);
expect(genSnapshot.additionalArgs, <String>[
'--deterministic',
'--snapshot_kind=app-aot-elf',
'--elf=build/foo/app.so',
'--no-sim-use-hardfp',
'--no-use-integer-division',
'main.dill',
]);
}, overrides: contextOverrides);
testUsingContext('builds shared library for android-arm64', () async {
fs.file('main.dill').writeAsStringSync('binary magic');
final String outputPath = fs.path.join('build', 'foo');
fs.directory(outputPath).createSync(recursive: true);
final int genSnapshotExitCode = await snapshotter.build(
platform: TargetPlatform.android_arm64,
buildMode: BuildMode.release,
mainPath: 'main.dill',
packagesPath: '.packages',
outputPath: outputPath,
buildSharedLibrary: true,
);
expect(genSnapshotExitCode, 0);
expect(genSnapshot.callCount, 1);
expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
expect(genSnapshot.snapshotType.mode, BuildMode.release);
expect(genSnapshot.additionalArgs, <String>[
'--deterministic',
'--snapshot_kind=app-aot-elf',
'--elf=build/foo/app.so',
'main.dill',
]);
}, overrides: contextOverrides); }, overrides: contextOverrides);
testUsingContext('builds Android arm release AOT snapshot', () async { testUsingContext('builds Android arm release AOT snapshot', () async {
......
...@@ -286,7 +286,7 @@ Information about project "Runner": ...@@ -286,7 +286,7 @@ Information about project "Runner":
platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine'); platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm')); when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm'));
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, targetPlatform: TargetPlatform.ios); const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null);
final FlutterProject project = FlutterProject.fromPath('path/to/project'); final FlutterProject project = FlutterProject.fromPath('path/to/project');
await updateGeneratedXcodeProperties( await updateGeneratedXcodeProperties(
project: project, project: project,
...@@ -304,7 +304,7 @@ Information about project "Runner": ...@@ -304,7 +304,7 @@ Information about project "Runner":
when(mockArtifacts.getArtifactPath(Artifact.flutterFramework, when(mockArtifacts.getArtifactPath(Artifact.flutterFramework,
platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine'); platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm')); when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm'));
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, trackWidgetCreation: true, targetPlatform: TargetPlatform.ios); const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, trackWidgetCreation: true);
final FlutterProject project = FlutterProject.fromPath('path/to/project'); final FlutterProject project = FlutterProject.fromPath('path/to/project');
await updateGeneratedXcodeProperties( await updateGeneratedXcodeProperties(
project: project, project: project,
...@@ -322,7 +322,7 @@ Information about project "Runner": ...@@ -322,7 +322,7 @@ Information about project "Runner":
when(mockArtifacts.getArtifactPath(Artifact.flutterFramework, when(mockArtifacts.getArtifactPath(Artifact.flutterFramework,
platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine'); platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm')); when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile_arm'));
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, targetPlatform: TargetPlatform.ios); const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null);
final FlutterProject project = FlutterProject.fromPath('path/to/project'); final FlutterProject project = FlutterProject.fromPath('path/to/project');
await updateGeneratedXcodeProperties( await updateGeneratedXcodeProperties(
project: project, project: project,
...@@ -340,7 +340,7 @@ Information about project "Runner": ...@@ -340,7 +340,7 @@ Information about project "Runner":
when(mockArtifacts.getArtifactPath(Artifact.flutterFramework, when(mockArtifacts.getArtifactPath(Artifact.flutterFramework,
platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine'); platform: TargetPlatform.ios, mode: anyNamed('mode'))).thenReturn('engine');
when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile')); when(mockArtifacts.engineOutPath).thenReturn(fs.path.join('out', 'ios_profile'));
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null, targetPlatform: TargetPlatform.ios); const BuildInfo buildInfo = BuildInfo(BuildMode.debug, null);
final FlutterProject project = FlutterProject.fromPath('path/to/project'); final FlutterProject project = FlutterProject.fromPath('path/to/project');
await updateGeneratedXcodeProperties( await updateGeneratedXcodeProperties(
......
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