Unverified Commit fed35b4d authored by Gurjinder Partola's avatar Gurjinder Partola Committed by GitHub

[flutter_tools] Add support for versioned Android cmdline tools (#78253)

parent 389a3fa5
...@@ -261,23 +261,57 @@ class AndroidSdk { ...@@ -261,23 +261,57 @@ class AndroidSdk {
return null; return null;
} }
String getAvdManagerPath() { String getCmdlineToolsPath(String binaryName) {
final String binaryName = globals.platform.isWindows ? 'avdmanager.bat' : 'avdmanager'; // First look for the latest version of the command-line tools
final File cmdlineToolsBinary = directory final File cmdlineToolsLatestBinary = directory
.childDirectory('cmdline-tools') .childDirectory('cmdline-tools')
.childDirectory('latest') .childDirectory('latest')
.childDirectory('bin') .childDirectory('bin')
.childFile(binaryName); .childFile(binaryName);
if (cmdlineToolsBinary.existsSync()) { if (cmdlineToolsLatestBinary.existsSync()) {
return cmdlineToolsBinary.path; 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;
}
})
.where((Version version) => version != null)
.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;
}
}
} }
// Finally fallback to the old SDK tools
final File toolsBinary = directory.childDirectory('tools').childDirectory('bin').childFile(binaryName); final File toolsBinary = directory.childDirectory('tools').childDirectory('bin').childFile(binaryName);
if (toolsBinary.existsSync()) { if (toolsBinary.existsSync()) {
return toolsBinary.path; return toolsBinary.path;
} }
return null; return null;
} }
String getAvdManagerPath() => getCmdlineToolsPath(globals.platform.isWindows ? 'avdmanager.bat' : 'avdmanager');
/// Sets up various paths used internally. /// Sets up various paths used internally.
/// ///
/// This method should be called in a case where the tooling may have updated /// This method should be called in a case where the tooling may have updated
...@@ -354,14 +388,11 @@ class AndroidSdk { ...@@ -354,14 +388,11 @@ class AndroidSdk {
final String executable = globals.platform.isWindows final String executable = globals.platform.isWindows
? 'sdkmanager.bat' ? 'sdkmanager.bat'
: 'sdkmanager'; : 'sdkmanager';
final File cmdlineTool = directory final String path = getCmdlineToolsPath(executable);
.childDirectory('cmdline-tools') if (path != null) {
.childDirectory('latest') return path;
.childDirectory('bin')
.childFile(executable);
if (cmdlineTool.existsSync()) {
return cmdlineTool.path;
} }
// If no binary was found, return the default location
return directory return directory
.childDirectory('tools') .childDirectory('tools')
.childDirectory('bin') .childDirectory('bin')
......
...@@ -80,6 +80,26 @@ void main() { ...@@ -80,6 +80,26 @@ void main() {
Config: () => config, Config: () => config,
}); });
testUsingContext('returns sdkmanager path under cmdline tools (highest version) on Linux/macOS', () {
sdkDir = MockAndroidSdk.createSdkDirectory();
config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
final List<String> versions = <String>['3.0', '2.1', '1.0'];
for (final String version in versions) {
fileSystem.file(
fileSystem.path.join(sdk.directory.path, 'cmdline-tools', version, 'bin', 'sdkmanager')
).createSync(recursive: true);
}
expect(sdk.sdkManagerPath, fileSystem.path.join(sdk.directory.path, 'cmdline-tools', '3.0', 'bin', 'sdkmanager'));
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => FakePlatform(operatingSystem: 'linux'),
Config: () => config,
});
testUsingContext('Caches adb location after first access', () { testUsingContext('Caches adb location after first access', () {
sdkDir = MockAndroidSdk.createSdkDirectory(); sdkDir = MockAndroidSdk.createSdkDirectory();
config.setValue('android-sdk', sdkDir.path); config.setValue('android-sdk', sdkDir.path);
......
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