diff --git a/eng/devices/android.cake b/eng/devices/android.cake index d08213f51bd8..38957c970ea8 100644 --- a/eng/devices/android.cake +++ b/eng/devices/android.cake @@ -597,25 +597,24 @@ void PrepareDevice(bool waitForBoot) { Information("Waiting for the emulator to finish booting..."); - // wait for it to finish booting - var waited = 0; - var total = EmulatorBootTimeoutSeconds; - while (AdbShell("getprop sys.boot_completed", settings).FirstOrDefault() != "1") + // Wait for the emulator to finish booting + var waited = 0; + var total = EmulatorBootTimeoutSeconds; + while (AdbShell("getprop sys.boot_completed", settings).FirstOrDefault() != "1") { - System.Threading.Thread.Sleep(1000); - - Information("Waiting {0}/{1} seconds for the emulator to boot up.", waited, total); - if (waited++ > total) - { - throw new Exception("The emulator did not finish booting in time."); - } - - // something may be wrong with ADB, so restart every 30 seconds just in case - if (waited % 30 == 0 && IsCIBuild()) - { - Information("Trying to restart ADB just in case..."); - AdbKillServer(adbSettings); - } + System.Threading.Thread.Sleep(1000); + + Information("Waiting {0}/{1} seconds for the emulator to boot up.", waited, total); + if (waited++ > total) + { + throw new Exception("The emulator did not finish booting in time."); + } + + if (waited % 60 == 0 && IsCIBuild()) + { + // Ensure ADB keys are configured + EnsureAdbKeys(settings); + } } Information("Waited {0} seconds for the emulator to boot up.", waited); @@ -641,4 +640,55 @@ void PrepareDevice(bool waitForBoot) Information("{0}", string.Join("\n", lines)); Information("Finished setting ADB properties."); +} + +void EnsureAdbKeys(AdbToolSettings settings) +{ + Information("Ensuring ADB keys are correctly configured..."); + + try + { + var adbKeyPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".android"); + var adbKeyFile = System.IO.Path.Combine(adbKeyPath, "adbkey"); + var adbKeyPubFile = System.IO.Path.Combine(adbKeyPath, "adbkey.pub"); + + // Delete existing ADB keys to avoid stale data + if (System.IO.File.Exists(adbKeyFile)) System.IO.File.Delete(adbKeyFile); + if (System.IO.File.Exists(adbKeyPubFile)) System.IO.File.Delete(adbKeyPubFile); + + // Restart ADB to regenerate keys + AdbKillServer(settings); + AdbStartServer(settings); + + // Verify ADB keys exist + if (!System.IO.File.Exists(adbKeyFile) || !System.IO.File.Exists(adbKeyPubFile)) + { + throw new Exception("Failed to regenerate ADB keys."); + } + + Information($"ADB keys have been successfully regenerated at {adbKeyPath}."); + + // Set correct permissions for ADB keys + Information("Set correct permissions for ADB keys."); + StartProcess("chmod", $"700 {adbKeyPath}"); + StartProcess("chmod", $"600 {adbKeyFile}"); + StartProcess("chmod", $"600 {adbKeyPubFile}"); + + // Manually authorize the emulator + Information("Manually authorize the emulator with the generated ADB keys."); + AdbShell("adb push ~/.android/adbkey.pub /data/misc/adb/adb_keys", settings); + AdbShell("adb shell chmod 600 /data/misc/adb/adb_keys", settings); + AdbShell("adb shell stop adbd", settings); + AdbShell("adb shell start adbd", settings); + + Information("Emulator authorized successfully."); + } + catch (Exception ex) + { + Warning($"Error ensuring ADB keys: {ex.Message}"); + + Information("Trying to restart ADB just in case..."); + + AdbKillServer(adbSettings); + } } \ No newline at end of file