build_apk.dart 2.37 KB
Newer Older
1 2 3 4 5 6
// 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';

7 8
import 'package:meta/meta.dart' show required;

9
import '../android/android_sdk.dart';
10
import '../android/gradle.dart';
11
import '../base/common.dart';
12
import '../build_info.dart';
13
import '../globals.dart';
14
import 'build.dart';
15

16 17
export '../android/android_device.dart' show AndroidDevice;

18
class ApkKeystoreInfo {
19 20 21 22 23 24
  ApkKeystoreInfo({
    @required this.keystore,
    this.password,
    this.keyAlias,
    @required this.keyPassword,
  }) {
25 26
    assert(keystore != null);
  }
27

28 29 30 31
  final String keystore;
  final String password;
  final String keyAlias;
  final String keyPassword;
32 33
}

34
class BuildApkCommand extends BuildSubCommand {
35 36
  BuildApkCommand() {
    usesTargetOption();
37
    addBuildModeFlags();
38
    usesPubOption();
39 40
  }

41 42 43 44
  @override
  final String name = 'apk';

  @override
45
  final String description = 'Build an Android APK file from your app.\n\n'
46 47
    'This command can build debug and release versions of your application. \'debug\' builds support\n'
    'debugging and a quick development cycle. \'release\' builds don\'t support debugging and are\n'
48
    'suitable for deploying to app stores.';
49

50
  @override
51
  Future<Null> runCommand() async {
52
    await super.runCommand();
53

54
    final BuildMode buildMode = getBuildMode();
55
    await buildApk(buildMode: buildMode, target: targetFile);
56
  }
57 58
}

59
Future<Null> buildApk({
Devon Carew's avatar
Devon Carew committed
60
  String target,
61 62
  BuildMode buildMode: BuildMode.debug,
  String kernelPath,
63
}) async {
64 65 66 67 68 69 70
  if (!isProjectUsingGradle()) {
    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.'
    );
71 72
  }

73
  // Validate that we can find an android sdk.
74 75
  if (androidSdk == null)
    throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.');
76

77
  final List<String> validationResult = androidSdk.validateSdkWellFormed();
78 79
  if (validationResult.isNotEmpty) {
    validationResult.forEach(printError);
80
    throwToolExit('Try re-installing or updating your Android SDK.');
81 82
  }

83
  return buildGradleProject(buildMode, target, kernelPath);
84
}