https://github.com/topjohnwu/Magisk
本文对 V26.1版本 进行编译
编译环境:Ubuntu 22.04 (WSL) (建议使用Linux环境进行编译,Windows环境坑太多)
官方构建文档:Building and Development | Magisk (topjohnwu.github.io)
<aside>
⚠️ 注意需要开启Git的**Enable symbolic links** 选项,因为在编译过程中需要使用到符号链接,否则会失败。
在Windows Git安装过程中有一步可以选择开启符号链接。
在Linux下可以使用**git config --global core.symlinks true** 进行开启
</aside>
因为构建APP需要JDK17支持,所以为了方便官方推荐下载Android Studio,然后设置系统环境变量**
ANDROID_STUDIO**为安装路径可进行编译,否则无法构建APP。
然后需要设置**
ANDROID_SDK_ROOT**环境变量,该目录位置可以在AndroidStudio的设置中SDK中找到。
git clone --recurse-submodules <https://github.com/topjohnwu/Magisk.git>
<aside>
⚠️ 这里注意必须要使用**--recurse-submodules** 选项拉取Magisk编译过程中需要的子模块,否则编译无法进行。
在下载过程中可能会遇到网络问题导致拉取不完整,建议使用代理进行下载或使用流量进行下载。如果拉取不完整,需要手动进行下载并且将对应子模块放置于**native/src/external目录下对应子模块中并执行git submodule update --init --recursive**
</aside>
在拉取下来的Magisk目录存在**
build.py** 文件,该文件为编译脚本,使用**python3 build.py ndk** 即可下载官方NDK工具链,只不过这个和常规NDK不一样,Magisk使用的工具链为ONDKhttps://github.com/topjohnwu/ondk ,即**Oxidized NDK (ONDK) is an unofficial repackaged [Android NDK](<https://developer.android.com/ndk>) that includes a Rust toolchain.下载完成后无需进行其他操作,但是前提是前置环境变量等设置正确。**
在这里可以使用**
python build.py all**直接进行编译,将会编译三大部分。编译需要较长时间,耐心等待即可。
第一部分为Native工具,包括 **magisk magiskinit busybox magiskboot**等
第二部分编译APK,分为两个APK,一个是**app-debug(release).apk,其实就是Magisk本体安装程序,将其后缀改为zip即可通过REC刷入。另一个是stub-debug(release).apk** 即Magisk代理程序,即在隐藏Magisk后会创建的空壳APP,其作用就是拿到完整Magisk APK并将其加载到内存中进行运行以防止检测。
When hiding the Magisk app, it will install a “stub” APK that has nothing in it. The only functionality this stub app has is downloading the full Magisk app APK into its internal storage and dynamically loading it. Due to the fact that the APK is literally *empty*, it does not contain the image resource for the app icon.
<aside>
💡 如果想通过AndroidStudio进行代码分析可以参考Building and Development | Magisk (topjohnwu.github.io)中的**IDE Support一节**
</aside>
可以直接安装编译输出的**
app-debug(release).apk,然后在内部选择打补丁(前提是已经解锁),打完补丁后fastboot刷入补丁后的boot.img。也可以直接REC刷入ZIP包,前提是第三方REC,否则刷不进去。如果是debug版本,安装完成就是D版本,调试信息会很详细。**
