Unverified Commit 9654659c authored by Chris Bracken's avatar Chris Bracken Committed by GitHub

Add AndroidSdk.sdkManagerPath, sdkManagerVersion (#14247)

Convenience getters for the the path to the Android SDK manager and the
currently installed version of the tool.

Pre-factoring to support better checks around the --android-licenses
command, which uses a feature of the SDK manager that is unsupported in
older versions of the tool.
parent 032de5ff
......@@ -9,6 +9,7 @@ import 'package:meta/meta.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/io.dart' show ProcessResult;
import '../base/os.dart';
import '../base/platform.dart';
import '../base/process_manager.dart';
......@@ -285,6 +286,22 @@ class AndroidSdk {
_latestVersion = _sdkVersions.isEmpty ? null : _sdkVersions.last;
}
/// Returns the filesystem path of the Android SDK manager tool or null if not found.
String get sdkManagerPath {
return fs.path.join(directory, 'tools', 'bin', 'sdkmanager');
}
/// Returns the version of the Android SDK manager tool or null if not found.
String get sdkManagerVersion {
if (!processManager.canRun(sdkManagerPath))
throwToolExit('Android sdkmanager not found. Update to the latest Android SDK to resolve this.');
final ProcessResult result = processManager.runSync(<String>[sdkManagerPath, '--version']);
if (result.exitCode != 0) {
throwToolExit('sdkmanager --version failed: ${result.exitCode}', exitCode: result.exitCode);
}
return result.stdout.trim();
}
@override
String toString() => 'AndroidSdk: $directory';
}
......
......@@ -186,13 +186,9 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
fs.path.dirname(javaBinary) + os.pathVarSeparator + platform.environment['PATH'];
}
final String sdkManagerPath = fs.path.join(
androidSdk.directory, 'tools', 'bin',
platform.isWindows ? 'sdkmanager.bat' : 'sdkmanager',
);
final Process process = await runCommand(
<String>[sdkManagerPath, '--licenses'],
environment: sdkManagerEnv,
<String>[androidSdk.sdkManagerPath, '--licenses'],
environment: sdkManagerEnv,
);
waitGroup<Null>(<Future<Null>>[
......
......@@ -5,17 +5,25 @@
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart' show ProcessResult;
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/config.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:test/test.dart';
import '../src/common.dart';
import '../src/context.dart';
class MockProcessManager extends Mock implements ProcessManager {}
void main() {
MemoryFileSystem fs;
MockProcessManager processManager;
setUp(() {
fs = new MemoryFileSystem();
processManager = new MockProcessManager();
});
group('android_sdk AndroidSdk', () {
......@@ -48,6 +56,56 @@ void main() {
FileSystem: () => fs,
});
testUsingContext('returns sdkmanager path', () {
sdkDir = _createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
expect(sdk.sdkManagerPath, fs.path.join(sdk.directory, 'tools', 'bin', 'sdkmanager'));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
});
testUsingContext('returns sdkmanager version', () {
sdkDir = _createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
when(processManager.runSync(<String>[sdk.sdkManagerPath, '--version']))
.thenReturn(new ProcessResult(1, 0, '26.1.1\n', ''));
expect(sdk.sdkManagerVersion, '26.1.1');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => processManager,
});
testUsingContext('throws on sdkmanager version check failure', () {
sdkDir = _createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(true);
when(processManager.runSync(<String>[sdk.sdkManagerPath, '--version']))
.thenReturn(new ProcessResult(1, 1, '26.1.1\n', 'Mystery error'));
expect(() => sdk.sdkManagerVersion, throwsToolExit(exitCode: 1));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => processManager,
});
testUsingContext('throws on sdkmanager version check if sdkmanager not found', () {
sdkDir = _createSdkDirectory(withSdkManager: false);
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
when(processManager.canRun(sdk.sdkManagerPath)).thenReturn(false);
expect(() => sdk.sdkManagerVersion, throwsToolExit());
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => processManager,
});
group('ndk', () {
const <String, String>{
'linux': 'linux-x86_64',
......@@ -102,8 +160,12 @@ void main() {
});
}
Directory _createSdkDirectory(
{bool withAndroidN: false, String withNdkDir, bool withNdkSysroot: false}) {
Directory _createSdkDirectory({
bool withAndroidN: false,
String withNdkDir,
bool withNdkSysroot: false,
bool withSdkManager: true,
}) {
final Directory dir = fs.systemTempDirectory.createTempSync('android-sdk');
_createSdkFile(dir, 'platform-tools/adb');
......@@ -121,6 +183,9 @@ Directory _createSdkDirectory(
_createSdkFile(dir, 'platforms/android-N/build.prop', contents: _buildProp);
}
if (withSdkManager)
_createSdkFile(dir, 'tools/bin/sdkmanager');
if (withNdkDir != null) {
final String ndkCompiler = fs.path.join(
'ndk-bundle',
......
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