Skip to content

Commit 383b332

Browse files
[CP-stable]Fix Android license detection for cmdline-tools 23.0+ (#192133)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: #191487 ### Impact Description: Android SDK Command-line Tools 23.0 deprecated `sdkmanager --licenses` in favor of the new `android` CLI. When invoked, `sdkmanager --licenses` now only outputs a deprecation notice (`Warning: The --licenses option is no longer needed.`) rather than the parseable license summary `flutter doctor` previously depended on. As a result, `flutter doctor` fails to parse license status and reports `✗ Android license status unknown.` while prompting users to run `flutter doctor --android-licenses`, which fails to resolve the issue. ### Changelog Description: [flutter/191487] When using Android SDK Command-line Tools 23.0+ for Android, flutter doctor incorrectly reports "Android license status unknown". ### Workaround: 1. Downgrade Android SDK Command-line Tools to version 22.0 via the Android Studio SDK Manager or CLI. 2. If licenses have already been accepted on disk in `<Android-SDK>/licenses/`, Android Gradle builds still succeed and the `flutter doctor` warning can be safely ignored (if the user knows to ignore). ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Ensure Android SDK Command-line Tools 23.0 is installed (verify in `$ANDROID_HOME/cmdline-tools/latest/source.properties` that `Pkg.Revision=23.0`). 2. Run `flutter doctor -v`. 3. Verify that under `Android toolchain`, it reports `• All Android licenses accepted.` instead of `✗ Android license status unknown.` 4. (Optional negative check) Temporarily rename the `$ANDROID_HOME/licenses` directory, run `flutter doctor -v`, and verify it reports `Android licenses not accepted`. Restore the directory.
1 parent 115c53d commit 383b332

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
@@ -32,6 +32,10 @@ enum LicensesAccepted { none, some, all, unknown }
3232
final licenseCounts = RegExp(r'(\d+) of (\d+) SDK package licenses? not accepted.');
3333
final licenseNotAccepted = RegExp(r'licenses? not accepted', caseSensitive: false);
3434
final licenseAccepted = RegExp(r'All SDK package licenses accepted.');
35+
final licensesNoLongerNeeded = RegExp(
36+
r'--licenses option is no longer needed',
37+
caseSensitive: false,
38+
);
3539

3640
class AndroidWorkflow implements Workflow {
3741
AndroidWorkflow({required AndroidSdk? androidSdk, required FeatureFlags featureFlags})
@@ -445,6 +449,7 @@ class AndroidLicenseValidator extends DoctorValidator {
445449

446450
Future<LicensesAccepted> get licensesAccepted async {
447451
LicensesAccepted? status;
452+
var sawNewCliLicensesMessage = false;
448453

449454
void handleLine(String line) {
450455
if (licenseCounts.hasMatch(line)) {
@@ -461,6 +466,8 @@ class AndroidLicenseValidator extends DoctorValidator {
461466
status = LicensesAccepted.none;
462467
} else if (licenseAccepted.hasMatch(line)) {
463468
status ??= LicensesAccepted.all;
469+
} else if (licensesNoLongerNeeded.hasMatch(line)) {
470+
sawNewCliLicensesMessage = true;
464471
}
465472
}
466473

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

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