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

5
import '../base/common.dart';
6
import '../base/file_system.dart';
7
import '../base/os.dart';
8
import '../base/platform.dart';
9
import '../base/process.dart';
10
import '../base/version.dart';
11
import '../convert.dart';
12
import '../globals.dart' as globals;
13
import 'android_studio.dart';
14

15 16
// ANDROID_HOME is deprecated.
// See https://developer.android.com/studio/command-line/variables.html#envar
17
const String kAndroidHome = 'ANDROID_HOME';
18
const String kAndroidSdkRoot = 'ANDROID_SDK_ROOT';
19

20 21
final RegExp _numberedAndroidPlatformRe = RegExp(r'^android-([0-9]+)$');
final RegExp _sdkVersionRe = RegExp(r'^ro.build.version.sdk=([0-9]+)$');
22

23
// Android SDK layout:
24

25
// $ANDROID_SDK_ROOT/platform-tools/adb
26

27 28 29 30 31
// $ANDROID_SDK_ROOT/build-tools/19.1.0/aapt, dx, zipalign
// $ANDROID_SDK_ROOT/build-tools/22.0.1/aapt
// $ANDROID_SDK_ROOT/build-tools/23.0.2/aapt
// $ANDROID_SDK_ROOT/build-tools/24.0.0-preview/aapt
// $ANDROID_SDK_ROOT/build-tools/25.0.2/apksigner
32

33 34 35
// $ANDROID_SDK_ROOT/platforms/android-22/android.jar
// $ANDROID_SDK_ROOT/platforms/android-23/android.jar
// $ANDROID_SDK_ROOT/platforms/android-N/android.jar
36
class AndroidSdk {
37
  AndroidSdk(this.directory) {
38
    reinitialize();
39 40
  }

41 42
  static const String _javaHomeEnvironmentVariable = 'JAVA_HOME';
  static const String _javaExecutable = 'java';
43

44 45
  /// The Android SDK root directory.
  final Directory directory;
46

47 48
  List<AndroidSdkVersion> _sdkVersions = <AndroidSdkVersion>[];
  AndroidSdkVersion? _latestVersion;
49

50 51 52 53 54 55
  /// Whether the `cmdline-tools` directory exists in the Android SDK.
  ///
  /// This is required to use the newest SDK manager which only works with
  /// the newer JDK.
  bool get cmdlineToolsAvailable => directory.childDirectory('cmdline-tools').existsSync();

56
  /// Whether the `platform-tools` or `cmdline-tools` directory exists in the Android SDK.
57 58 59 60 61
  ///
  /// It is possible to have an Android SDK folder that is missing this with
  /// the expectation that it will be downloaded later, e.g. by gradle or the
  /// sdkmanager. The [licensesAvailable] property should be used to determine
  /// whether the licenses are at least possibly accepted.
62
  bool get platformToolsAvailable => cmdlineToolsAvailable
63
     || directory.childDirectory('platform-tools').existsSync();
64 65 66 67 68 69 70 71

  /// Whether the `licenses` directory exists in the Android SDK.
  ///
  /// The existence of this folder normally indicates that the SDK licenses have
  /// been accepted, e.g. via the sdkmanager, Android Studio, or by copying them
  /// from another workstation such as in CI scenarios. If these files are valid
  /// gradle or the sdkmanager will be able to download and use other parts of
  /// the SDK on demand.
72
  bool get licensesAvailable => directory.childDirectory('licenses').existsSync();
73

74 75 76
  static AndroidSdk? locateAndroidSdk() {
    String? findAndroidHomeDir() {
      String? androidHomeDir;
77
      if (globals.config.containsKey('android-sdk')) {
78
        androidHomeDir = globals.config.getValue('android-sdk') as String?;
79 80 81 82 83
      } else if (globals.platform.environment.containsKey(kAndroidHome)) {
        androidHomeDir = globals.platform.environment[kAndroidHome];
      } else if (globals.platform.environment.containsKey(kAndroidSdkRoot)) {
        androidHomeDir = globals.platform.environment[kAndroidSdkRoot];
      } else if (globals.platform.isLinux) {
84 85
        if (globals.fsUtils.homeDirPath != null) {
          androidHomeDir = globals.fs.path.join(
86
            globals.fsUtils.homeDirPath!,
87 88 89
            'Android',
            'Sdk',
          );
90
        }
91
      } else if (globals.platform.isMacOS) {
92 93
        if (globals.fsUtils.homeDirPath != null) {
          androidHomeDir = globals.fs.path.join(
94
            globals.fsUtils.homeDirPath!,
95 96 97 98
            'Library',
            'Android',
            'sdk',
          );
99
        }
100
      } else if (globals.platform.isWindows) {
101 102
        if (globals.fsUtils.homeDirPath != null) {
          androidHomeDir = globals.fs.path.join(
103
            globals.fsUtils.homeDirPath!,
104 105 106 107 108
            'AppData',
            'Local',
            'Android',
            'sdk',
          );
109
        }
110 111 112
      }

      if (androidHomeDir != null) {
113
        if (validSdkDirectory(androidHomeDir)) {
114
          return androidHomeDir;
115
        }
116 117
        if (validSdkDirectory(globals.fs.path.join(androidHomeDir, 'sdk'))) {
          return globals.fs.path.join(androidHomeDir, 'sdk');
118
        }
119 120 121
      }

      // in build-tools/$version/aapt
122
      final List<File> aaptBins = globals.os.whichAll('aapt');
123 124
      for (File aaptBin in aaptBins) {
        // Make sure we're using the aapt from the SDK.
125
        aaptBin = globals.fs.file(aaptBin.resolveSymbolicLinksSync());
126
        final String dir = aaptBin.parent.parent.parent.path;
127
        if (validSdkDirectory(dir)) {
128
          return dir;
129
        }
130 131 132
      }

      // in platform-tools/adb
133
      final List<File> adbBins = globals.os.whichAll('adb');
134 135
      for (File adbBin in adbBins) {
        // Make sure we're using the adb from the SDK.
136
        adbBin = globals.fs.file(adbBin.resolveSymbolicLinksSync());
137
        final String dir = adbBin.parent.parent.path;
138
        if (validSdkDirectory(dir)) {
139
          return dir;
140
        }
141 142 143 144 145
      }

      return null;
    }

146
    final String? androidHomeDir = findAndroidHomeDir();
147 148
    if (androidHomeDir == null) {
      // No dice.
149
      globals.printTrace('Unable to locate an Android SDK.');
150
      return null;
151 152
    }

153
    return AndroidSdk(globals.fs.directory(androidHomeDir));
154 155 156
  }

  static bool validSdkDirectory(String dir) {
Lau Ching Jun's avatar
Lau Ching Jun committed
157
    return sdkDirectoryHasLicenses(dir) || sdkDirectoryHasPlatformTools(dir);
158 159 160
  }

  static bool sdkDirectoryHasPlatformTools(String dir) {
161
    return globals.fs.isDirectorySync(globals.fs.path.join(dir, 'platform-tools'));
162 163
  }

Lau Ching Jun's avatar
Lau Ching Jun committed
164
  static bool sdkDirectoryHasLicenses(String dir) {
165
    return globals.fs.isDirectorySync(globals.fs.path.join(dir, 'licenses'));
166 167 168 169
  }

  List<AndroidSdkVersion> get sdkVersions => _sdkVersions;

170
  AndroidSdkVersion? get latestVersion => _latestVersion;
171

172
  late final String? adbPath = getPlatformToolsPath(globals.platform.isWindows ? 'adb.exe' : 'adb');
173

174
  String? get emulatorPath => getEmulatorPath();
175

176
  String? get avdManagerPath => getAvdManagerPath();
177

178
  /// Locate the path for storing AVD emulator images. Returns null if none found.
179 180 181
  String? getAvdPath() {
    final String? avdHome = globals.platform.environment['ANDROID_AVD_HOME'];
    final String? home = globals.platform.environment['HOME'];
182
    final List<String> searchPaths = <String>[
183 184 185 186
      if (avdHome != null)
        avdHome,
      if (home != null)
        globals.fs.path.join(home, '.android', 'avd'),
187 188 189
    ];

    if (globals.platform.isWindows) {
190 191
      final String? homeDrive = globals.platform.environment['HOMEDRIVE'];
      final String? homePath = globals.platform.environment['HOMEPATH'];
192 193 194 195 196 197 198 199 200

      if (homeDrive != null && homePath != null) {
        // Can't use path.join for HOMEDRIVE/HOMEPATH
        // https://github.com/dart-lang/path/issues/37
        final String home = homeDrive + homePath;
        searchPaths.add(globals.fs.path.join(home, '.android', 'avd'));
      }
    }

201
    for (final String searchPath in searchPaths) {
202 203 204 205 206
      if (globals.fs.directory(searchPath).existsSync()) {
        return searchPath;
      }
    }
    return null;
207 208
  }

209
  Directory get _platformsDir => directory.childDirectory('platforms');
210 211 212 213 214 215 216 217 218 219 220

  Iterable<Directory> get _platforms {
    Iterable<Directory> platforms = <Directory>[];
    if (_platformsDir.existsSync()) {
      platforms = _platformsDir
        .listSync()
        .whereType<Directory>();
    }
    return platforms;
  }

221 222
  /// Validate the Android SDK. This returns an empty list if there are no
  /// issues; otherwise, it returns a list of issues found.
223
  List<String> validateSdkWellFormed() {
224
    if (adbPath == null || !globals.processManager.canRun(adbPath)) {
225
      return <String>['Android SDK file not found: ${adbPath ?? 'adb'}.'];
226
    }
227

228 229 230 231 232 233 234 235 236 237 238 239
    if (sdkVersions.isEmpty || latestVersion == null) {
      final StringBuffer msg = StringBuffer('No valid Android SDK platforms found in ${_platformsDir.path}.');
      if (_platforms.isEmpty) {
        msg.write(' Directory was empty.');
      } else {
        msg.write(' Candidates were:\n');
        msg.write(_platforms
          .map((Directory dir) => '  - ${dir.basename}')
          .join('\n'));
      }
      return <String>[msg.toString()];
    }
240

241
    return latestVersion!.validateSdkWellFormed();
242 243
  }

244
  String? getPlatformToolsPath(String binaryName) {
245 246 247 248 249 250 251
    final File cmdlineToolsBinary = directory.childDirectory('cmdline-tools').childFile(binaryName);
    if (cmdlineToolsBinary.existsSync()) {
      return cmdlineToolsBinary.path;
    }
    final File platformToolBinary = directory.childDirectory('platform-tools').childFile(binaryName);
    if (platformToolBinary.existsSync()) {
      return platformToolBinary.path;
252
    }
253
    return null;
254 255
  }

256
  String? getEmulatorPath() {
257
    final String binaryName = globals.platform.isWindows ? 'emulator.exe' : 'emulator';
Danny Tuppeny's avatar
Danny Tuppeny committed
258 259 260 261
    // Emulator now lives inside "emulator" but used to live inside "tools" so
    // try both.
    final List<String> searchFolders = <String>['emulator', 'tools'];
    for (final String folder in searchFolders) {
262 263 264
      final File file = directory.childDirectory(folder).childFile(binaryName);
      if (file.existsSync()) {
        return file.path;
265
      }
Danny Tuppeny's avatar
Danny Tuppeny committed
266 267
    }
    return null;
268 269
  }

270
  String? getCmdlineToolsPath(String binaryName, {bool skipOldTools = false}) {
271 272
    // First look for the latest version of the command-line tools
    final File cmdlineToolsLatestBinary = directory
273 274 275 276
      .childDirectory('cmdline-tools')
      .childDirectory('latest')
      .childDirectory('bin')
      .childFile(binaryName);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    if (cmdlineToolsLatestBinary.existsSync()) {
      return cmdlineToolsLatestBinary.path;
    }

    // Next look for the highest version of the command-line tools
    final Directory cmdlineToolsDir = directory.childDirectory('cmdline-tools');
    if (cmdlineToolsDir.existsSync()) {
      final List<Version> cmdlineTools = cmdlineToolsDir
        .listSync()
        .whereType<Directory>()
        .map((Directory subDirectory) {
          try {
            return Version.parse(subDirectory.basename);
          } on Exception {
            return null;
          }
        })
294
        .whereType<Version>()
295 296 297 298 299 300 301 302 303 304 305 306 307
        .toList();
      cmdlineTools.sort();

      for (final Version cmdlineToolsVersion in cmdlineTools.reversed) {
        final File cmdlineToolsBinary = directory
          .childDirectory('cmdline-tools')
          .childDirectory(cmdlineToolsVersion.toString())
          .childDirectory('bin')
          .childFile(binaryName);
        if (cmdlineToolsBinary.existsSync()) {
          return cmdlineToolsBinary.path;
        }
      }
308
    }
309 310 311
    if (skipOldTools) {
      return null;
    }
312 313

    // Finally fallback to the old SDK tools
314 315 316
    final File toolsBinary = directory.childDirectory('tools').childDirectory('bin').childFile(binaryName);
    if (toolsBinary.existsSync()) {
      return toolsBinary.path;
317
    }
318

319 320 321
    return null;
  }

322
  String? getAvdManagerPath() => getCmdlineToolsPath(globals.platform.isWindows ? 'avdmanager.bat' : 'avdmanager');
323

324 325 326 327 328
  /// Sets up various paths used internally.
  ///
  /// This method should be called in a case where the tooling may have updated
  /// SDK artifacts, such as after running a gradle build.
  void reinitialize() {
329
    List<Version> buildTools = <Version>[]; // 19.1.0, 22.0.1, ...
330

331
    final Directory buildToolsDir = directory.childDirectory('build-tools');
332
    if (buildToolsDir.existsSync()) {
333
      buildTools = buildToolsDir
334 335 336
        .listSync()
        .map((FileSystemEntity entity) {
          try {
337
            return Version.parse(entity.basename);
338
          } on Exception {
339 340 341
            return null;
          }
        })
342
        .whereType<Version>()
343 344 345
        .toList();
    }

346
    // Match up platforms with the best corresponding build-tools.
347
    _sdkVersions = _platforms.map<AndroidSdkVersion?>((Directory platformDir) {
348
      final String platformName = platformDir.basename;
349
      int platformVersion;
350 351

      try {
352
        final Match? numberedVersion = _numberedAndroidPlatformRe.firstMatch(platformName);
353
        if (numberedVersion != null) {
354
          platformVersion = int.parse(numberedVersion.group(1)!);
355 356
        } else {
          final String buildProps = platformDir.childFile('build.prop').readAsStringSync();
357
          final String? versionString = const LineSplitter()
358
              .convert(buildProps)
359 360 361
              .map<RegExpMatch?>(_sdkVersionRe.firstMatch)
              .whereType<Match>()
              .first
362
              .group(1);
363 364 365
          if (versionString == null) {
            return null;
          }
366 367
          platformVersion = int.parse(versionString);
        }
368
      } on Exception {
369 370 371
        return null;
      }

372
      Version? buildToolsVersion = Version.primary(buildTools.where((Version version) {
373
        return version.major == platformVersion;
374 375
      }).toList());

376 377
      buildToolsVersion ??= Version.primary(buildTools);

378
      if (buildToolsVersion == null) {
379
        return null;
380
      }
381

382
      return AndroidSdkVersion._(
383
        this,
384 385 386
        sdkLevel: platformVersion,
        platformName: platformName,
        buildToolsVersion: buildToolsVersion,
387
        fileSystem: globals.fs,
388
      );
389
    }).whereType<AndroidSdkVersion>().toList();
390 391 392 393 394 395

    _sdkVersions.sort();

    _latestVersion = _sdkVersions.isEmpty ? null : _sdkVersions.last;
  }

396
  /// Returns the filesystem path of the Android SDK manager tool.
397
  String? get sdkManagerPath {
398 399 400
    final String executable = globals.platform.isWindows
      ? 'sdkmanager.bat'
      : 'sdkmanager';
401
    final String? path = getCmdlineToolsPath(executable, skipOldTools: true);
402 403
    if (path != null) {
      return path;
404
    }
405
    return null;
406 407
  }

408
  /// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
409 410 411 412 413
  static String? findJavaBinary({
    required AndroidStudio? androidStudio,
    required FileSystem fileSystem,
    required OperatingSystemUtils operatingSystemUtils,
    required Platform platform,
414 415
  }) {
    if (androidStudio?.javaPath != null) {
416
      return fileSystem.path.join(androidStudio!.javaPath!, 'bin', 'java');
417
    }
418

419
    final String? javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable];
420 421
    if (javaHomeEnv != null) {
      // Trust JAVA_HOME.
422
      return fileSystem.path.join(javaHomeEnv, 'bin', 'java');
423 424 425 426
    }

    // MacOS specific logic to avoid popping up a dialog window.
    // See: http://stackoverflow.com/questions/14292698/how-do-i-check-if-the-java-jdk-is-installed-on-mac.
427
    if (platform.isMacOS) {
428
      try {
429
        final String javaHomeOutput = globals.processUtils.runSync(
430
          <String>['/usr/libexec/java_home', '-v', '1.8'],
431 432 433
          throwOnError: true,
          hideStdout: true,
        ).stdout.trim();
434
        if (javaHomeOutput != null) {
435 436
          if ((javaHomeOutput != null) && (javaHomeOutput.isNotEmpty)) {
            final String javaHome = javaHomeOutput.split('\n').last.trim();
437
            return fileSystem.path.join(javaHome, 'bin', 'java');
438 439
          }
        }
440
      } on Exception { /* ignore */ }
441 442 443
    }

    // Fallback to PATH based lookup.
444
    return operatingSystemUtils.which(_javaExecutable)?.path;
445 446
  }

447
  Map<String, String>? _sdkManagerEnv;
448 449
  /// Returns an environment with the Java folder added to PATH for use in calling
  /// Java-based Android SDK commands such as sdkmanager and avdmanager.
450 451 452 453
  Map<String, String> get sdkManagerEnv {
    if (_sdkManagerEnv == null) {
      // If we can locate Java, then add it to the path used to run the Android SDK manager.
      _sdkManagerEnv = <String, String>{};
454
      final String? javaBinary = findJavaBinary(
455 456 457 458 459
        androidStudio: globals.androidStudio,
        fileSystem: globals.fs,
        operatingSystemUtils: globals.os,
        platform: globals.platform,
      );
460 461
      if (javaBinary != null && globals.platform.environment['PATH'] != null) {
        _sdkManagerEnv!['PATH'] = globals.fs.path.dirname(javaBinary) +
462
                                 globals.os.pathVarSeparator +
463
                                 globals.platform.environment['PATH']!;
464 465
      }
    }
466
    return _sdkManagerEnv!;
467 468
  }

469
  /// Returns the version of the Android SDK manager tool or null if not found.
470
  String? get sdkManagerVersion {
471 472 473 474 475
    if (sdkManagerPath == null || !globals.processManager.canRun(sdkManagerPath)) {
      throwToolExit(
        'Android sdkmanager not found. Update to the latest Android SDK and ensure that '
        'the cmdline-tools are installed to resolve this.'
      );
476
    }
477
    final RunResult result = globals.processUtils.runSync(
478
      <String>[sdkManagerPath!, '--version'],
479 480
      environment: sdkManagerEnv,
    );
481
    if (result.exitCode != 0) {
482
      globals.printTrace('sdkmanager --version failed: exitCode: ${result.exitCode} stdout: ${result.stdout} stderr: ${result.stderr}');
483
      return null;
484 485 486 487
    }
    return result.stdout.trim();
  }

488
  @override
489 490 491 492
  String toString() => 'AndroidSdk: $directory';
}

class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
493 494
  AndroidSdkVersion._(
    this.sdk, {
495 496 497 498
    required this.sdkLevel,
    required this.platformName,
    required this.buildToolsVersion,
    required FileSystem fileSystem,
499 500
  }) : assert(sdkLevel != null),
       assert(platformName != null),
501 502
       assert(buildToolsVersion != null),
       _fileSystem = fileSystem;
503 504

  final AndroidSdk sdk;
505 506
  final int sdkLevel;
  final String platformName;
507
  final Version buildToolsVersion;
508

509 510
  final FileSystem _fileSystem;

511
  String get buildToolsVersionName => buildToolsVersion.toString();
512 513 514

  String get androidJarPath => getPlatformsPath('android.jar');

515 516 517 518 519 520
  /// Return the path to the android application package tool.
  ///
  /// This is used to dump the xml in order to launch built android applications.
  ///
  /// See also:
  ///   * [AndroidApk.fromApk], which depends on this to determine application identifiers.
521 522
  String get aaptPath => getBuildToolsPath('aapt');

523
  List<String> validateSdkWellFormed() {
524 525 526
    final String? existsAndroidJarPath = _exists(androidJarPath);
    if (existsAndroidJarPath != null) {
      return <String>[existsAndroidJarPath];
527
    }
528

529 530 531
    final String? canRunAaptPath = _canRun(aaptPath);
    if (canRunAaptPath != null) {
      return <String>[canRunAaptPath];
532
    }
533 534

    return <String>[];
535 536 537
  }

  String getPlatformsPath(String itemName) {
538
    return sdk.directory.childDirectory('platforms').childDirectory(platformName).childFile(itemName).path;
539 540
  }

541
  String getBuildToolsPath(String binaryName) {
542
    return sdk.directory.childDirectory('build-tools').childDirectory(buildToolsVersionName).childFile(binaryName).path;
543 544
  }

545
  @override
546
  int compareTo(AndroidSdkVersion other) => sdkLevel - other.sdkLevel;
547

548
  @override
549
  String toString() => '[${sdk.directory}, SDK version $sdkLevel, build-tools $buildToolsVersionName]';
550

551
  String? _exists(String path) {
552
    if (!_fileSystem.isFileSync(path)) {
553
      return 'Android SDK file not found: $path.';
554
    }
555
    return null;
556
  }
557

558
  String? _canRun(String path) {
559
    if (!globals.processManager.canRun(path)) {
560
      return 'Android SDK file not found: $path.';
561
    }
562 563
    return null;
  }
564
}