for 代码插桩框架 或 xposed框架
在android5.0之前,使用dalvik虚拟机,之后换成了art虚拟机,这个方法对新的系统可能不好用了,作为一种思路仅供参考。
原因
在android4.x的时代,流行刷机和第三方ROM,比如MIUI、乐蛙、百度等等。
ROM的制作者要在ROM中集成Xposed或者加载MIUI的Framework,必须修改Boot分区的ramdisk/init.environ.rc来加载额外的BOOTCLASSPATH,例如添加:
/data/app/com.miui.sdk-1.apk
/data/app/com.miui.sdk-2.apk
/system/app/miui.apk
/data/app/com.miui.framework-1.apk
/data/app/com.miui.framework-2.apk
/system/app/MiuiFramework.apk
但有些手机因为BL锁的原因,就是不能改boot分区。
为了绕开这个限制,直接修改libdvm.so源码,自定义BOOTCLASSPATH变量,最终实现修改目的。
修改方法
- 一般手机商不会去修改虚拟机的底层代码,所以只要找到对应的android大版本源码来修改编译替换就可以了。
- 在dalvik初始化读取BOOTCLASSPATH变量后,插入自己要添加的class路径
- 源码路径dalvik/vm/Init.cpp,修改setCommandLineDefaults函数
static void setCommandLineDefaults()
{
const char* envStr = getenv("CLASSPATH");
if (envStr != NULL) {
gDvm.classPathStr = strdup(envStr);
} else {
gDvm.classPathStr = strdup(".");
}
envStr = getenv("BOOTCLASSPATH");
//add by benjaminwan
if (envStr != NULL) {
const char* stockenv = getenv("BOOTCLASSPATH");
//ALOGW("BJW Stock BOOTCLASSPATH = %s",stockenv);
const char* myaddclass = ":/data/app/com.miui.sdk-1.apk:/data/app/com.miui.sdk-2.apk:/system/app/miui.apk:/data/app/com.miui.framework-1.apk:/data/app/com.miui.framework-2.apk:/system/app/MiuiFramework.apk";
int len=strlen(stockenv)+strlen(myaddclass)+1;
char* result=(char *)malloc(len);
snprintf(result, len, "%s%s", stockenv, myaddclass);
envStr = result;
//ALOGW("BJW OEM BOOTCLASSPATH = %s",envStr);
}
//add by benjaminwan
if (envStr != NULL) {
gDvm.bootClassPathStr = strdup(envStr);
} else {
gDvm.bootClassPathStr = strdup(".");
}