@echo off SetLocal DisableDelayedExpansion set true=1==1 set false=1==0 REM ########################################################################################## REM # REM # Magisk Boot Image Patcher - original created by topjohnwu and modded by shakalaca's REM # modded by NewBit XDA for Android Studio AVD REM # Successfully tested on Android API: REM # [Dec. 2019] - 29 Google Apis Play Store x86_64 Production Build REM # [Jan. 2021] - 30 Google Apis Play Store x86_64 Production Build REM # [Apr. 2021] - 30 Android (S) Google Apis Play Store x86_64 Production Build rev 3 REM # REM ########################################################################################## REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-S\google_apis_playstore\x86_64\ramdisk.img REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-30\google_apis_playstore\x86_64\ramdisk.img REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-29\google_apis_playstore\x86_64\ramdisk.img call :ProcessArguments %* call :GetANDROIDHOME IF %DEBUG% ( echo [^!] We are in Debug Mode echo params=%params% echo DEBUG=%DEBUG% echo PATCHFSTAB=%PATCHFSTAB% echo GetUSBHPmodZ=%GetUSBHPmodZ% echo RAMDISKIMG=%RAMDISKIMG% echo restore=%restore% echo InstallKernelModules=%InstallKernelModules% echo InstallPrebuiltKernelModules=%InstallPrebuiltKernelModules% echo ListAllAVDs=%ListAllAVDs% echo InstallApps=%InstallApps% echo NOPARAMSATALL=%NOPARAMSATALL% ) IF NOT %InstallApps% ( REM If there is no file to work with, abort the script IF "%1" == "" ( call :ShowHelpText && exit /B 0 ) IF %ListAllAVDs% ( call :ShowHelpText && exit /B 0 ) IF NOT exist "%ANDROIDHOME%%1" ( echo file %1 not found && exit /B 0 ) ) REM Set Folders and FileNames echo [*] Set Directorys set AVDPATHWITHRDFFILE=%ANDROIDHOME%%1 for /F "delims=" %%i in ("%AVDPATHWITHRDFFILE%") do ( set AVDPATH=%%~dpi set RDFFILE=%%~nxi ) REM If we can CD into the ramdisk.img, it is not a file! cd %AVDPATHWITHRDFFILE% >nul 2>&1 IF "%ERRORLEVEL%"=="0" ( call :ShowHelpText && exit /B 0 ) IF %restore% ( call :restore_backups && exit /B 0 ) call :TestADB REM The Folder where the script was called from set ROOTAVD=%cd% set MAGISKZIP=%ROOTAVD%\Magisk.zip REM Kernel Names set BZFILE=%ROOTAVD%\bzImage set KRFILE=kernel-ranchu IF %InstallApps% ( call :installapps && exit /B 0 ) set ADBWORKDIR=/data/data/com.android.shell set ADBBASEDIR=%ADBWORKDIR%/Magisk echo [-] In any AVD via ADB, you can execute code without root in /data/data/com.android.shell call :TestADBWORKDIR REM change to ROOTAVD directory cd %ROOTAVD% echo [*] Cleaning up the ADB working space adb shell rm -rf %ADBBASEDIR% echo [*] Creating the ADB working space adb shell mkdir %ADBBASEDIR% echo [*] looking for Magisk installer Zip IF NOT exist "%MAGISKZIP%" ( echo [-] Please download Magisk.zip file ) ELSE ( call :pushtoAVD "%MAGISKZIP%" ) REM Proceed with ramdisk set INITRAMFS=%ROOTAVD%\initramfs.img IF %RAMDISKIMG% ( REM Is it a ramdisk named file? echo.%RDFFILE% | findstr /I ramdisk.*.img >NUL || ( echo [!] please give a path to a ramdisk file exit /B 0 ) call :create_backup %RDFFILE% call :pushtoAVD "%AVDPATHWITHRDFFILE%" "ramdisk.img" IF %InstallKernelModules% ( IF EXIST "%INITRAMFS%" ( call :pushtoAVD "%INITRAMFS%" ) ) ) echo [-] Copy rootAVD Script into Magisk DIR adb push rootAVD.sh %ADBBASEDIR% echo [-] run the actually Boot/Ramdisk/Kernel Image Patch Script echo [*] from Magisk by topjohnwu and modded by NewBit XDA adb shell sh %ADBBASEDIR%/rootAVD.sh %* IF "%ERRORLEVEL%"=="0" ( REM In Debug-Mode we can skip parts of the script IF NOT %DEBUG% ( IF %RAMDISKIMG% ( call :pullfromAVD ramdiskpatched4AVD.img "%AVDPATHWITHRDFFILE%" call :pullfromAVD Magisk.apk %ROOTAVD%\Apps\ call :pullfromAVD Magisk.zip IF %InstallPrebuiltKernelModules% ( call :pullfromAVD %BZFILE% call :InstallKernelModules ) IF %InstallKernelModules% ( call :InstallKernelModules ) echo [-] Clean up the ADB working space adb shell rm -rf %ADBBASEDIR% call :installapps echo [-] Shut-Down and Reboot [Cold Boot Now] the AVD and see IF it worked echo [-] Root and Su with Magisk for Android Studio AVDs echo [-] Modded by NewBit XDA - Jan. 2021 echo [*] Huge Credits and big Thanks to topjohnwu, shakalaca and vvb2060 call :ShutDownAVD ) ) ) exit /B %ERRORLEVEL% :TestADBWORKDIR echo [*] Testing the ADB working space SetLocal EnableDelayedExpansion set ADBWORKS= adb shell cd %ADBWORKDIR% > tmpFile 2>&1 set /P ADBWORKS=Nul && ( echo [^^!] %ADBWORKDIR% is not available call :_Exit 2> nul ) echo [^^!] %ADBWORKDIR% is available EndLocal exit /B 0 :ShutDownAVD SetLocal EnableDelayedExpansion set ADBPULLECHO= adb shell setprop sys.powerctl shutdown > tmpFile 2>&1 set /P ADBPULLECHO=Nul || ( echo [-] Trying to shut down the AVD ) echo [^^!] If the AVD doesnt shut down, try it manually^^! EndLocal exit /B 0 :InstallKernelModules SetLocal EnableDelayedExpansion IF EXIST "%BZFILE%" ( call :create_backup %KRFILE% echo [*] Copy %BZFILE% ^(Kernel^) into kernel-ranchu copy "%BZFILE%" "%AVDPATH%%KRFILE%" >Nul IF "%ERRORLEVEL%"=="0" ( del "%BZFILE%" "%INITRAMFS%" ) ) EndLocal exit /B 0 :pullfromAVD SetLocal EnableDelayedExpansion set SRC=%1 set DST=%2 set ADBPULLECHO= setlocal enableDelayedExpansion for /f "delims=" %%i in ("!SRC!") do ( endlocal & REM set "SRC=%%~nxi" ) setlocal enableDelayedExpansion for /f "delims=" %%i in ("!DST!") do ( endlocal & REM set "DST=%%~nxi" ) adb pull %ADBBASEDIR%/%SRC% %2 > tmpFile 2>&1 set /P ADBPULLECHO=Nul || ( echo [*] Pull %SRC% into %DST% echo [-] %ADBPULLECHO% ) EndLocal exit /B 0 :pushtoAVD SetLocal EnableDelayedExpansion set SRC=%1 set DST=%2 set ADBPUSHECHO= setlocal enableDelayedExpansion for /f "delims=" %%i in ("!SRC!") do ( endlocal & REM set "SRC=%%~nxi" ) setlocal enableDelayedExpansion for /f "delims=" %%i in ("!DST!") do ( endlocal & REM set "DST=%%~nxi" ) IF "%DST%"=="" ( echo [*] Push %SRC% into %ADBBASEDIR% adb push %1 %ADBBASEDIR% > tmpFile 2>&1 ) ELSE ( echo [*] Push %SRC% into %ADBBASEDIR%/%DST% adb push %1 %ADBBASEDIR%/%DST% > tmpFile 2>&1 ) set /P ADBPUSHECHO=Nul IF EXIST "%AVDPATH%%BACKUPFILE%" ( echo [-] Backup File was created ) ) ELSE ( echo [-] Backup exists already ) ENDLOCAL exit /B 0 :TestADB SetLocal EnableDelayedExpansion set ADB_DIR="" set ADB_EX="" echo [-] Test IF ADB SHELL is working set ADBWORKS= adb shell -n echo true > tmpFile 2>&1 set /P ADBWORKS=Nul && ( echo [^^!] ADB device is offline echo [*] no ADB connection possible call :_Exit 2> nul ) echo.%ADBWORKS%| FIND /I "unauthorized">Nul && ( echo [^^!] %ADBWORKS% echo [*] no ADB connection possible call :_Exit 2> nul ) echo.%ADBWORKS%| FIND /I "recognized">Nul && ( IF EXIST "%ANDROIDHOME%%ADB_DIR_W%" ( set ADB_DIR=%ADB_DIR_W% ) ELSE ( echo [^^!] ADB not found, please install platform-tools and add it to your %%PATH%% call :_Exit 2> nul ) for /f "delims=" %%i in ('dir "%ANDROIDHOME%%ADB_DIR%adb.exe" /s /b /a-d') do ( set ADB_EX=%%i ) IF "!ADB_EX!" == "" ( echo [^^!] ADB binary not found in %ENVVAR%\%ADB_DIR% call :_Exit 2> nul ) echo [^^!] ADB is not in your Path, try to echo set PATH=%ENVVAR%\!ADB_DIR!;%%PATH%% IF EXIST "!ADB_EX!" ( echo [*] setting it, just during this session, for you set "PATH=%ANDROIDHOME%!ADB_DIR!;%PATH%" REM goto :TestADB call :TestADB ) ) echo.%ADBWORKS%| FIND /I "error">Nul && ( echo [^^!] %ADBWORKS% echo [*] no ADB connection possible call :_Exit 2> nul ) echo.%ADBWORKS%| FIND /I "no devices/emulators found">Nul && ( echo [^^!] %ADBWORKS% echo [*] no ADB connection possible call :_Exit 2> nul ) ) IF EXIST "!ADB_EX!" ( ENDLOCAL & set "PATH=%PATH%" ) ELSE ( ENDLOCAL ) exit /B 0 :restore_backups for /f "delims=" %%i in ('dir "%AVDPATH%*.backup" /s /b /a-d') do ( echo [^!] Restoring %%~ni%%~xi to %%~ni copy "%%i" "%%~di%%~pi%%~ni" >nul 2>&1 ) echo [*] Backups still remain in place REM call :_Exit 2> nul exit /B 0 :ProcessArguments set params=%* set DEBUG=%false% set PATCHFSTAB=%false% set GetUSBHPmodZ=%false% set RAMDISKIMG=%false% set restore=%false% set InstallKernelModules=%false% set InstallPrebuiltKernelModules=%false% set ListAllAVDs=%false% set InstallApps=%false% set NOPARAMSATALL=%false% REM While debugging and developing you can turn this flag on echo.%params%| FIND /I "DEBUG">Nul && ( set DEBUG=%true% REM Shows whatever line get executed... REM echo on ) REM Call rootAVD with PATCHFSTAB if you want the RAMDISK merge your modded fstab.ranchu before Magisk Mirror gets mounted echo.%params%| FIND /I "PATCHFSTAB">Nul && ( set PATCHFSTAB=%true% ) REM Call rootAVD with GetUSBHPmodZ to download the usbhostpermissons module echo.%params%| FIND /I "GetUSBHPmodZ">Nul && ( set GetUSBHPmodZ=%true% ) REM Call rootAVD with ListAllAVDs to show all AVDs with command examples echo.%params%| FIND /I "ListAllAVDs">Nul && ( set ListAllAVDs=%true% ) REM Call rootAVD with InstallApps to just install all APKs placed in the Apps folder echo.%params%| FIND /I "InstallApps">Nul && ( set InstallApps=%true% ) set RAMDISKIMG=%true% IF "%2" == "restore" ( set restore=%true% ) ELSE IF "%2"=="InstallKernelModules" ( set InstallKernelModules=%true% ) ELSE IF "%2"=="InstallPrebuiltKernelModules" ( set InstallPrebuiltKernelModules=%true% ) IF "%params%"=="" ( REM No Parameters SET at all set NOPARAMSATALL=%true% ) exit /B 0 :installapps SetLocal EnableDelayedExpansion echo [-] Install all APKs placed in the Apps folder for %%i in (APPS\*.apk) do ( set APK=%%i :whileloop echo [*] Trying to install !APK! for /f "delims=" %%A in ('adb install -r -d !APK! 2^>^&1' ) do ( echo [-] %%A echo.%%A| FIND /I "INSTALL_FAILED_UPDATE_INCOMPATIBLE">Nul && ( set Package= for %%p in (%%A) do ( echo.!Package!| FIND /I "Package">Nul && ( echo [*] Need to uninstall %%p first adb uninstall %%p > tmpFile 2>&1 set /P ADBECHO=