功能实现:将代码有bug的类通过热修复技术动态替换的效果
demo下载地址:里面有所以代码以及patch包运行即可
http://download.csdn.net/detail/h291850336/9911383
基本介绍:
android的Dalvik/ART虚拟机虽然与标准Java的JVM虚拟机不一样,ClassLoader具体的加载细节不一样,但是工作机制是类似的,也就是说在Android中同样可以采用类似的动态加载插件的功能
参考博客
http://blog.csdn.net/xyang81/article/details/7292380
https://juejin.im/post/5976af6151882510ca6d0559
https://github.com/dodola/HotFix
https://mp.weixin.qq.com/s?__biz=MzI1MTA1MzM2Nw==&mid=400118620&idx=1&sn=b4fdd5055731290eef12ad0d17f39d4a&scene=1&srcid=1106Imu9ZgwybID13e7y2nEi#wechat_redirect
java加载机制
Java默认提供的三个ClassLoader
ClassLoader使用的是双亲委托模型来搜索类的
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
//打印类加载器从哪些地方加载了相关的jar或class文件
System.out.println("-----------appclassloader-----------------------");
// classloaertest获取类加载器
Class classtest = ClassLoaderTest.class;
ClassLoader mainLoader = classtest.getClassLoader();
System.out.println("main class loader : " + mainLoader.toString()); //sun.misc.Launcher$AppClassLoader@63947c6b
//打印 appclassloader 的加载器
URL[] murls = ((URLClassLoader)mainLoader).getURLs();
for (URL url : murls){
System.out.println(url);
}
System.out.println("---------ExtClassLoader-------------------------");
//打印classtest 的parent字段
ClassLoader parentClass = mainLoader.getParent();
System.out.println("parent class : " + parentClass.toString()); //sun.misc.Launcher$ExtClassLoader@3cd1a2f1
//打印 appclassloader 的加载器
URL[] parenturls = ((URLClassLoader)parentClass).getURLs();
for (URL url : parenturls){
System.out.println(url);
}
System.out.println("-------------extclassloader---------------------");
//extclassloader 的parent字段
ClassLoader extclassloaderParent = parentClass.getParent();
Sy