美文网首页
android 替换应用classloader

android 替换应用classloader

作者: couriravant | 来源:发表于2023-07-03 15:25 被阅读0次

    想尝试一种新的热修复方式,替换应用的ClassLoader,在loadclass中如果碰到修复类存在,则替换掉原来的类,但是失败了,记录下历程:
    首先自定义一个ClassLoader

    public class MyPathClassLoader extends BaseDexClassLoader {
        private static final String TAG = "MyPathClassLoader";
        public MyPathClassLoader(String dexPath, ClassLoader parent) {
            super(dexPath, null, null, parent);
            Log.d(TAG, "dexpath1: "+dexPath);
        }
    
        public MyPathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent) {
            super(dexPath, null, librarySearchPath, parent);
            Log.d(TAG, "dexpath2: "+dexPath + " librarySearchPath:"+ librarySearchPath);
        }
    
        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            Log.d(TAG, "loadClass: "+name);
            return super.loadClass(name);
        }
    
    
        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            Log.d(TAG, "loadClass : "+name +" resolve:"+resolve);
            return super.loadClass(name, resolve);
        }
    
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            Log.d(TAG, "findClass: "+name);
            return super.findClass(name);
        }
    
        @Override
        public String findLibrary(String name) {
            Log.d(TAG, "findLibrary: "+name);
            return super.findLibrary(name);
        }
    }
    
    

    在application的attachBaseContext方法中替换classloader,替换除了application之外的所有类的classloader,

    override fun attachBaseContext(base: Context) {
            super.attachBaseContext(base)
    
            val appInfo: ApplicationInfo = applicationInfo
            val apkPath = appInfo.sourceDir
         val classLoader =
                MyPathClassLoader(apkPath, classLoader)
            hookPackageClassLoader(base, classLoader)
    }
    
     private fun hookPackageClassLoader(context: Context, appClassLoaderNew: ClassLoader): Boolean {
            try {
                val packageInfoField: Field =
                    Class.forName("android.app.ContextImpl").getDeclaredField("mPackageInfo")
                packageInfoField.isAccessible = true
                val loadedApkObject: Any = packageInfoField.get(baseContext)
                var LoadedApkClass = Class.forName("android.app.LoadedApk")
    //            val getClassLoaderMethod: Method = LoadedApkClass.getDeclaredMethod("getClassLoader")
    //            val appClassLoaderOld = getClassLoaderMethod.invoke(loadedApkObject) as ClassLoader
                val appClassLoaderField: Field = LoadedApkClass.getDeclaredField("mClassLoader")
                appClassLoaderField.isAccessible = true
                appClassLoaderField.set(loadedApkObject, appClassLoaderNew)
                return true
            } catch (ignored: Throwable) {
                Log.d("MyPathClassLoader", "error:"+ignored.message)
            }
            return false
        }
    
    

    但是在MainActivity测试效果时发现TestA类,并没有由自定义的类加载器加载,而是PathClassLoader加载的,应该是由于android N混合编译,安装过程中提前加载了部分热点类导致的,实验失败。。

      override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            Log.d("MyPathClassLoader", "testa classloader:"+TestA().javaClass.classLoader.toString())
    
            TestA().doSomething()
            Log.d("MyPathClassLoader", "classcloader:"+MainActivity.javaClass.classLoader.toString())
    

    如果继续下去,应该跟tinker一样了,所以没有继续。如果想尝试asm直接更新编译后的class,应该会导致应用校验问题,其他的java后端的hotreload方法,由于android使用的不是jvm虚拟机也无法使用,比如JRebel和HotSwap,大家还有啥好的想法吗
    refer:https://cloud.tencent.com/developer/article/1012644
    https://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=2649286341&idx=1&sn=054d595af6e824cbe4edd79427fc2706&scene=0#wechat_redirect

    相关文章

      网友评论

          本文标题:android 替换应用classloader

          本文链接:https://www.haomeiwen.com/subject/rwnqudtx.html