-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[CI] Pre-authorize AVD with ADB keys before starting the emulator #32730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
Android UI tests - emulator started correctly: There are failing
|
|
All test failures are unrelated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR attempts to fix "adb: device unauthorized" errors by pre-authorizing ADB keys before starting the Android emulator. It adds a new code block that ensures ADB keys exist and copies them to the AVD directory before boot, plus improves error logging in exception handlers.
Key changes:
- Pre-authorization logic that calls
EnsureAdbKeysand copies the public key to the AVD directory before emulator boot - Enhanced error handling with warning messages instead of silent catch blocks
| // Pre-authorize ADB keys before starting emulator to avoid "device unauthorized" errors | ||
| Information("Pre-authorizing ADB keys for emulator..."); | ||
| try | ||
| { | ||
| // Ensure ADB keys exist | ||
| EnsureAdbKeys(adbSettings); | ||
|
|
||
| // Copy the public key to the AVD directory so it's trusted from boot | ||
| var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
| var adbKeyPubSource = System.IO.Path.Combine(homeDir, ".android", "adbkey.pub"); | ||
| var avdPath = System.IO.Path.Combine(homeDir, ".android", "avd", $"{avdName}.avd"); | ||
| var avdAdbKeysDest = System.IO.Path.Combine(avdPath, "adbkey.pub"); | ||
|
|
||
| if (System.IO.File.Exists(adbKeyPubSource) && System.IO.Directory.Exists(avdPath)) | ||
| { | ||
| System.IO.File.Copy(adbKeyPubSource, avdAdbKeysDest, overwrite: true); | ||
| Information($"Pre-authorized ADB key copied to: {avdAdbKeysDest}"); | ||
| } | ||
| else | ||
| { | ||
| Warning($"Could not pre-authorize ADB key. Source exists: {System.IO.File.Exists(adbKeyPubSource)}, AVD path exists: {System.IO.Directory.Exists(avdPath)}"); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Warning($"Failed to pre-authorize ADB keys (will retry during boot): {ex.Message}"); | ||
| } | ||
|
|
Copilot
AI
Nov 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pre-authorization approach may not work as intended. The standard Android emulator authorization mechanism expects authorized keys to be in /data/misc/adb/adb_keys on the running emulator, not in the AVD directory structure before boot.
Copying adbkey.pub to {avdName}.avd/adbkey.pub is not a documented Android emulator feature. The emulator reads authorized keys from its internal /data/misc/adb/adb_keys file at runtime, which is already handled by the existing EnsureAdbKeys function (see lines 951-992 where it pushes keys to /data/misc/adb/adb_keys).
Consider testing whether this pre-authorization actually prevents "device unauthorized" errors, or if the existing runtime authorization mechanism (already in EnsureAdbKeys) is sufficient.
| // Pre-authorize ADB keys before starting emulator to avoid "device unauthorized" errors | |
| Information("Pre-authorizing ADB keys for emulator..."); | |
| try | |
| { | |
| // Ensure ADB keys exist | |
| EnsureAdbKeys(adbSettings); | |
| // Copy the public key to the AVD directory so it's trusted from boot | |
| var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | |
| var adbKeyPubSource = System.IO.Path.Combine(homeDir, ".android", "adbkey.pub"); | |
| var avdPath = System.IO.Path.Combine(homeDir, ".android", "avd", $"{avdName}.avd"); | |
| var avdAdbKeysDest = System.IO.Path.Combine(avdPath, "adbkey.pub"); | |
| if (System.IO.File.Exists(adbKeyPubSource) && System.IO.Directory.Exists(avdPath)) | |
| { | |
| System.IO.File.Copy(adbKeyPubSource, avdAdbKeysDest, overwrite: true); | |
| Information($"Pre-authorized ADB key copied to: {avdAdbKeysDest}"); | |
| } | |
| else | |
| { | |
| Warning($"Could not pre-authorize ADB key. Source exists: {System.IO.File.Exists(adbKeyPubSource)}, AVD path exists: {System.IO.Directory.Exists(avdPath)}"); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Warning($"Failed to pre-authorize ADB keys (will retry during boot): {ex.Message}"); | |
| } | |
| // Ensure ADB keys exist and are authorized at runtime | |
| EnsureAdbKeys(adbSettings); |

Description of Change
This is an attempt at fixing
adb: device unauthorized.by copying the ADB public key to the AVD before starting the emulator.Issues Fixed
Fixes #32601