第一种:
只适合jni编程
ContextWrapper --->字段: mBase
rtn ----> ContextImpl
ContextImpl --->字段:mPackageInfo
rtn ---->LoadedApk
LoadedApk --->字段:mClassLoader
----------------------------------------------------替换自己的dexclassLoader,可实现动态加载----------------------------------------------------
-----以下是代码片段,仅供参考------------------------------------
jclass contextClass = env->FindClass("android/content/ContextWrapper");
jfieldID mBaseFieldID = env->GetFieldID(contextClass, "mBase", "Landroid/content/Context;");
jobject mBase = env->GetObjectField(context, mBaseFieldID);
jclass mBaseClass = env->GetObjectClass(mBase);
jfieldID mPackageInfoFieldID = env->GetFieldID(mBaseClass, "mPackageInfo", "Landroid/app/LoadedApk;");
jobject mPackageInfo = env->GetObjectField(mBase, mPackageInfoFieldID);
jclass mPackageInfoClass = env->GetObjectClass(mPackageInfo);
jfieldID mClassLoaderFieldID = env->GetFieldID(mPackageInfoClass, "mClassLoader", "Ljava/lang/ClassLoader;");
jobject mClassLoader = env->GetObjectField(mPackageInfo, mClassLoaderFieldID);
.....................................................
env->SetObjectField(mPackageInfo, mClassLoaderFieldID, myClassLoader);
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
第二种:
android.app.ActivityThread ---->静态方法,反射获取当前线程 currentActivityThread
Class obj_class = Class.forName("android.app.ActivityThread");
Method method = obj_class.getMethod("currentActivityThread", new Object[] {});
return method.invoke(null, new Object[] {});
currentActivityThread 字段:mPackages --->api <19 可以强转HashMap 否则 ArrayMap
android.app.LoadedApk = HashMap .get(包名)
android.app.LoadedApk 字段:mClassLoader
--------------------------------反射替换------------------------------