Android Studio 模拟器 安卓12 安装Magisk

本文脚本修改自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安装成功了

作者:Toycon原文地址:https://www.cnblogs.com/toycon/p/18097813

%s 个评论

要回复文章请先登录注册