android替换当前应用的ClassLoder两种方式


第一种:

  只适合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

  --------------------------------反射替换------------------------------

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM