Created
May 24, 2025 03:18
-
-
Save chsbuffer/a84335b3d90582a74cfd15f92e12af4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 定义常量 | |
| $magiskRepo = "C:\a\magisk" | |
| $magiskApk = Resolve-Path "Magisk-v29.0.apk" | |
| $device = "pixel_8_pro" | |
| $AndroidSdkRoot = "C:\Users\$env:UserName\AppData\Local\Android\Sdk" | |
| $sdkmanager = Join-Path $AndroidSdkRoot "cmdline-tools\latest\bin\sdkmanager.bat" | |
| $avdmanager = Join-Path $AndroidSdkRoot "cmdline-tools\latest\bin\avdmanager.bat" | |
| $emulator = Join-Path $AndroidSdkRoot "emulator\emulator.exe" | |
| $adb = Join-Path $AndroidSdkRoot "platform-tools\adb.exe" | |
| $magiskBuildScript = Join-Path $magiskRepo "build.py" | |
| # 工具检查 | |
| $requiredTools = @($sdkmanager, $avdmanager, $emulator, $adb, $magiskBuildScript, $magiskApk) | |
| foreach ($tool in $requiredTools) { | |
| if (-not (Test-Path $tool)) { | |
| Write-Host "错误:未找到 $tool。" -ForegroundColor Red | |
| exit 1 | |
| } | |
| } | |
| function Download-AVD($image) { | |
| Write-Host "正在下载: $image" -ForegroundColor Cyan | |
| & $sdkmanager --install "$image" | Write-Host | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "下载 $image 失败。" -ForegroundColor Red | |
| return | |
| } | |
| $apiLevel = ($image -split ';')[1] -replace 'android-' | |
| $avdName = "android_${apiLevel}_${device}" | |
| Write-Host "正在为 $image 创建 AVD: $avdName" -ForegroundColor Cyan | |
| & $avdmanager create avd -n "$avdName" -k "$image" -d "$device" | Write-Host | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "创建 AVD $avdName 失败。" -ForegroundColor Red | |
| } | |
| else { | |
| Write-Host "AVD $avdName 创建成功。" -ForegroundColor Green | |
| } | |
| } | |
| function Root-AVD($image) { | |
| $apiLevel = ($image -split ';')[1] -replace 'android-' | |
| $avdName = "android_${apiLevel}_${device}" | |
| $ramdiskDir = Join-Path $AndroidSdkRoot ("$image" -replace ';', '\') | |
| $originalRamdiskPath = Join-Path $ramdiskDir "ramdisk.img" | |
| $patchedRamdiskPath = Join-Path $ramdiskDir "ramdisk_root.img" | |
| $backupRamdiskPath = Join-Path $ramdiskDir "ramdisk.img.bak" | |
| if (-not (Test-Path $originalRamdiskPath)) { | |
| Write-Host "未找到原始 ramdisk.img,跳过。" -ForegroundColor Red | |
| return | |
| } | |
| if (Test-Path $backupRamdiskPath) { | |
| Write-Host "找到备份 ramdisk.img,已修补,跳过。" -ForegroundColor Red | |
| return | |
| } | |
| Write-Host "`n--- 正在处理 AVD: $avdName ---" -ForegroundColor Yellow | |
| Start-Process -FilePath $emulator -ArgumentList "-avd $avdName -no-window -no-audio" -PassThru -NoNewWindow | |
| & $adb wait-for-local-device | |
| Write-Host "正在修补 ramdisk.img..." -ForegroundColor Cyan | |
| Start-Process -Wait -WorkingDirectory $magiskRepo -FilePath "python" -ArgumentList "`"$magiskBuildScript`" avd_patch --apk `"$magiskApk`" `"$originalRamdiskPath`" `"$patchedRamdiskPath`"" -NoNewWindow | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "修补失败。" -ForegroundColor Red | |
| & $adb emu kill | |
| return | |
| } | |
| if (-not (Test-Path $patchedRamdiskPath)) { | |
| Write-Host "修补后的文件不存在,跳过。" -ForegroundColor Red | |
| & $adb emu kill | |
| return | |
| } | |
| Move-Item -Path $originalRamdiskPath -Destination $backupRamdiskPath -Force -ErrorAction SilentlyContinue | |
| Move-Item -Path $patchedRamdiskPath -Destination $originalRamdiskPath -Force | |
| & $adb install $magiskApk | |
| & $adb emu kill | |
| Write-Host "AVD: $avdName 已成功 Root。" -ForegroundColor Green | |
| } | |
| $images = @( | |
| "system-images;android-29;google_apis_playstore;x86", | |
| "system-images;android-30;google_apis_playstore;x86", | |
| "system-images;android-31;google_apis_playstore;x86_64", | |
| "system-images;android-32;google_apis_playstore;x86_64", | |
| "system-images;android-33;google_apis_playstore;x86_64", | |
| "system-images;android-34;google_apis_playstore;x86_64", | |
| "system-images;android-35;google_apis_playstore;x86_64" | |
| ) | |
| foreach ($image in $images) { | |
| Download-AVD $image | |
| Root-AVD $image | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment