Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions eng/devices/android.cake
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,41 @@ async Task HandleVirtualDevice(AndroidEmulatorToolSettings emuSettings, AndroidA
// delete the AVD first, if it exists
Information("Deleting AVD if exists: {0}...", avdName);
try { AndroidAvdDelete(avdName, avdSettings); }
catch { }
catch (Exception ex) { Warning("Failed to delete AVD: {0}", ex.Message); }

// create the new AVD
Information("Creating AVD: {0} ({1})...", avdName, avdImage);
AndroidAvdCreate(avdName, avdImage, avdSkin, force: true, settings: avdSettings);
}

// 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}");
}

Comment on lines +463 to +490
Copy link

Copilot AI Nov 19, 2025

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.

Suggested change
// 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);

Copilot uses AI. Check for mistakes.
// start the emulator
Information("Starting Emulator: {0}...", avdName);
emulatorProcess = AndroidEmulatorStart(avdName, emuSettings);
Expand Down Expand Up @@ -538,7 +566,7 @@ void CleanUpVirtualDevice(AndroidEmulatorProcess emulatorProcess, AndroidAvdMana
Information("AndroidAvdDelete");
// delete the AVD
try { AndroidAvdDelete(androidAvd, avdSettings); }
catch { }
catch (Exception ex) { Warning("Failed to delete AVD during cleanup: {0}", ex.Message); }
}
}

Expand Down
Loading