Skip to content

Commit 9d0f24e

Browse files
ik020gmackall
andauthored
Fix Android license detection for cmdline-tools 23.0+ (flutter#191554)
## Description Fixes flutter#191487 Android SDK cmdline-tools 23.0 deprecated `sdkmanager --licenses` in favor of the new `android` CLI. Running `sdkmanager --licenses` now only prints a deprecation warning instead of the parseable license summary Flutter previously relied on: ``` WARNING: The SDK Manager CLI tool (sdkmanager) is deprecated. Android CLI will be used instead. The 'android' binary can also be found in the cmdline-tools directory, and 'android sdk' is the replacement for 'sdkmanager'. To learn more about the Android CLI and how to use it, see the documentation (https://d.android.com/tools/agents/android-cli) Warning: The --licenses option is no longer needed. ``` None of the existing regexes in `licensesAccepted` match this output, so `flutter doctor` incorrectly reports `Android license status unknown` even when licenses are actually accepted. ## Approach I initially considered treating the new deprecation message as `LicensesAccepted.all`, but testing against a real cmdline-tools 23.0 install showed this message prints identically whether or not the licenses directory exists — so it can't be trusted as a signal of actual license state. I verified this by temporarily renaming my `<sdk>/licenses` directory and re-running `sdkmanager --licenses`; the output was byte-for-byte identical either way. I also checked the new `android` CLI's `--help` output (`sdk`, `sdk install`, `info`) — there's no license-status subcommand or flag exposed in this version, so there's no better structured signal to query instead. Since the SDK's actual license acceptance state is still tracked on disk under `<sdk>/licenses/` (this is presumably what Gradle checks directly, since Gradle builds still succeed after this cmdline-tools update), this PR adds a narrow, additive fallback: - When `sdkmanager`'s output matches the specific new-CLI deprecation message (and only then — not for arbitrary unparseable/garbage output), `licensesAccepted` checks the `licenses/` directory on disk directly instead of trusting stdout. - All existing regex-based parsing for older `sdkmanager` versions is untouched. ## Known limitation This fallback can only distinguish "some license files are present and non-empty" (→ `all`) from "no license files at all" (→ `none`). It cannot reproduce the previous `LicensesAccepted.some` state for the new CLI, since that information is no longer exposed by the tool in any form I could find. Happy to adjust the approach if maintainers have a preference here. ## Tests Added two regression tests reproducing the exact real-world output captured from a Windows machine running cmdline-tools 23.0: - `licensesAccepted falls back to the licenses directory when sdkmanager output is unparseable (new Android CLI, licenses present)` - `licensesAccepted falls back to none when sdkmanager output is unparseable and no licenses are present (new Android CLI, licenses missing)` All existing tests in `android_workflow_test.dart` continue to pass (35/35). Also manually verified end-to-end: `flutter doctor -v` on the affected machine now correctly reports `All Android licenses accepted.` instead of `Android license status unknown.` --------- Co-authored-by: Gray Mackall <[email protected]>
1 parent 625c5c9 commit 9d0f24e

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎packages/flutter_tools/lib/src/android/android_workflow.dart‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ enum LicensesAccepted { none, some, all, unknown }
3333
final licenseCounts = RegExp(r'(\d+) of (\d+) SDK package licenses? not accepted.');
3434
final licenseNotAccepted = RegExp(r'licenses? not accepted', caseSensitive: false);
3535
final licenseAccepted = RegExp(r'All SDK package licenses accepted.');
36+
final licensesNoLongerNeeded = RegExp(
37+
r'--licenses option is no longer needed',
38+
caseSensitive: false,
39+
);
3640

3741
class AndroidWorkflow implements Workflow {
3842
AndroidWorkflow({required AndroidSdk? androidSdk, required FeatureFlags featureFlags})
@@ -446,6 +450,7 @@ class AndroidLicenseValidator extends DoctorValidator {
446450

447451
Future<LicensesAccepted> get licensesAccepted async {
448452
LicensesAccepted? status;
453+
var sawNewCliLicensesMessage = false;
449454

450455
void handleLine(String line) {
451456
if (licenseCounts.hasMatch(line)) {
@@ -462,6 +467,8 @@ class AndroidLicenseValidator extends DoctorValidator {
462467
status = LicensesAccepted.none;
463468
} else if (licenseAccepted.hasMatch(line)) {
464469
status ??= LicensesAccepted.all;
470+
} else if (licensesNoLongerNeeded.hasMatch(line)) {
471+
sawNewCliLicensesMessage = true;
465472
}
466473
}
467474

@@ -488,13 +495,37 @@ class AndroidLicenseValidator extends DoctorValidator {
488495
.listen(handleLine)
489496
.asFuture<void>();
490497
await Future.wait<void>(<Future<void>>[output, errors]);
491-
return status ?? LicensesAccepted.unknown;
498+
if (status != null) {
499+
return status!;
500+
}
501+
if (sawNewCliLicensesMessage) {
502+
return _licensesAcceptedFromDisk();
503+
}
504+
return LicensesAccepted.unknown;
492505
} on IOException catch (e) {
493506
_logger.printTrace('Failed to run Android sdk manager: $e');
494507
return LicensesAccepted.unknown;
495508
}
496509
}
497510

511+
/// Fallback license check for Android cmdline-tools versions where
512+
/// `sdkmanager --licenses` no longer prints a parseable status
513+
/// (see https://github.com/flutter/flutter/issues/191487).
514+
LicensesAccepted _licensesAcceptedFromDisk() {
515+
if (_androidSdk == null || !_androidSdk.licensesAvailable) {
516+
return LicensesAccepted.none;
517+
}
518+
final Directory licensesDir = _androidSdk.directory.childDirectory('licenses');
519+
try {
520+
final bool hasAcceptedLicense = licensesDir.listSync().whereType<File>().any(
521+
(File file) => !file.basename.startsWith('.') && file.lengthSync() > 0,
522+
);
523+
return hasAcceptedLicense ? LicensesAccepted.all : LicensesAccepted.none;
524+
} on FileSystemException {
525+
return LicensesAccepted.unknown;
526+
}
527+
}
528+
498529
/// Run the Android SDK manager tool in order to accept SDK licenses.
499530
Future<bool> runLicenseManager() async {
500531
if (_androidSdk == null) {

‎packages/flutter_tools/test/general.shard/android/android_workflow_test.dart‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,112 @@ Review licenses that have not been accepted (y/N)?
307307
expect(result, LicensesAccepted.none);
308308
});
309309

310+
testWithoutContext(
311+
'licensesAccepted falls back to the licenses directory when sdkmanager output is unparseable (new Android CLI, licenses present)',
312+
() async {
313+
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
314+
sdk.licensesAvailable = true;
315+
sdk.directory = fileSystem.directory('/sdk')..createSync(recursive: true);
316+
fileSystem.directory('/sdk/licenses').createSync(recursive: true);
317+
fileSystem
318+
.file('/sdk/licenses/android-sdk-license')
319+
.writeAsStringSync('24333f8a63b6825ea9c5514f83c2829b004d1fee\n');
320+
321+
// Real output from Android cmdline-tools 23.0's `sdkmanager --licenses`.
322+
// See https://github.com/flutter/flutter/issues/191487.
323+
const output = '''
324+
WARNING: The SDK Manager CLI tool (sdkmanager) is deprecated. Android CLI will be used instead.
325+
The 'android' binary can also be found in the cmdline-tools directory, and 'android sdk' is the replacement for 'sdkmanager'.
326+
To learn more about the Android CLI and how to use it, see the documentation (https://d.android.com/tools/agents/android-cli)
327+
Warning: The --licenses option is no longer needed.
328+
''';
329+
processManager.addCommand(
330+
const FakeCommand(command: <String>['/foo/bar/sdkmanager', '--licenses'], stdout: output),
331+
);
332+
333+
final licenseValidator = AndroidLicenseValidator(
334+
java: FakeJava(),
335+
androidSdk: sdk,
336+
processManager: processManager,
337+
platform: FakePlatform(environment: <String, String>{'HOME': '/home/me'}),
338+
stdio: stdio,
339+
logger: BufferLogger.test(),
340+
userMessages: UserMessages(),
341+
);
342+
final LicensesAccepted result = await licenseValidator.licensesAccepted;
343+
344+
expect(result, LicensesAccepted.all);
345+
},
346+
);
347+
348+
testWithoutContext(
349+
'licensesAccepted falls back to none when sdkmanager output is unparseable and no licenses are present (new Android CLI, licenses missing)',
350+
() async {
351+
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
352+
sdk.licensesAvailable = false;
353+
sdk.directory = fileSystem.directory('/sdk')..createSync(recursive: true);
354+
355+
const output = '''
356+
WARNING: The SDK Manager CLI tool (sdkmanager) is deprecated. Android CLI will be used instead.
357+
The 'android' binary can also be found in the cmdline-tools directory, and 'android sdk' is the replacement for 'sdkmanager'.
358+
To learn more about the Android CLI and how to use it, see the documentation (https://d.android.com/tools/agents/android-cli)
359+
Warning: The --licenses option is no longer needed.
360+
''';
361+
processManager.addCommand(
362+
const FakeCommand(command: <String>['/foo/bar/sdkmanager', '--licenses'], stdout: output),
363+
);
364+
365+
final licenseValidator = AndroidLicenseValidator(
366+
java: FakeJava(),
367+
androidSdk: sdk,
368+
processManager: processManager,
369+
platform: FakePlatform(environment: <String, String>{'HOME': '/home/me'}),
370+
stdio: stdio,
371+
logger: BufferLogger.test(),
372+
userMessages: UserMessages(),
373+
);
374+
final LicensesAccepted result = await licenseValidator.licensesAccepted;
375+
376+
expect(result, LicensesAccepted.none);
377+
},
378+
);
379+
380+
testWithoutContext(
381+
'licensesAccepted returns unknown when the licenses directory cannot be listed (new Android CLI)',
382+
() async {
383+
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
384+
// `licensesAvailable` reports true, but the licenses directory does
385+
// not actually exist on disk (e.g. a race where it was deleted
386+
// between the check and use), so `listSync()` throws a
387+
// FileSystemException rather than returning results.
388+
sdk.licensesAvailable = true;
389+
sdk.directory = fileSystem.directory('/sdk')..createSync(recursive: true);
390+
391+
const output = '''
392+
WARNING: The SDK Manager CLI tool (sdkmanager) is deprecated. Android CLI will be used instead.
393+
The 'android' binary can also be found in the cmdline-tools directory, and 'android sdk' is the replacement for 'sdkmanager'.
394+
To learn more about the Android CLI and how to use it, see the documentation (https://d.android.com/tools/agents/android-cli)
395+
Warning: The --licenses option is no longer needed.
396+
''';
397+
processManager.addCommand(
398+
const FakeCommand(command: <String>['/foo/bar/sdkmanager', '--licenses'], stdout: output),
399+
);
400+
401+
final licenseValidator = AndroidLicenseValidator(
402+
java: FakeJava(),
403+
androidSdk: sdk,
404+
processManager: processManager,
405+
platform: FakePlatform(environment: <String, String>{'HOME': '/home/me'}),
406+
stdio: stdio,
407+
logger: BufferLogger.test(),
408+
userMessages: UserMessages(),
409+
);
410+
final LicensesAccepted result = await licenseValidator.licensesAccepted;
411+
412+
expect(result, LicensesAccepted.unknown);
413+
},
414+
);
415+
310416
testWithoutContext('runLicenseManager succeeds for version >= 26', () async {
311417
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
312418
sdk.sdkManagerVersion = '26.0.0';

0 commit comments

Comments
 (0)