gradle_errors.dart 16.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

7 8
import '../base/error_handling_io.dart';
import '../base/file_system.dart';
9
import '../base/process.dart';
10
import '../globals_null_migrated.dart' as globals;
11 12
import '../project.dart';
import '../reporting/reporting.dart';
13
import 'android_studio.dart';
14 15 16 17

typedef GradleErrorTest = bool Function(String);

/// A Gradle error handled by the tool.
18
class GradleHandledError {
19
  const GradleHandledError({
20 21
    required this.test,
    required this.handler,
22 23 24 25 26 27 28 29 30
    this.eventLabel,
  });

  /// The test function.
  /// Returns [true] if the current error message should be handled.
  final GradleErrorTest test;

  /// The handler function.
  final Future<GradleBuildStatus> Function({
31 32 33 34
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
35 36
  }) handler;

37
  /// The [BuildEvent] label is named gradle-[eventLabel].
38 39
  /// If not empty, the build event is logged along with
  /// additional metadata such as the attempt number.
40
  final String? eventLabel;
41 42 43
}

/// The status of the Gradle build.
44
enum GradleBuildStatus {
45 46 47 48 49 50 51 52
  /// The tool cannot recover from the failure and should exit.
  exit,
  /// The tool can retry the exact same build.
  retry,
  /// The tool can build the plugins as AAR and retry the build.
  retryWithAarPlugins,
}

53 54
/// Returns a simple test function that evaluates to `true` if at least one of
/// `errorMessages` is contained in the error message.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
GradleErrorTest _lineMatcher(List<String> errorMessages) {
  return (String line) {
    return errorMessages.any((String errorMessage) => line.contains(errorMessage));
  };
}

/// The list of Gradle errors that the tool can handle.
///
/// The handlers are executed in the order in which they appear in the list.
///
/// Only the first error handler for which the [test] function returns [true]
/// is handled. As a result, sort error handlers based on how strict the [test]
/// function is to eliminate false positives.
final List<GradleHandledError> gradleErrors = <GradleHandledError>[
  licenseNotAcceptedHandler,
  networkErrorHandler,
  permissionDeniedErrorHandler,
  flavorUndefinedHandler,
  r8FailureHandler,
74
  minSdkVersion,
75
  transformInputIssue,
76
  lockFileDepMissing,
77
  androidXFailureHandler, // Keep last since the pattern is broader.
78 79 80 81 82 83 84 85 86
];

// Permission defined error message.
@visibleForTesting
final GradleHandledError permissionDeniedErrorHandler = GradleHandledError(
  test: _lineMatcher(const <String>[
    'Permission denied',
  ]),
  handler: ({
87 88 89 90
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
91
  }) async {
92
    globals.printStatus('${globals.logger.terminal.warningMark} Gradle does not have execution permission.', emphasis: true);
93
    globals.printStatus(
94 95 96 97 98 99 100 101 102
      'You should change the ownership of the project directory to your user, '
      'or move the project to a directory with execute permissions.',
      indent: 4
    );
    return GradleBuildStatus.exit;
  },
  eventLabel: 'permission-denied',
);

103 104 105 106 107 108 109 110
/// Gradle crashes for several known reasons when downloading that are not
/// actionable by Flutter.
///
/// The Gradle cache directory must be deleted, otherwise it may attempt to
/// re-use the bad zip file.
///
/// See also:
///  * https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home
111 112 113 114 115 116 117 118 119 120
@visibleForTesting
final GradleHandledError networkErrorHandler = GradleHandledError(
  test: _lineMatcher(const <String>[
    'java.io.FileNotFoundException: https://downloads.gradle.org',
    'java.io.IOException: Unable to tunnel through proxy',
    'java.lang.RuntimeException: Timeout of',
    'java.util.zip.ZipException: error in opening zip file',
    'javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake',
    'java.net.SocketException: Connection reset',
    'java.io.FileNotFoundException',
121
    "> Could not get resource 'http",
122 123
  ]),
  handler: ({
124 125 126 127
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
128
  }) async {
129
    globals.printError(
130
      '${globals.logger.terminal.warningMark} Gradle threw an error while downloading artifacts from the network. '
131
      'Retrying to download...'
132
    );
133
    try {
134
      final String? homeDir = globals.platform.environment['HOME'];
135 136 137 138 139 140 141
      if (homeDir != null) {
        final Directory directory = globals.fs.directory(globals.fs.path.join(homeDir, '.gradle'));
        ErrorHandlingFileSystem.deleteIfExists(directory, recursive: true);
      }
    } on FileSystemException catch (err) {
      globals.printTrace('Failed to delete Gradle cache: $err');
    }
142 143 144 145 146 147 148 149 150 151 152 153
    return GradleBuildStatus.retry;
  },
  eventLabel: 'network',
);

// R8 failure.
@visibleForTesting
final GradleHandledError r8FailureHandler = GradleHandledError(
  test: _lineMatcher(const <String>[
    'com.android.tools.r8',
  ]),
  handler: ({
154 155 156 157
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
158
  }) async {
159
    globals.printStatus('${globals.logger.terminal.warningMark} The shrinker may have failed to optimize the Java bytecode.', emphasis: true);
160 161
    globals.printStatus('To disable the shrinker, pass the `--no-shrink` flag to this command.', indent: 4);
    globals.printStatus('To learn more, see: https://developer.android.com/studio/build/shrink-code', indent: 4);
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 188 189 190 191 192
    return GradleBuildStatus.exit;
  },
  eventLabel: 'r8',
);

// AndroidX failure.
//
// 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.');

@visibleForTesting
final GradleHandledError androidXFailureHandler = GradleHandledError(
  test: (String line) {
    return !androidXPluginWarningRegex.hasMatch(line) &&
           _androidXFailureRegex.hasMatch(line);
  },
  handler: ({
193 194 195 196
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
197 198 199 200 201 202
  }) async {
    final bool hasPlugins = project.flutterPluginsFile.existsSync();
    if (!hasPlugins) {
      // If the app doesn't use any plugin, then it's unclear where
      // the incompatibility is coming from.
      BuildEvent(
203
        'gradle-android-x-failure',
204
        type: 'gradle',
205
        eventError: 'app-not-using-plugins',
206
        flutterUsage: globals.flutterUsage,
207 208 209 210 211
      ).send();
    }
    if (hasPlugins && !usesAndroidX) {
      // If the app isn't using AndroidX, then the app is likely using
      // a plugin already migrated to AndroidX.
212
      globals.printStatus(
213
        'AndroidX incompatibilities may have caused this build to fail. '
214
        'Please migrate your app to AndroidX. See https://goo.gl/CP92wY .'
215 216
      );
      BuildEvent(
217
        'gradle-android-x-failure',
218
        type: 'gradle',
219
        eventError: 'app-not-using-androidx',
220
        flutterUsage: globals.flutterUsage,
221 222 223 224 225 226 227
      ).send();
    }
    if (hasPlugins && usesAndroidX && shouldBuildPluginAsAar) {
      // This is a dependency conflict instead of an AndroidX failure since
      // by this point the app is using AndroidX, the plugins are built as
      // AARs, Jetifier translated Support libraries for AndroidX equivalents.
      BuildEvent(
228
        'gradle-android-x-failure',
229
        type: 'gradle',
230
        eventError: 'using-jetifier',
231
        flutterUsage: globals.flutterUsage,
232 233 234
      ).send();
    }
    if (hasPlugins && usesAndroidX && !shouldBuildPluginAsAar) {
235
      globals.printStatus(
236 237
        'The build failed likely due to AndroidX incompatibilities in a plugin. '
        'The tool is about to try using Jetifier to solve the incompatibility.'
238 239
      );
      BuildEvent(
240
        'gradle-android-x-failure',
241
        type: 'gradle',
242
        eventError: 'not-using-jetifier',
243
        flutterUsage: globals.flutterUsage,
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
      ).send();
      return GradleBuildStatus.retryWithAarPlugins;
    }
    return GradleBuildStatus.exit;
  },
  eventLabel: 'android-x',
);

/// 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.
@visibleForTesting
final GradleHandledError licenseNotAcceptedHandler = GradleHandledError(
  test: _lineMatcher(const <String>[
    'You have not accepted the license agreements of the following SDK components',
  ]),
  handler: ({
261 262 263 264
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
265 266
  }) async {
    const String licenseNotAcceptedMatcher =
267
      r'You have not accepted the license agreements of the following SDK components:\s*\[(.+)\]';
268 269 270

    final RegExp licenseFailure = RegExp(licenseNotAcceptedMatcher, multiLine: true);
    assert(licenseFailure != null);
271
    final Match? licenseMatch = licenseFailure.firstMatch(line);
272
    globals.printStatus(
273
      '${globals.logger.terminal.warningMark} Unable to download needed Android SDK components, as the '
274
      'following licenses have not been accepted:\n'
275
      '${licenseMatch?.group(1)}\n\n'
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
      'To resolve this, please run the following command in a Terminal:\n'
      'flutter doctor --android-licenses'
    );
    return GradleBuildStatus.exit;
  },
  eventLabel: 'license-not-accepted',
);

final RegExp _undefinedTaskPattern = RegExp(r'Task .+ not found in root project.');

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

/// Handler when a flavor is undefined.
@visibleForTesting
final GradleHandledError flavorUndefinedHandler = GradleHandledError(
  test: (String line) {
    return _undefinedTaskPattern.hasMatch(line);
  },
  handler: ({
295 296 297 298
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
299
  }) async {
300
    final RunResult tasksRunResult = await globals.processUtils.run(
301
      <String>[
302
        globals.gradleUtils!.getExecutable(project),
303 304 305 306 307 308
        'app:tasks' ,
        '--all',
        '--console=auto',
      ],
      throwOnError: true,
      workingDirectory: project.android.hostAppGradleRoot.path,
309 310
      environment: <String, String>{
        if (javaPath != null)
311
          'JAVA_HOME': javaPath!,
312
      },
313 314 315
    );
    // Extract build types and product flavors.
    final Set<String> variants = <String>{};
316
    for (final String task in tasksRunResult.stdout.split('\n')) {
317
      final Match? match = _assembleTaskPattern.matchAsPrefix(task);
318
      if (match != null) {
319
        final String variant = match.group(1)!.toLowerCase();
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        if (!variant.endsWith('test')) {
          variants.add(variant);
        }
      }
    }
    final Set<String> productFlavors = <String>{};
    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)) {
            productFlavors.add(variant1);
          }
        }
      }
    }
336
    globals.printStatus(
337
      '\n${globals.logger.terminal.warningMark}  Gradle project does not define a task suitable '
338 339 340
      'for the requested build.'
    );
    if (productFlavors.isEmpty) {
341
      globals.printStatus(
342 343 344 345 346
        'The android/app/build.gradle file does not define '
        'any custom product flavors. '
        'You cannot use the --flavor option.'
      );
    } else {
347
      globals.printStatus(
348 349 350 351 352 353 354 355 356
        'The android/app/build.gradle file defines product '
        'flavors: ${productFlavors.join(', ')} '
        'You must specify a --flavor option to select one of them.'
      );
    }
    return GradleBuildStatus.exit;
  },
  eventLabel: 'flavor-undefined',
);
357 358 359 360 361 362 363 364 365 366 367


final RegExp _minSdkVersionPattern = RegExp(r'uses-sdk:minSdkVersion ([0-9]+) cannot be smaller than version ([0-9]+) declared in library \[\:(.+)\]');

/// Handler when a plugin requires a higher Android API level.
@visibleForTesting
final GradleHandledError minSdkVersion = GradleHandledError(
  test: (String line) {
    return _minSdkVersionPattern.hasMatch(line);
  },
  handler: ({
368 369 370 371
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
372 373 374 375 376 377
  }) async {
    final File gradleFile = project.directory
        .childDirectory('android')
        .childDirectory('app')
        .childFile('build.gradle');

378 379
    final Match? minSdkVersionMatch = _minSdkVersionPattern.firstMatch(line);
    assert(minSdkVersionMatch?.groupCount == 3);
380

381 382 383 384
    final String bold = globals.logger.terminal.bolden(
      'Fix this issue by adding the following to the file ${gradleFile.path}:\n'
      'android {\n'
      '  defaultConfig {\n'
385
      '    minSdkVersion ${minSdkVersionMatch?.group(2)}\n'
386 387 388
      '  }\n'
      '}\n'
    );
389
    globals.printStatus(
390
      '\n'
391
      'The plugin ${minSdkVersionMatch?.group(3)} requires a higher Android SDK version.\n'
392
      '$bold\n'
393
      "Note that your app won't be available to users running Android SDKs below ${minSdkVersionMatch?.group(2)}.\n"
394 395 396 397 398 399
      'Alternatively, try to find a version of this plugin that supports these lower versions of the Android SDK.'
    );
    return GradleBuildStatus.exit;
  },
  eventLabel: 'plugin-min-sdk',
);
400 401 402 403 404 405 406 407 408

/// Handler when https://issuetracker.google.com/issues/141126614 or
/// https://github.com/flutter/flutter/issues/58247 is triggered.
@visibleForTesting
final GradleHandledError transformInputIssue = GradleHandledError(
  test: (String line) {
    return line.contains('https://issuetracker.google.com/issues/158753935');
  },
  handler: ({
409 410 411 412
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
413 414 415 416 417
  }) async {
    final File gradleFile = project.directory
        .childDirectory('android')
        .childDirectory('app')
        .childFile('build.gradle');
418 419 420 421 422 423 424 425
    final String bold = globals.logger.terminal.bolden(
      'Fix this issue by adding the following to the file ${gradleFile.path}:\n'
      'android {\n'
      '  lintOptions {\n'
      '    checkReleaseBuilds false\n'
      '  }\n'
      '}'
    );
426
    globals.printStatus(
427 428 429
      '\n'
      'This issue appears to be https://github.com/flutter/flutter/issues/58247.\n'
      '$bold'
430 431 432 433 434
    );
    return GradleBuildStatus.exit;
  },
  eventLabel: 'transform-input-issue',
);
435 436 437 438 439 440 441 442

/// Handler when a dependency is missing in the lockfile.
@visibleForTesting
final GradleHandledError lockFileDepMissing = GradleHandledError(
  test: (String line) {
    return line.contains('which is not part of the dependency lock state');
  },
  handler: ({
443 444 445 446
    required String line,
    required FlutterProject project,
    required bool usesAndroidX,
    required bool shouldBuildPluginAsAar,
447 448 449 450
  }) async {
    final File gradleFile = project.directory
        .childDirectory('android')
        .childFile('build.gradle');
451 452 453 454
    final String bold = globals.logger.terminal.bolden(
      'To regenerate the lockfiles run: `./gradlew :generateLockfiles` in ${gradleFile.path}\n'
      'To remove dependency locking, remove the `dependencyLocking` from ${gradleFile.path}\n'
    );
455
    globals.printStatus(
456 457 458
      '\n'
      'You need to update the lockfile, or disable Gradle dependency locking.\n'
      '$bold'
459 460 461 462 463
    );
    return GradleBuildStatus.exit;
  },
  eventLabel: 'lock-dep-issue',
);