Commit 99b70dab authored by Devon Carew's avatar Devon Carew

handle android n in the android sdk

parent ca8dbe90
...@@ -11,16 +11,22 @@ import '../base/os.dart'; ...@@ -11,16 +11,22 @@ import '../base/os.dart';
import '../globals.dart'; import '../globals.dart';
// Android SDK layout: // Android SDK layout:
//
// $ANDROID_HOME/platform-tools/adb // $ANDROID_HOME/platform-tools/adb
// $ANDROID_HOME/build-tools/19.1.0/aapt, dx, zipalign // $ANDROID_HOME/build-tools/19.1.0/aapt, dx, zipalign
// $ANDROID_HOME/build-tools/22.0.1/aapt // $ANDROID_HOME/build-tools/22.0.1/aapt
// $ANDROID_HOME/build-tools/23.0.2/aapt // $ANDROID_HOME/build-tools/23.0.2/aapt
// $ANDROID_HOME/build-tools/24.0.0-preview/aapt
// $ANDROID_HOME/platforms/android-22/android.jar // $ANDROID_HOME/platforms/android-22/android.jar
// $ANDROID_HOME/platforms/android-23/android.jar // $ANDROID_HOME/platforms/android-23/android.jar
// $ANDROID_HOME/platforms/android-N/android.jar
// TODO(devoncarew): We need a way to locate the Android SDK w/o using an environment variable. // Special case some version names in the sdk.
// Perhaps something like `flutter config --android-home=foo/bar`. const Map<String, int> _namedVersionMap = const <String, int> {
'android-N': 24
};
/// Locate ADB. Prefer to use one from an Android SDK, if we can locate that. /// Locate ADB. Prefer to use one from an Android SDK, if we can locate that.
String getAdbPath([AndroidSdk existingSdk]) { String getAdbPath([AndroidSdk existingSdk]) {
...@@ -47,8 +53,6 @@ class AndroidSdk { ...@@ -47,8 +53,6 @@ class AndroidSdk {
AndroidSdkVersion _latestVersion; AndroidSdkVersion _latestVersion;
static AndroidSdk locateAndroidSdk() { static AndroidSdk locateAndroidSdk() {
// TODO: Use explicit configuration information from a metadata file?
String androidHomeDir; String androidHomeDir;
if (Platform.environment.containsKey('ANDROID_HOME')) { if (Platform.environment.containsKey('ANDROID_HOME')) {
androidHomeDir = Platform.environment['ANDROID_HOME']; androidHomeDir = Platform.environment['ANDROID_HOME'];
...@@ -130,11 +134,11 @@ class AndroidSdk { ...@@ -130,11 +134,11 @@ class AndroidSdk {
.toList(); .toList();
} }
List<Version> buildToolsVersions = <Version>[]; // 19.1.0, 22.0.1, ... List<Version> buildTools = <Version>[]; // 19.1.0, 22.0.1, ...
Directory buildToolsDir = new Directory(path.join(directory, 'build-tools')); Directory buildToolsDir = new Directory(path.join(directory, 'build-tools'));
if (buildToolsDir.existsSync()) { if (buildToolsDir.existsSync()) {
buildToolsVersions = buildToolsDir buildTools = buildToolsDir
.listSync() .listSync()
.map((FileSystemEntity entity) { .map((FileSystemEntity entity) {
try { try {
...@@ -149,25 +153,32 @@ class AndroidSdk { ...@@ -149,25 +153,32 @@ class AndroidSdk {
// Here we match up platforms with cooresponding build-tools. If we don't // Here we match up platforms with cooresponding build-tools. If we don't
// have a match, we don't return anything for that platform version. So if // have a match, we don't return anything for that platform version. So if
// the user only have 'android-22' and 'build-tools/19.0.0', we don't find // the user only has 'android-22' and 'build-tools/19.0.0', we don't find
// an Android sdk. // an Android sdk.
_sdkVersions = platforms.map((String platform) { _sdkVersions = platforms.map((String platformName) {
int sdkVersion; int platformVersion;
try { try {
sdkVersion = int.parse(platform.substring('android-'.length)); if (_namedVersionMap.containsKey(platformName))
platformVersion = _namedVersionMap[platformName];
else
platformVersion = int.parse(platformName.substring('android-'.length));
} catch (error) { } catch (error) {
return null; return null;
} }
Version buildToolsVersion = Version.primary(buildToolsVersions.where((Version version) { Version buildToolsVersion = Version.primary(buildTools.where((Version version) {
return version.major == sdkVersion; return version.major == platformVersion;
}).toList()); }).toList());
if (buildToolsVersion == null) if (buildToolsVersion == null)
return null; return null;
return new AndroidSdkVersion(this, platform, buildToolsVersion.toString()); return new AndroidSdkVersion(
this,
platformVersionName: platformName,
buildToolsVersionName: buildToolsVersion.toString()
);
}).where((AndroidSdkVersion version) => version != null).toList(); }).where((AndroidSdkVersion version) => version != null).toList();
_sdkVersions.sort(); _sdkVersions.sort();
...@@ -180,13 +191,21 @@ class AndroidSdk { ...@@ -180,13 +191,21 @@ class AndroidSdk {
} }
class AndroidSdkVersion implements Comparable<AndroidSdkVersion> { class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
AndroidSdkVersion(this.sdk, this.androidVersion, this.buildToolsVersion); AndroidSdkVersion(this.sdk, {
this.platformVersionName,
this.buildToolsVersionName
});
final AndroidSdk sdk; final AndroidSdk sdk;
final String androidVersion; final String platformVersionName;
final String buildToolsVersion; final String buildToolsVersionName;
int get sdkLevel => int.parse(androidVersion.substring('android-'.length)); int get sdkLevel {
if (_namedVersionMap.containsKey(platformVersionName))
return _namedVersionMap[platformVersionName];
else
return int.parse(platformVersionName.substring('android-'.length));
}
String get androidJarPath => getPlatformsPath('android.jar'); String get androidJarPath => getPlatformsPath('android.jar');
...@@ -205,20 +224,18 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> { ...@@ -205,20 +224,18 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
} }
String getPlatformsPath(String itemName) { String getPlatformsPath(String itemName) {
return path.join(sdk.directory, 'platforms', androidVersion, itemName); return path.join(sdk.directory, 'platforms', platformVersionName, itemName);
} }
String getBuildToolsPath(String binaryName) { String getBuildToolsPath(String binaryName) {
return path.join(sdk.directory, 'build-tools', buildToolsVersion, binaryName); return path.join(sdk.directory, 'build-tools', buildToolsVersionName, binaryName);
} }
@override @override
int compareTo(AndroidSdkVersion other) { int compareTo(AndroidSdkVersion other) => sdkLevel - other.sdkLevel;
return sdkLevel - other.sdkLevel;
}
@override @override
String toString() => '[${sdk.directory}, SDK version $sdkLevel, build-tools $buildToolsVersion]'; String toString() => '[${sdk.directory}, SDK version $sdkLevel, build-tools $buildToolsVersionName]';
bool _exists(String path, { bool complain: false }) { bool _exists(String path, { bool complain: false }) {
if (!FileSystemEntity.isFileSync(path)) { if (!FileSystemEntity.isFileSync(path)) {
......
// 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:io';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'src/context.dart';
void main() {
group('android_sdk AndroidSdk', () {
Directory sdkDir;
tearDown(() {
sdkDir?.deleteSync(recursive: true);
});
testUsingContext('parse sdk', () {
sdkDir = _createSdkDirectory();
AndroidSdk sdk = new AndroidSdk(sdkDir.path);
expect(sdk.latestVersion, isNotNull);
expect(sdk.latestVersion.sdkLevel, 23);
});
testUsingContext('parse sdk N', () {
sdkDir = _createSdkDirectory(withAndroidN: true);
AndroidSdk sdk = new AndroidSdk(sdkDir.path);
expect(sdk.latestVersion, isNotNull);
expect(sdk.latestVersion.sdkLevel, 24);
});
});
group('android_sdk AndroidSdkVersion', () {
testUsingContext('parse normal', () {
AndroidSdk sdk = new AndroidSdk('.');
AndroidSdkVersion ver = new AndroidSdkVersion(sdk,
platformVersionName: 'android-23', buildToolsVersionName: '23.0.0');
expect(ver.sdkLevel, 23);
});
testUsingContext('parse android n', () {
AndroidSdk sdk = new AndroidSdk('.');
AndroidSdkVersion ver = new AndroidSdkVersion(sdk,
platformVersionName: 'android-N', buildToolsVersionName: '24.0.0');
expect(ver.sdkLevel, 24);
});
});
}
Directory _createSdkDirectory({ bool withAndroidN: false }) {
Directory dir = Directory.systemTemp.createTempSync('android-sdk');
_createSdkFile(dir, 'platform-tools/adb');
_createSdkFile(dir, 'build-tools/19.1.0/aapt');
_createSdkFile(dir, 'build-tools/22.0.1/aapt');
_createSdkFile(dir, 'build-tools/23.0.2/aapt');
if (withAndroidN)
_createSdkFile(dir, 'build-tools/24.0.0-preview/aapt');
_createSdkFile(dir, 'platforms/android-22/android.jar');
_createSdkFile(dir, 'platforms/android-23/android.jar');
if (withAndroidN)
_createSdkFile(dir, 'platforms/android-N/android.jar');
return dir;
}
void _createSdkFile(Directory dir, String filePath) {
File file = new File(path.join(dir.path, filePath));
file.createSync(recursive: true);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment