gradle.dart 37.5 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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
import 'package:crypto/crypto.dart';
8 9
import 'package:meta/meta.dart';

10
import '../android/android_sdk.dart';
11
import '../artifacts.dart';
12
import '../base/common.dart';
13
import '../base/file_system.dart';
14 15
import '../base/logger.dart';
import '../base/os.dart';
16
import '../base/platform.dart';
17
import '../base/process.dart';
18
import '../base/terminal.dart';
19
import '../base/utils.dart';
20
import '../base/version.dart';
21 22
import '../build_info.dart';
import '../cache.dart';
23
import '../features.dart';
24
import '../flutter_manifest.dart';
25
import '../globals.dart';
26
import '../project.dart';
27
import '../reporting/reporting.dart';
28
import 'android_sdk.dart';
29
import 'android_studio.dart';
30

31
final RegExp _assembleTaskPattern = RegExp(r'assemble(\S+)');
32

33 34
GradleProject _cachedGradleAppProject;
GradleProject _cachedGradleLibraryProject;
35
String _cachedGradleExecutable;
36

37 38 39 40
enum FlutterPluginVersion {
  none,
  v1,
  v2,
41
  managed,
42
}
43

44 45 46
// Investigation documented in #13975 suggests the filter should be a subset
// of the impact of -q, but users insist they see the error message sometimes
// anyway.  If we can prove it really is impossible, delete the filter.
47 48
// This technically matches everything *except* the NDK message, since it's
// passed to a function that filters out all lines that don't match a filter.
49
final RegExp ndkMessageFilter = RegExp(r'^(?!NDK is missing a ".*" directory'
50 51 52
  r'|If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning'
  r'|If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to .*)');

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// This regex is intentionally broad. AndroidX errors can manifest in multiple
// different ways and each one depends on the specific code config and
// filesystem paths of the project. Throwing the broadest net possible here to
// catch all known and likely cases.
//
// Example stack traces:
//
// https://github.com/flutter/flutter/issues/27226 "AAPT: error: resource android:attr/fontVariationSettings not found."
// https://github.com/flutter/flutter/issues/27106 "Android resource linking failed|Daemon: AAPT2|error: failed linking references"
// https://github.com/flutter/flutter/issues/27493 "error: cannot find symbol import androidx.annotation.NonNull;"
// https://github.com/flutter/flutter/issues/23995 "error: package android.support.annotation does not exist import android.support.annotation.NonNull;"
final RegExp androidXFailureRegex = RegExp(r'(AAPT|androidx|android\.support)');

final RegExp androidXPluginWarningRegex = RegExp(r'\*{57}'
  r"|WARNING: This version of (\w+) will break your Android build if it or its dependencies aren't compatible with AndroidX."
  r'|See https://goo.gl/CP92wY for more information on the problem and how to fix it.'
  r'|This warning prints for all Android build failures. The real root cause of the error may be unrelated.');

71
FlutterPluginVersion getFlutterPluginVersion(AndroidProject project) {
72
  final File plugin = project.hostAppGradleRoot.childFile(
73
      fs.path.join('buildSrc', 'src', 'main', 'groovy', 'FlutterPlugin.groovy'));
74
  if (plugin.existsSync()) {
75
    final String packageLine = plugin.readAsLinesSync().skip(4).first;
76
    if (packageLine == 'package io.flutter.gradle') {
77 78 79
      return FlutterPluginVersion.v2;
    }
    return FlutterPluginVersion.v1;
80
  }
81 82
  final File appGradle = project.hostAppGradleRoot.childFile(
      fs.path.join('app', 'build.gradle'));
83 84
  if (appGradle.existsSync()) {
    for (String line in appGradle.readAsLinesSync()) {
85
      if (line.contains(RegExp(r'apply from: .*/flutter.gradle'))) {
86 87
        return FlutterPluginVersion.managed;
      }
88 89 90
      if (line.contains("def flutterPluginVersion = 'managed'")) {
        return FlutterPluginVersion.managed;
      }
91
    }
92
  }
93
  return FlutterPluginVersion.none;
94 95
}

96 97
/// Returns the apk file created by [buildGradleProject]
Future<File> getGradleAppOut(AndroidProject androidProject) async {
98
  switch (getFlutterPluginVersion(androidProject)) {
99 100 101
    case FlutterPluginVersion.none:
      // Fall through. Pretend we're v1, and just go with it.
    case FlutterPluginVersion.v1:
102
      return androidProject.gradleAppOutV1File;
103 104
    case FlutterPluginVersion.managed:
      // Fall through. The managed plugin matches plugin v2 for now.
105
    case FlutterPluginVersion.v2:
106
      return fs.file((await _gradleAppProject()).apkDirectory.childFile('app.apk'));
107 108 109 110
  }
  return null;
}

111 112 113 114 115 116 117 118
Future<GradleProject> _gradleAppProject() async {
  _cachedGradleAppProject ??= await _readGradleProject(isLibrary: false);
  return _cachedGradleAppProject;
}

Future<GradleProject> _gradleLibraryProject() async {
  _cachedGradleLibraryProject ??= await _readGradleProject(isLibrary: true);
  return _cachedGradleLibraryProject;
119 120
}

121 122 123
/// Runs `gradlew dependencies`, ensuring that dependencies are resolved and
/// potentially downloaded.
Future<void> checkGradleDependencies() async {
124
  final Status progress = logger.startProgress('Ensuring gradle dependencies are up to date...', timeout: timeoutConfiguration.slowOperation);
125
  final FlutterProject flutterProject = FlutterProject.current();
126 127 128 129 130 131 132 133 134 135
  final String gradle = await _ensureGradle(flutterProject);
  await runCheckedAsync(
    <String>[gradle, 'dependencies'],
    workingDirectory: flutterProject.android.hostAppGradleRoot.path,
    environment: _gradleEnv,
  );
  androidSdk.reinitialize();
  progress.stop();
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/// Tries to create `settings_aar.gradle` in an app project by removing the subprojects
/// from the existing `settings.gradle` file. This operation will fail if the existing
/// `settings.gradle` file has local edits.
void createSettingsAarGradle(Directory androidDirectory) {
  final File newSettingsFile = androidDirectory.childFile('settings_aar.gradle');
  if (newSettingsFile.existsSync()) {
    return;
  }
  final File currentSettingsFile = androidDirectory.childFile('settings.gradle');
  if (!currentSettingsFile.existsSync()) {
    return;
  }
  final String currentFileContent = currentSettingsFile.readAsStringSync();

  final String newSettingsRelativeFile = fs.path.relative(newSettingsFile.path);
  final Status status = logger.startProgress('✏️  Creating `$newSettingsRelativeFile`...',
      timeout: timeoutConfiguration.fastOperation);

  final String flutterRoot = fs.path.absolute(Cache.flutterRoot);
  final File deprecatedFile = fs.file(fs.path.join(flutterRoot, 'packages','flutter_tools',
      'gradle', 'deprecated_settings.gradle'));
  assert(deprecatedFile.existsSync());
  final String settingsAarContent = fs.file(fs.path.join(flutterRoot, 'packages','flutter_tools',
      'gradle', 'settings_aar.gradle.tmpl')).readAsStringSync();

  // Get the `settings.gradle` content variants that should be patched.
  final List<String> existingVariants = deprecatedFile.readAsStringSync().split(';EOF');
  existingVariants.add(settingsAarContent);

  bool exactMatch = false;
  for (String fileContentVariant in existingVariants) {
    if (currentFileContent.trim() == fileContentVariant.trim()) {
      exactMatch = true;
      break;
    }
  }
  if (!exactMatch) {
    status.cancel();
    printError('*******************************************************************************************');
    printError('Flutter tried to create the file `$newSettingsRelativeFile`, but failed.');
    // Print how to manually update the file.
    printError(fs.file(fs.path.join(flutterRoot, 'packages','flutter_tools',
        'gradle', 'manual_migration_settings.gradle.md')).readAsStringSync());
    printError('*******************************************************************************************');
    throwToolExit('Please create the file and run this command again.');
  }
  // Copy the new file.
  newSettingsFile.writeAsStringSync(settingsAarContent);
  status.stop();
  printStatus('✅ `$newSettingsRelativeFile` created successfully.');
}

188 189
// Note: Dependencies are resolved and possibly downloaded as a side-effect
// of calculating the app properties using Gradle. This may take minutes.
190
Future<GradleProject> _readGradleProject({bool isLibrary = false}) async {
191
  final FlutterProject flutterProject = FlutterProject.current();
192
  final String gradle = await _ensureGradle(flutterProject);
193
  updateLocalProperties(project: flutterProject);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

  final FlutterManifest manifest = flutterProject.manifest;
  final Directory hostAppGradleRoot = flutterProject.android.hostAppGradleRoot;

  if (featureFlags.isPluginAsAarEnabled &&
      !manifest.isPlugin && !manifest.isModule) {
    createSettingsAarGradle(hostAppGradleRoot);
  }
  if (manifest.isPlugin) {
    assert(isLibrary);
    return GradleProject(
      <String>['debug', 'profile', 'release'],
      <String>[], // Plugins don't have flavors.
      flutterProject.directory.childDirectory('build').path,
    );
  }
210
  final Status status = logger.startProgress('Resolving dependencies...', timeout: timeoutConfiguration.slowOperation);
211
  GradleProject project;
212 213
  // Get the properties and tasks from Gradle, so we can determinate the `buildDir`,
  // flavors and build types defined in the project. If gradle fails, then check if the failure is due to t
214
  try {
215
    final RunResult propertiesRunResult = await runCheckedAsync(
216 217
      <String>[gradle, isLibrary ? 'properties' : 'app:properties'],
      workingDirectory: hostAppGradleRoot.path,
218
      environment: _gradleEnv,
219
    );
220
    final RunResult tasksRunResult = await runCheckedAsync(
221 222
      <String>[gradle, isLibrary ? 'tasks': 'app:tasks', '--all', '--console=auto'],
      workingDirectory: hostAppGradleRoot.path,
223 224 225
      environment: _gradleEnv,
    );
    project = GradleProject.fromAppProperties(propertiesRunResult.stdout, tasksRunResult.stdout);
226
  } catch (exception) {
227
    if (getFlutterPluginVersion(flutterProject.android) == FlutterPluginVersion.managed) {
228
      status.cancel();
229 230
      // Handle known exceptions.
      throwToolExitIfLicenseNotAccepted(exception);
231
      // Print a general Gradle error and exit.
232
      printError('* Error running Gradle:\n$exception\n');
233
      throwToolExit('Please review your Gradle project setup in the android/ folder.');
234
    }
235
    // Fall back to the default
236
    project = GradleProject(
237
      <String>['debug', 'profile', 'release'],
238 239
      <String>[],
      fs.path.join(flutterProject.android.hostAppGradleRoot.path, 'app', 'build')
240
    );
241
  }
242 243
  status.stop();
  return project;
244 245
}

246 247 248 249 250
/// Handle Gradle error thrown when Gradle needs to download additional
/// Android SDK components (e.g. Platform Tools), and the license
/// for that component has not been accepted.
void throwToolExitIfLicenseNotAccepted(Exception exception) {
  const String licenseNotAcceptedMatcher =
251 252
    r'You have not accepted the license agreements of the following SDK components:'
    r'\s*\[(.+)\]';
253 254
  final RegExp licenseFailure = RegExp(licenseNotAcceptedMatcher, multiLine: true);
  final Match licenseMatch = licenseFailure.firstMatch(exception.toString());
255 256 257 258 259 260 261 262 263 264 265 266
  if (licenseMatch != null) {
    final String missingLicenses = licenseMatch.group(1);
    final String errorMessage =
      '\n\n* Error running Gradle:\n'
      'Unable to download needed Android SDK components, as the following licenses have not been accepted:\n'
      '$missingLicenses\n\n'
      'To resolve this, please run the following command in a Terminal:\n'
      'flutter doctor --android-licenses';
    throwToolExit(errorMessage);
  }
}

267 268
String _locateGradlewExecutable(Directory directory) {
  final File gradle = directory.childFile(
269
    platform.isWindows ? 'gradlew.bat' : 'gradlew',
270
  );
271

272 273
  if (gradle.existsSync()) {
    os.makeExecutable(gradle);
274
    return gradle.absolute.path;
275 276 277 278 279
  } else {
    return null;
  }
}

280 281
Future<String> _ensureGradle(FlutterProject project) async {
  _cachedGradleExecutable ??= await _initializeGradle(project);
282 283 284 285 286
  return _cachedGradleExecutable;
}

// Note: Gradle may be bootstrapped and possibly downloaded as a side-effect
// of validating the Gradle executable. This may take several seconds.
287
Future<String> _initializeGradle(FlutterProject project) async {
288
  final Directory android = project.android.hostAppGradleRoot;
289
  final Status status = logger.startProgress('Initializing gradle...', timeout: timeoutConfiguration.slowOperation);
290
  String gradle = _locateGradlewExecutable(android);
291
  if (gradle == null) {
292 293
    injectGradleWrapper(android);
    gradle = _locateGradlewExecutable(android);
294
  }
295 296 297 298 299 300 301
  if (gradle == null)
    throwToolExit('Unable to locate gradlew script');
  printTrace('Using gradle from $gradle.');
  // Validates the Gradle executable by asking for its version.
  // Makes Gradle Wrapper download and install Gradle distribution, if needed.
  await runCheckedAsync(<String>[gradle, '-v'], environment: _gradleEnv);
  status.stop();
302
  return gradle;
303 304
}

305 306 307 308 309 310
/// Injects the Gradle wrapper into the specified directory.
void injectGradleWrapper(Directory directory) {
  copyDirectorySync(cache.getArtifactDirectory('gradle_wrapper'), directory);
  _locateGradlewExecutable(directory);
  final File propertiesFile = directory.childFile(fs.path.join('gradle', 'wrapper', 'gradle-wrapper.properties'));
  if (!propertiesFile.existsSync()) {
311
    final String gradleVersion = getGradleVersionForAndroidPlugin(directory);
312
    propertiesFile.writeAsStringSync('''
313 314 315 316 317 318 319 320 321 322
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-$gradleVersion-all.zip
''', flush: true,
    );
  }
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/// Returns true if [targetVersion] is within the range [min] and [max] inclusive.
bool _isWithinVersionRange(String targetVersion, {String min, String max}) {
  final Version parsedTargetVersion = Version.parse(targetVersion);
  return parsedTargetVersion >= Version.parse(min) &&
      parsedTargetVersion <= Version.parse(max);
}

const String defaultGradleVersion = '4.10.2';

/// Returns the Gradle version that is required by the given Android Gradle plugin version
/// by picking the largest compatible version from
/// https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
String getGradleVersionFor(String androidPluginVersion) {
  if (_isWithinVersionRange(androidPluginVersion, min: '1.0.0', max: '1.1.3')) {
    return '2.3';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '1.2.0', max: '1.3.1')) {
    return '2.9';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '1.5.0', max: '1.5.0')) {
    return '2.2.1';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '2.0.0', max: '2.1.2')) {
    return '2.13';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '2.1.3', max: '2.2.3')) {
    return '2.14.1';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '2.3.0', max: '2.9.9')) {
    return '3.3';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '3.0.0', max: '3.0.9')) {
    return '4.1';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '3.1.0', max: '3.1.9')) {
    return '4.4';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '3.2.0', max: '3.2.1')) {
    return '4.6';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '3.3.0', max: '3.3.2')) {
    return '4.10.2';
  }
  if (_isWithinVersionRange(androidPluginVersion, min: '3.4.0', max: '3.5.0')) {
    return '5.1.1';
  }
  throwToolExit('Unsuported Android Plugin version: $androidPluginVersion.');
  return '';
}

final RegExp _androidPluginRegExp = RegExp('com\.android\.tools\.build\:gradle\:(\\d+\.\\d+\.\\d+\)');

/// Returns the Gradle version that the current Android plugin depends on when found,
/// otherwise it returns a default version.
///
/// The Android plugin version is specified in the [build.gradle] file within
/// the project's Android directory.
String getGradleVersionForAndroidPlugin(Directory directory) {
  final File buildFile = directory.childFile('build.gradle');
  if (!buildFile.existsSync()) {
    return defaultGradleVersion;
  }
  final String buildFileContent = buildFile.readAsStringSync();
  final Iterable<Match> pluginMatches = _androidPluginRegExp.allMatches(buildFileContent);

  if (pluginMatches.isEmpty) {
    return defaultGradleVersion;
  }
  final String androidPluginVersion = pluginMatches.first.group(1);
  return getGradleVersionFor(androidPluginVersion);
}

395 396
/// Overwrite local.properties in the specified Flutter project's Android
/// sub-project, if needed.
397
///
398 399
/// If [requireAndroidSdk] is true (the default) and no Android SDK is found,
/// this will fail with a [ToolExit].
400
void updateLocalProperties({
401 402 403
  @required FlutterProject project,
  BuildInfo buildInfo,
  bool requireAndroidSdk = true,
404 405 406
}) {
  if (requireAndroidSdk) {
    _exitIfNoAndroidSdk();
407 408
  }

409
  final File localProperties = project.android.localPropertiesFile;
410 411 412 413
  bool changed = false;

  SettingsFile settings;
  if (localProperties.existsSync()) {
414
    settings = SettingsFile.parseFromFile(localProperties);
415
  } else {
416
    settings = SettingsFile();
417 418 419
    changed = true;
  }

420 421
  void changeIfNecessary(String key, String value) {
    if (settings.values[key] != value) {
422 423 424 425 426
      if (value == null) {
        settings.values.remove(key);
      } else {
        settings.values[key] = value;
      }
427 428
      changed = true;
    }
429 430
  }

431
  final FlutterManifest manifest = project.manifest;
432

433 434
  if (androidSdk != null)
    changeIfNecessary('sdk.dir', escapePath(androidSdk.directory));
435

436
  changeIfNecessary('flutter.sdk', escapePath(Cache.flutterRoot));
437 438

  if (buildInfo != null) {
439
    changeIfNecessary('flutter.buildMode', buildInfo.modeName);
440
    final String buildName = validatedBuildNameForPlatform(TargetPlatform.android_arm, buildInfo.buildName ?? manifest.buildName);
441
    changeIfNecessary('flutter.versionName', buildName);
442
    final String buildNumber = validatedBuildNumberForPlatform(TargetPlatform.android_arm, buildInfo.buildNumber ?? manifest.buildNumber);
443 444
    changeIfNecessary('flutter.versionCode', buildNumber?.toString());
  }
445

446 447
  if (changed)
    settings.writeContents(localProperties);
448 449
}

450 451 452 453
/// Writes standard Android local properties to the specified [properties] file.
///
/// Writes the path to the Android SDK, if known.
void writeLocalProperties(File properties) {
454
  final SettingsFile settings = SettingsFile();
455 456 457 458 459 460 461 462 463 464 465 466 467
  if (androidSdk != null) {
    settings.values['sdk.dir'] = escapePath(androidSdk.directory);
  }
  settings.writeContents(properties);
}

/// Throws a ToolExit, if the path to the Android SDK is not known.
void _exitIfNoAndroidSdk() {
  if (androidSdk == null) {
    throwToolExit('Unable to locate Android SDK. Please run `flutter doctor` for more details.');
  }
}

468
Future<void> buildGradleProject({
469
  @required FlutterProject project,
470
  @required AndroidBuildInfo androidBuildInfo,
471
  @required String target,
472
  @required bool isBuildingBundle,
473
}) async {
474
  // Update the local.properties file with the build mode, version name and code.
475 476 477
  // FlutterPlugin v1 reads local.properties to determine build mode. Plugin v2
  // uses the standard Android way to determine what to build, but we still
  // update local.properties, in case we want to use it in the future.
478 479 480 481
  // Version name and number are provided by the pubspec.yaml file
  // and can be overwritten with flutter build command.
  // The default Gradle script reads the version name and number
  // from the local.properties file.
482
  updateLocalProperties(project: project, buildInfo: androidBuildInfo.buildInfo);
483

484
  final String gradle = await _ensureGradle(project);
485

486
  switch (getFlutterPluginVersion(project.android)) {
487 488 489
    case FlutterPluginVersion.none:
      // Fall through. Pretend it's v1, and just go for it.
    case FlutterPluginVersion.v1:
490
      return _buildGradleProjectV1(project, gradle);
491 492
    case FlutterPluginVersion.managed:
      // Fall through. Managed plugin builds the same way as plugin v2.
493
    case FlutterPluginVersion.v2:
494
      return _buildGradleProjectV2(project, gradle, androidBuildInfo, target, isBuildingBundle);
495 496 497
  }
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
Future<void> buildGradleAar({
  @required FlutterProject project,
  @required AndroidBuildInfo androidBuildInfo,
  @required String target,
  @required String outputDir,
}) async {
  final FlutterManifest manifest = project.manifest;

  GradleProject gradleProject;
  if (manifest.isModule) {
    gradleProject = await _gradleAppProject();
  } else if (manifest.isPlugin) {
    gradleProject = await _gradleLibraryProject();
  } else {
    throwToolExit('AARs can only be built for plugin or module projects.');
  }

  if (outputDir != null && outputDir.isNotEmpty) {
    gradleProject.buildDirectory = outputDir;
  }

  final String aarTask = gradleProject.aarTaskFor(androidBuildInfo.buildInfo);
  if (aarTask == null) {
    printUndefinedTask(gradleProject, androidBuildInfo.buildInfo);
    throwToolExit('Gradle build aborted.');
  }
  final Status status = logger.startProgress(
    'Running Gradle task \'$aarTask\'...',
    timeout: timeoutConfiguration.slowOperation,
    multilineOutput: true,
  );

  final String gradle = await _ensureGradle(project);
  final String gradlePath = fs.file(gradle).absolute.path;
  final String flutterRoot = fs.path.absolute(Cache.flutterRoot);
  final String initScript = fs.path.join(flutterRoot, 'packages','flutter_tools', 'gradle', 'aar_init_script.gradle');
  final List<String> command = <String>[
    gradlePath,
    '-I=$initScript',
    '-Pflutter-root=$flutterRoot',
    '-Poutput-dir=${gradleProject.buildDirectory}',
    '-Pis-plugin=${manifest.isPlugin}',
    '-Dbuild-plugins-as-aars=true',
  ];

  if (target != null && target.isNotEmpty) {
    command.add('-Ptarget=$target');
  }

  if (androidBuildInfo.targetArchs.isNotEmpty) {
    final String targetPlatforms = androidBuildInfo.targetArchs
        .map(getPlatformNameForAndroidArch).join(',');
    command.add('-Ptarget-platform=$targetPlatforms');
  }
  command.add(aarTask);

  final Stopwatch sw = Stopwatch()..start();
  int exitCode = 1;

  try {
    exitCode = await runCommandAndStreamOutput(
      command,
      workingDirectory: project.android.hostAppGradleRoot.path,
      allowReentrantFlutter: true,
      environment: _gradleEnv,
      mapFunction: (String line) {
        // Always print the full line in verbose mode.
        if (logger.isVerbose) {
          return line;
        }
        return null;
      },
    );
  } finally {
    status.stop();
  }
  flutterUsage.sendTiming('build', 'gradle-aar', Duration(milliseconds: sw.elapsedMilliseconds));

  if (exitCode != 0) {
    throwToolExit('Gradle task $aarTask failed with exit code $exitCode', exitCode: exitCode);
  }

  final Directory repoDirectory = gradleProject.repoDirectory;
  if (!repoDirectory.existsSync()) {
    throwToolExit('Gradle task $aarTask failed to produce $repoDirectory', exitCode: exitCode);
  }
  printStatus('Built ${fs.path.relative(repoDirectory.path)}.', color: TerminalColor.green);
}

587
Future<void> _buildGradleProjectV1(FlutterProject project, String gradle) async {
588
  // Run 'gradlew build'.
589
  final Status status = logger.startProgress(
590
    'Running \'gradlew build\'...',
591
    timeout: timeoutConfiguration.slowOperation,
592 593
    multilineOutput: true,
  );
594
  final Stopwatch sw = Stopwatch()..start();
595
  final int exitCode = await runCommandAndStreamOutput(
596
    <String>[fs.file(gradle).absolute.path, 'build'],
597
    workingDirectory: project.android.hostAppGradleRoot.path,
598 599
    allowReentrantFlutter: true,
    environment: _gradleEnv,
600
  );
Devon Carew's avatar
Devon Carew committed
601
  status.stop();
602
  flutterUsage.sendTiming('build', 'gradle-v1', Duration(milliseconds: sw.elapsedMilliseconds));
603

604 605
  if (exitCode != 0)
    throwToolExit('Gradle build failed: $exitCode', exitCode: exitCode);
606

607
  printStatus('Built ${fs.path.relative(project.android.gradleAppOutV1File.path)}.');
608 609
}

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
String _hex(List<int> bytes) {
  final StringBuffer result = StringBuffer();
  for (int part in bytes)
    result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
  return result.toString();
}

String _calculateSha(File file) {
  final Stopwatch sw = Stopwatch()..start();
  final List<int> bytes = file.readAsBytesSync();
  printTrace('calculateSha: reading file took ${sw.elapsedMilliseconds}us');
  flutterUsage.sendTiming('build', 'apk-sha-read', Duration(milliseconds: sw.elapsedMilliseconds));
  sw.reset();
  final String sha = _hex(sha1.convert(bytes).bytes);
  printTrace('calculateSha: computing sha took ${sw.elapsedMilliseconds}us');
  flutterUsage.sendTiming('build', 'apk-sha-calc', Duration(milliseconds: sw.elapsedMilliseconds));
  return sha;
}

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
void printUndefinedTask(GradleProject project, BuildInfo buildInfo) {
  printError('');
  printError('The Gradle project does not define a task suitable for the requested build.');
  if (!project.buildTypes.contains(buildInfo.modeName)) {
    printError('Review the android/app/build.gradle file and ensure it defines a ${buildInfo.modeName} build type.');
    return;
  }
  if (project.productFlavors.isEmpty) {
    printError('The android/app/build.gradle file does not define any custom product flavors.');
    printError('You cannot use the --flavor option.');
  } else {
    printError('The android/app/build.gradle file defines product flavors: ${project.productFlavors.join(', ')}');
    printError('You must specify a --flavor option to select one of them.');
  }
}

645
Future<void> _buildGradleProjectV2(
646 647
  FlutterProject flutterProject,
  String gradle,
648
  AndroidBuildInfo androidBuildInfo,
649 650 651
  String target,
  bool isBuildingBundle,
) async {
652
  final GradleProject project = await _gradleAppProject();
653
  final BuildInfo buildInfo = androidBuildInfo.buildInfo;
654 655 656 657 658 659 660 661

  String assembleTask;

  if (isBuildingBundle) {
    assembleTask = project.bundleTaskFor(buildInfo);
  } else {
    assembleTask = project.assembleTaskFor(buildInfo);
  }
662
  if (assembleTask == null) {
663 664
    printUndefinedTask(project, buildInfo);
    throwToolExit('Gradle build aborted.');
665
  }
666
  final Status status = logger.startProgress(
667
    'Running Gradle task \'$assembleTask\'...',
668
    timeout: timeoutConfiguration.slowOperation,
669 670
    multilineOutput: true,
  );
671 672
  final String gradlePath = fs.file(gradle).absolute.path;
  final List<String> command = <String>[gradlePath];
673 674 675
  if (logger.isVerbose) {
    command.add('-Pverbose=true');
  } else {
676 677 678
    command.add('-q');
  }
  if (artifacts is LocalEngineArtifacts) {
679
    final LocalEngineArtifacts localEngineArtifacts = artifacts;
680 681 682
    printTrace('Using local engine: ${localEngineArtifacts.engineOutPath}');
    command.add('-PlocalEngineOut=${localEngineArtifacts.engineOutPath}');
  }
683 684 685
  if (target != null) {
    command.add('-Ptarget=$target');
  }
686 687
  assert(buildInfo.trackWidgetCreation != null);
  command.add('-Ptrack-widget-creation=${buildInfo.trackWidgetCreation}');
688 689 690 691 692 693 694 695
  if (buildInfo.extraFrontEndOptions != null)
    command.add('-Pextra-front-end-options=${buildInfo.extraFrontEndOptions}');
  if (buildInfo.extraGenSnapshotOptions != null)
    command.add('-Pextra-gen-snapshot-options=${buildInfo.extraGenSnapshotOptions}');
  if (buildInfo.fileSystemRoots != null && buildInfo.fileSystemRoots.isNotEmpty)
    command.add('-Pfilesystem-roots=${buildInfo.fileSystemRoots.join('|')}');
  if (buildInfo.fileSystemScheme != null)
    command.add('-Pfilesystem-scheme=${buildInfo.fileSystemScheme}');
696 697 698 699 700 701
  if (androidBuildInfo.splitPerAbi)
    command.add('-Psplit-per-abi=true');
  if (androidBuildInfo.targetArchs.isNotEmpty) {
    final String targetPlatforms = androidBuildInfo.targetArchs
        .map(getPlatformNameForAndroidArch).join(',');
    command.add('-Ptarget-platform=$targetPlatforms');
702
  }
703 704 705 706 707 708 709 710
  if (featureFlags.isPluginAsAarEnabled) {
     // Pass a system flag instead of a project flag, so this flag can be
     // read from include_flutter.groovy.
    command.add('-Dbuild-plugins-as-aars=true');
    if (!flutterProject.manifest.isModule) {
      command.add('--settings-file=settings_aar.gradle');
    }
  }
711
  command.add(assembleTask);
712
  bool potentialAndroidXFailure = false;
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  final Stopwatch sw = Stopwatch()..start();
  int exitCode = 1;
  try {
    exitCode = await runCommandAndStreamOutput(
      command,
      workingDirectory: flutterProject.android.hostAppGradleRoot.path,
      allowReentrantFlutter: true,
      environment: _gradleEnv,
      // TODO(mklim): if AndroidX warnings are no longer required, this
      // mapFunction and all its associated variabled can be replaced with just
      // `filter: ndkMessagefilter`.
      mapFunction: (String line) {
        final bool isAndroidXPluginWarning = androidXPluginWarningRegex.hasMatch(line);
        if (!isAndroidXPluginWarning && androidXFailureRegex.hasMatch(line)) {
          potentialAndroidXFailure = true;
        }
        // Always print the full line in verbose mode.
        if (logger.isVerbose) {
          return line;
        } else if (isAndroidXPluginWarning || !ndkMessageFilter.hasMatch(line)) {
          return null;
        }
735

736 737 738 739 740 741
        return line;
      },
    );
  } finally {
    status.stop();
  }
742

743 744 745 746 747 748
  if (exitCode != 0) {
    if (potentialAndroidXFailure) {
      printError('*******************************************************************************************');
      printError('The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.');
      printError('See https://goo.gl/CP92wY for more information on the problem and how to fix it.');
      printError('*******************************************************************************************');
749
      BuildEvent('android-x-failure').send();
750
    }
751
    throwToolExit('Gradle task $assembleTask failed with exit code $exitCode', exitCode: exitCode);
752
  }
753
  flutterUsage.sendTiming('build', 'gradle-v2', Duration(milliseconds: sw.elapsedMilliseconds));
754

755
  if (!isBuildingBundle) {
756
    final Iterable<File> apkFiles = findApkFiles(project, androidBuildInfo);
757
    if (apkFiles.isEmpty)
758
      throwToolExit('Gradle build failed to produce an Android package.');
759 760 761
    // Copy the first APK to app.apk, so `flutter run`, `flutter install`, etc. can find it.
    // TODO(blasten): Handle multiple APKs.
    apkFiles.first.copySync(project.apkDirectory.childFile('app.apk').path);
762

763 764
    printTrace('calculateSha: ${project.apkDirectory}/app.apk');
    final File apkShaFile = project.apkDirectory.childFile('app.apk.sha1');
765
    apkShaFile.writeAsStringSync(_calculateSha(apkFiles.first));
766

767 768 769 770 771 772 773 774 775
    for (File apkFile in apkFiles) {
      String appSize;
      if (buildInfo.mode == BuildMode.debug) {
        appSize = '';
      } else {
        appSize = ' (${getSizeAsMB(apkFile.lengthSync())})';
      }
      printStatus('Built ${fs.path.relative(apkFile.path)}$appSize.',
          color: TerminalColor.green);
776 777
    }
  } else {
778
    final File bundleFile = findBundleFile(project, buildInfo);
779 780 781 782 783 784 785 786 787
    if (bundleFile == null)
      throwToolExit('Gradle build failed to produce an Android bundle package.');

    String appSize;
    if (buildInfo.mode == BuildMode.debug) {
      appSize = '';
    } else {
      appSize = ' (${getSizeAsMB(bundleFile.lengthSync())})';
    }
788 789
    printStatus('Built ${fs.path.relative(bundleFile.path)}$appSize.',
        color: TerminalColor.green);
790
  }
791 792
}

793 794
@visibleForTesting
Iterable<File> findApkFiles(GradleProject project, AndroidBuildInfo androidBuildInfo) {
795 796 797 798
  final Iterable<String> apkFileNames = project.apkFilesFor(androidBuildInfo);
  if (apkFileNames.isEmpty)
    return const <File>[];

799
  return apkFileNames.expand<File>((String apkFileName) {
800
    File apkFile = project.apkDirectory.childFile(apkFileName);
801
    if (apkFile.existsSync())
802
      return <File>[apkFile];
803 804 805 806 807 808
    final BuildInfo buildInfo = androidBuildInfo.buildInfo;
    final String modeName = camelCase(buildInfo.modeName);
    apkFile = project.apkDirectory
        .childDirectory(modeName)
        .childFile(apkFileName);
    if (apkFile.existsSync())
809
      return <File>[apkFile];
810 811 812 813 814 815 816
    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())
817
        return <File>[apkFile];
818
    }
819
    return const <File>[];
820
  });
821
}
822

823 824
@visibleForTesting
File findBundleFile(GradleProject project, BuildInfo buildInfo) {
825
  final String bundleFileName = project.bundleFileFor(buildInfo);
826
  if (bundleFileName == null) {
827
    return null;
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  }
  File bundleFile = project.bundleDirectory
    .childDirectory(camelCase(buildInfo.modeName))
    .childFile(bundleFileName);
  if (bundleFile.existsSync()) {
    return bundleFile;
  }
  if (buildInfo.flavor == null) {
    return 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. On Windows, filenames aren't case sensitive.
  // For example: foo_barRelease where `foo_bar` is the flavor and `Release` the mode name.
  final String childDirName = '${buildInfo.flavor}${camelCase('_' + buildInfo.modeName)}';
  bundleFile = project.bundleDirectory
      .childDirectory(childDirName)
      .childFile(bundleFileName);
  if (bundleFile.existsSync()) {
846 847 848 849 850
    return bundleFile;
  }
  return null;
}

851
Map<String, String> get _gradleEnv {
852
  final Map<String, String> env = Map<String, String>.from(platform.environment);
853 854 855 856
  if (javaPath != null) {
    // Use java bundled with Android Studio.
    env['JAVA_HOME'] = javaPath;
  }
857 858 859
  // Don't log analytics for downstream Flutter commands.
  // e.g. `flutter build bundle`.
  env['FLUTTER_SUPPRESS_ANALYTICS'] = 'true';
860 861
  return env;
}
862 863

class GradleProject {
864 865 866 867 868
  GradleProject(
    this.buildTypes,
    this.productFlavors,
    this.buildDirectory,
  );
869

870
  factory GradleProject.fromAppProperties(String properties, String tasks) {
871
    // Extract build directory.
872
    final String buildDirectory = properties
873 874 875 876 877 878
        .split('\n')
        .firstWhere((String s) => s.startsWith('buildDir: '))
        .substring('buildDir: '.length)
        .trim();

    // Extract build types and product flavors.
879
    final Set<String> variants = <String>{};
880
    for (String s in tasks.split('\n')) {
881 882 883 884 885 886
      final Match match = _assembleTaskPattern.matchAsPrefix(s);
      if (match != null) {
        final String variant = match.group(1).toLowerCase();
        if (!variant.endsWith('test'))
          variants.add(variant);
      }
887
    }
888 889
    final Set<String> buildTypes = <String>{};
    final Set<String> productFlavors = <String>{};
890 891 892 893 894 895 896 897 898 899 900 901 902
    for (final String variant1 in variants) {
      for (final String variant2 in variants) {
        if (variant2.startsWith(variant1) && variant2 != variant1) {
          final String buildType = variant2.substring(variant1.length);
          if (variants.contains(buildType)) {
            buildTypes.add(buildType);
            productFlavors.add(variant1);
          }
        }
      }
    }
    if (productFlavors.isEmpty)
      buildTypes.addAll(variants);
903
    return GradleProject(
904 905 906 907
        buildTypes.toList(),
        productFlavors.toList(),
        buildDirectory,
      );
908 909
  }

910
  /// The build types such as [release] or [debug].
911
  final List<String> buildTypes;
912 913

  /// The product flavors defined in build.gradle.
914
  final List<String> productFlavors;
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933

  /// The build directory. This is typically <project>build/.
  String buildDirectory;

  /// The directory where the APK artifact is generated.
  Directory get apkDirectory {
    return fs.directory(fs.path.join(buildDirectory, 'outputs', 'apk'));
  }

  /// The directory where the app bundle artifact is generated.
  Directory get bundleDirectory {
    return fs.directory(fs.path.join(buildDirectory, 'outputs', 'bundle'));
  }

  /// The directory where the repo is generated.
  /// Only applicable to AARs.
  Directory get repoDirectory {
    return fs.directory(fs.path.join(buildDirectory, 'outputs', 'repo'));
  }
934 935

  String _buildTypeFor(BuildInfo buildInfo) {
936 937 938
    final String modeName = camelCase(buildInfo.modeName);
    if (buildTypes.contains(modeName.toLowerCase()))
      return modeName;
939 940 941 942 943 944
    return null;
  }

  String _productFlavorFor(BuildInfo buildInfo) {
    if (buildInfo.flavor == null)
      return productFlavors.isEmpty ? '' : null;
945 946
    else if (productFlavors.contains(buildInfo.flavor))
      return buildInfo.flavor;
947 948 949 950 951 952 953 954 955 956 957 958
    else
      return null;
  }

  String assembleTaskFor(BuildInfo buildInfo) {
    final String buildType = _buildTypeFor(buildInfo);
    final String productFlavor = _productFlavorFor(buildInfo);
    if (buildType == null || productFlavor == null)
      return null;
    return 'assemble${toTitleCase(productFlavor)}${toTitleCase(buildType)}';
  }

959 960 961
  Iterable<String> apkFilesFor(AndroidBuildInfo androidBuildInfo) {
    final String buildType = _buildTypeFor(androidBuildInfo.buildInfo);
    final String productFlavor = _productFlavorFor(androidBuildInfo.buildInfo);
962
    if (buildType == null || productFlavor == null)
963 964
      return const <String>[];

965
    final String flavorString = productFlavor.isEmpty ? '' : '-' + productFlavor;
966 967 968 969 970 971 972
    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'];
973
  }
974 975 976 977 978 979 980 981 982

  String bundleTaskFor(BuildInfo buildInfo) {
    final String buildType = _buildTypeFor(buildInfo);
    final String productFlavor = _productFlavorFor(buildInfo);
    if (buildType == null || productFlavor == null)
      return null;
    return 'bundle${toTitleCase(productFlavor)}${toTitleCase(buildType)}';
  }

983 984 985 986 987 988 989 990
  String aarTaskFor(BuildInfo buildInfo) {
    final String buildType = _buildTypeFor(buildInfo);
    final String productFlavor = _productFlavorFor(buildInfo);
    if (buildType == null || productFlavor == null)
      return null;
    return 'assembleAar${toTitleCase(productFlavor)}${toTitleCase(buildType)}';
  }

991 992 993 994 995
  String bundleFileFor(BuildInfo buildInfo) {
    // For app bundle all bundle names are called as app.aab. Product flavors
    // & build types are differentiated as folders, where the aab will be added.
    return 'app.aab';
  }
996
}