Android Studio 模擬器 安卓12 安裝Magisk

Toycon發表於2024-03-26

本文指令碼修改自github上的一個指令碼
環境為Mac OS-Arm版

1. 建立一個目錄

mkdir magisk-sh

2. 下載Magisk apk

可以去github上下載,連結:https://github.com/topjohnwu/Magisk/releases
本文采用v26.1版本
下載完成之後,可以直接拖入模擬器中安裝
還需要將magisk apk檔案放入剛才建立的目錄中

3. 編寫指令碼

指令碼字尾名為.sh
指令碼僅供參考,有些地方需要修改

  • 修改APILEVEL為自己的
  • 修改APK為自己的,注意要有版本號,沒有要修改一些東西
  • 修改RAMDISK,如果是從Android Studio中下載的sdk和模擬器的話,只需要修改system-images目錄後面的就行了,其他的自行尋找目錄
  • 需要注意修改4,其中arm64-v8a是arm版的目錄,不同架構的需要修改成對應的架構,如x86_64
# 修改1
APILEVEL=31
# 修改2
APK="Magisk-v26.1"

TMPDIR="/data/local/tmp/.magisk"
# 修改3
RAMDISK="/Users/toycon/Library/Android/sdk/system-images/android-31/google_apis/arm64-v8a/ramdisk.img"

  

yellow=`tput setaf 3`

green=`tput setaf 2`

red=`tput setaf 1`

reset=`tput sgr0`

  

echo "${green}[*] Using APK ${APK}.apk"

echo "[*] Emulator temp dir is ${TMPDIR}"

echo "[*] RAMDISK image to patch is ${RAMDISK}${reset}"

  

rm -fr "${APK}"

  

echo "${yellow}[+] Extracting Magisk APK...${reset}"

apktool d -r -s "${APK}.apk"

  

echo "${yellow}[*] Testing ADB access...${reset}"

adb shell uname -a

if [ $? -eq 0 ]; then

echo "${green}[*] ADB access seems to be working. Proceeding..."

else

echo "${red}[!] ADB access error. Aborted.${reset}"

exit 1

fi

  

echo "${yellow}[+] Pushing required files from APK to device TEMP folder${reset}"

adb shell "rm -fr ${TMPDIR}"

adb shell "mkdir ${TMPDIR}"

  
# 修改4
adb push "${APK}/lib/arm64-v8a/libmagiskboot.so" "${TMPDIR}/magiskboot"

adb push "${APK}/lib/arm64-v8a/libmagiskinit.so" "${TMPDIR}/magiskinit"

adb push "${APK}/lib/arm64-v8a/libmagisk64.so" "${TMPDIR}/magisk64"

adb push "${APK}/assets/stub.apk" "${TMPDIR}/stub.apk"

  

adb shell "chmod +x ${TMPDIR}/magiskboot"

adb shell "chmod +x ${TMPDIR}/magisk64"

  

echo "${yellow}[+] Prepatching and uploading 2-staged RAMDISK (into single staged version)${reset}"

lz4cat "${RAMDISK}" | bbe -b '/070701000493e6000001ed0000000000000000000000010000000000000000000000000000000000000000000000000000000b00000000TRAILER!!!/:/07070100/' -e 'D;A 07070100' | adb shell "cat > ${TMPDIR}/ramdisk.cpio"

  

adb shell "cd ${TMPDIR}; cp -af ramdisk.cpio ramdisk.cpio.orig"

  

echo "${yellow}[+] RAMDISK is ready to patch!${reset}"

  

adb shell "echo KEEPVERITY=true > ${TMPDIR}/config"

adb shell "echo KEEPFORCEENCRYPT=true >> ${TMPDIR}/config"

adb shell "echo PREINITDEVICE=\$(${TMPDIR}/magisk64 --preinit-device) >> ${TMPDIR}/config"

adb shell "cd ${TMPDIR}; ./magiskboot compress=xz magisk64 magisk64.xz"

adb shell "cd ${TMPDIR}; ./magiskboot compress=xz stub.apk stub.xz"

  

echo "${yellow}[+] Patching MAGISK to RAMDISK${reset}"

adb shell "cd ${TMPDIR}; KEEPVERITY=true KEEPFORCEENCRYPT=true ./magiskboot cpio ramdisk.cpio \"add 0750 init magiskinit\" \"mkdir 0750 overlay.d\" \"mkdir 0750 overlay.d/sbin\" \"add 0644 overlay.d/sbin/magisk64.xz magisk64.xz\" \"add 0644 overlay.d/sbin/stub.xz stub.xz\" \"patch\" \"backup ramdisk.cpio.orig\" \"mkdir 000 .backup\" \"add 000 .backup/.magisk config\""

  

echo "${yellow}[+] Pulling and compressing patched RAMDISK and cleaning up files on device${reset}"

adb shell "cat ${TMPDIR}/ramdisk.cpio" | gzip -c - > ramdisk-patched.img

  

adb shell "rm -fr ${TMPDIR}"

  

echo "${green}[*] MAGISK patched RAMDISK image is available: ramdisk-patched.img"

echo "[*] Run emulator with option '-ramdisk ramdisk-patched.img'${reset}"

4. 儲存並執行

cd magisk-sh
sh ./magisk-install.sh

5. 執行問題及解決

... : command not found.

缺什麼安裝什麼
比如:我在執行時缺apktoolbbe,透過 homebrew 安裝即可

brew install apktool
brew install bbe

若未安裝 homebrew ,可自行上網搜尋安裝,或安裝其他缺失的軟體。

6. 將ramdisk.img複製到模擬器目錄

cp ./ramdisk-patched.img /Users/toycon/Library/Android/sdk/system-images/android-31/google_apis/arm64-v8a/ramdisk.img

重啟模擬器即可,然後就能看到Magisk安裝成功了

相關文章