Log flags in build apk and appbundle (#39457)

This commit is contained in:
Emmanuel Garcia
2019-08-29 21:37:35 -07:00
committed by GitHub
parent 2dbf0106d8
commit 6266d5f35d
12 changed files with 411 additions and 184 deletions

View File

@@ -1,62 +0,0 @@
// Copyright 2019 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 'package:meta/meta.dart';
import '../base/common.dart';
import '../build_info.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'gradle.dart';
/// Provides a method to build a module or plugin as AAR.
abstract class AarBuilder {
/// Builds the AAR artifacts.
Future<void> build({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
});
}
/// Default implementation of [AarBuilder].
class AarBuilderImpl extends AarBuilder {
AarBuilderImpl();
/// Builds the AAR and POM files for the current Flutter module or plugin.
@override
Future<void> build({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration\n'
'is no longer valid. Please consult\n\n'
' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
if (!project.manifest.isModule && !project.manifest.isPlugin) {
throwToolExit('AARs can only be built for plugin or module projects.');
}
// Validate that we can find an Android SDK.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the `ANDROID_SDK_ROOT` environment variable.');
}
await buildGradleAar(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
outputDir: outputDir,
);
androidSdk.reinitialize();
}
}

View File

@@ -0,0 +1,169 @@
// Copyright 2019 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 'package:meta/meta.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../build_info.dart';
import '../globals.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'gradle.dart';
/// The builder in the current context.
AndroidBuilder get androidBuilder => context.get<AndroidBuilder>() ?? _AndroidBuilderImpl();
/// Provides the methods to build Android artifacts.
abstract class AndroidBuilder {
/// Builds an AAR artifact.
Future<void> buildAar({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
});
/// Builds an APK artifact.
Future<void> buildApk({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
});
/// Builds an App Bundle artifact.
Future<void> buildAab({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
});
}
/// Default implementation of [AarBuilder].
class _AndroidBuilderImpl extends AndroidBuilder {
_AndroidBuilderImpl();
/// Builds the AAR and POM files for the current Flutter module or plugin.
@override
Future<void> buildAar({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration '
'is no longer valid. Please consult\n\n'
' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
if (!project.manifest.isModule && !project.manifest.isPlugin) {
throwToolExit('AARs can only be built for plugin or module projects.');
}
// Validate that we can find an Android SDK.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the `ANDROID_SDK_ROOT` environment variable.');
}
await buildGradleAar(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
outputDir: outputDir,
);
androidSdk.reinitialize();
}
/// Builds the APK.
@override
Future<void> buildApk({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration '
'is no longer valid. Please consult\n\n'
' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the ANDROID_SDK_ROOT environment variable.');
}
await buildGradleProject(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: false,
);
androidSdk.reinitialize();
}
/// Builds the App Bundle.
@override
Future<void> buildAab({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration '
'is no longer valid. Please consult\n\n'
'https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null) {
throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.');
}
final List<String> validationResult = androidSdk.validateSdkWellFormed();
if (validationResult.isNotEmpty) {
for (String message in validationResult) {
printError(message, wrap: false);
}
throwToolExit('Try re-installing or updating your Android SDK.');
}
return buildGradleProject(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: true,
);
}
}
/// A fake implementation of [AndroidBuilder].
@visibleForTesting
class FakeAndroidBuilder implements AndroidBuilder {
@override
Future<void> buildAar({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
@required String outputDir,
}) async {}
@override
Future<void> buildApk({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {}
@override
Future<void> buildAab({
@required FlutterProject project,
@required AndroidBuildInfo androidBuildInfo,
@required String target,
}) async {}
}

View File

@@ -6,9 +6,9 @@ import 'dart:async';
import 'package:meta/meta.dart';
import '../android/android_builder.dart';
import '../android/android_sdk.dart';
import '../android/android_workflow.dart';
import '../android/apk.dart';
import '../application_package.dart';
import '../base/common.dart' show throwToolExit;
import '../base/file_system.dart';
@@ -482,7 +482,7 @@ class AndroidDevice extends Device {
if (!prebuiltApplication || androidSdk.licensesAvailable && androidSdk.latestVersion == null) {
printTrace('Building APK');
final FlutterProject project = FlutterProject.current();
await buildApk(
await androidBuilder.buildApk(
project: project,
target: mainPath,
androidBuildInfo: AndroidBuildInfo(debuggingOptions.buildInfo,

View File

@@ -1,41 +0,0 @@
// Copyright 2015 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 'package:meta/meta.dart';
import '../base/common.dart';
import '../build_info.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'gradle.dart';
Future<void> buildApk({
@required FlutterProject project,
@required String target,
@required AndroidBuildInfo androidBuildInfo,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration\n'
'is no longer valid. Please consult\n\n'
' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null)
throwToolExit('No Android SDK found. Try setting the ANDROID_SDK_ROOT environment variable.');
await buildGradleProject(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: false,
);
androidSdk.reinitialize();
}

View File

@@ -1,49 +0,0 @@
// Copyright 2015 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 'package:meta/meta.dart';
import '../base/common.dart';
import '../build_info.dart';
import '../globals.dart';
import '../project.dart';
import 'android_sdk.dart';
import 'gradle.dart';
Future<void> buildAppBundle({
@required FlutterProject project,
@required String target,
@required AndroidBuildInfo androidBuildInfo,
}) async {
if (!project.android.isUsingGradle) {
throwToolExit(
'The build process for Android has changed, and the current project configuration\n'
'is no longer valid. Please consult\n\n'
'https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n'
'for details on how to upgrade the project.'
);
}
// Validate that we can find an android sdk.
if (androidSdk == null)
throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.');
final List<String> validationResult = androidSdk.validateSdkWellFormed();
if (validationResult.isNotEmpty) {
for (String message in validationResult) {
printError(message, wrap: false);
}
throwToolExit('Try re-installing or updating your Android SDK.');
}
return buildGradleProject(
project: project,
androidBuildInfo: androidBuildInfo,
target: target,
isBuildingBundle: true,
);
}

View File

@@ -4,8 +4,7 @@
import 'dart:async';
import '../android/aar.dart';
import '../base/context.dart';
import '../android/android_builder.dart';
import '../base/os.dart';
import '../build_info.dart';
import '../project.dart';
@@ -13,9 +12,6 @@ import '../reporting/reporting.dart';
import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult;
import 'build.dart';
/// The AAR builder in the current context.
AarBuilder get aarBuilder => context.get<AarBuilder>() ?? AarBuilderImpl();
class BuildAarCommand extends BuildSubCommand {
BuildAarCommand({bool verboseHelp = false}) {
addBuildModeFlags(verboseHelp: verboseHelp);
@@ -74,7 +70,7 @@ class BuildAarCommand extends BuildSubCommand {
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo,
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName));
await aarBuilder.build(
await androidBuilder.buildAar(
project: _getProject(),
target: '', // Not needed because this command only builds Android's code.
androidBuildInfo: androidBuildInfo,

View File

@@ -4,11 +4,12 @@
import 'dart:async';
import '../android/apk.dart';
import '../android/android_builder.dart';
import '../base/terminal.dart';
import '../build_info.dart';
import '../globals.dart';
import '../project.dart';
import '../reporting/reporting.dart';
import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult;
import 'build.dart';
@@ -39,18 +40,40 @@ class BuildApkCommand extends BuildSubCommand {
@override
final String name = 'apk';
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
DevelopmentArtifact.universal,
DevelopmentArtifact.android,
};
@override
final String description = 'Build an Android APK file from your app.\n\n'
'This command can build debug and release versions of your application. \'debug\' builds support '
'debugging and a quick development cycle. \'release\' builds don\'t support debugging and are '
'suitable for deploying to app stores.';
@override
Future<Map<CustomDimensions, String>> get usageValues async {
final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};
usage[CustomDimensions.commandBuildApkTargetPlatform] =
(argResults['target-platform'] as List<String>).join(',');
usage[CustomDimensions.commandBuildApkSplitPerAbi] =
argResults['split-per-abi'].toString();
if (argResults['release']) {
usage[CustomDimensions.commandBuildApkBuildMode] = 'release';
} else if (argResults['debug']) {
usage[CustomDimensions.commandBuildApkBuildMode] = 'debug';
} else if (argResults['profile']) {
usage[CustomDimensions.commandBuildApkBuildMode] = 'profile';
} else {
// The build defaults to release.
usage[CustomDimensions.commandBuildApkBuildMode] = 'release';
}
return usage;
}
@override
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
DevelopmentArtifact.universal,
DevelopmentArtifact.android,
};
@override
Future<FlutterCommandResult> runCommand() async {
final BuildInfo buildInfo = getBuildInfo();
@@ -76,7 +99,7 @@ class BuildApkCommand extends BuildSubCommand {
'--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 androidBuilder.buildApk(
project: FlutterProject.current(),
target: targetFile,
androidBuildInfo: androidBuildInfo,

View File

@@ -4,9 +4,10 @@
import 'dart:async';
import '../android/app_bundle.dart';
import '../android/android_builder.dart';
import '../build_info.dart';
import '../project.dart';
import '../reporting/reporting.dart';
import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart';
@@ -39,12 +40,32 @@ class BuildAppBundleCommand extends BuildSubCommand {
'debugging and a quick development cycle. \'release\' builds don\'t support debugging and are '
'suitable for deploying to app stores. \n app bundle improves your app size';
@override
Future<Map<CustomDimensions, String>> get usageValues async {
final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};
usage[CustomDimensions.commandBuildAppBundleTargetPlatform] =
(argResults['target-platform'] as List<String>).join(',');
if (argResults['release']) {
usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
} else if (argResults['debug']) {
usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'debug';
} else if (argResults['profile']) {
usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'profile';
} else {
// The build defaults to release.
usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
}
return usage;
}
@override
Future<FlutterCommandResult> runCommand() async {
final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(),
targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName)
);
await buildAppBundle(
await androidBuilder.buildAab(
project: FlutterProject.current(),
target: targetFile,
androidBuildInfo: androidBuildInfo,

View File

@@ -48,6 +48,11 @@ enum CustomDimensions {
commandBuildAarProjectType, // cd35
buildEventCommand, // cd36
buildEventSettings, // cd37
commandBuildApkTargetPlatform, // cd38
commandBuildApkBuildMode, // cd39
commandBuildApkSplitPerAbi, // cd40
commandBuildAppBundleTargetPlatform, // cd41
commandBuildAppBundleBuildMode, // cd42
}
String cdKey(CustomDimensions cd) => 'cd${cd.index + 1}';