《深入探索Android热修复技术原理》读书笔记

2018-11-01  本文已影响0人  CyanStone
热修复概述

热替换代码修复
Field classLoaderField = Class.class.getDeclaredField("classLoader");
classLoaderField.setAccessible(true);
classLoaderField.set(newClass, oldClass.getClassLoader);
class A<T> {
    private T t;
    public T get();
    public void set(T t) {
        this.t = t;
    }
}

class B extends A<Number> {
    private Number n;
    @override
    public Number get() {
        return n;
    }
    @override
    public void set(Number n) {
        this.n = n;
    }
}

冷启动代码修复
struct ClassObject : Object {
    //...
    u4  accessFlags;
    //...
}

//pre-verified标志的定义是:
CLASS_ISPREVERIFIED = (1<<16);

//我们只需要在JNI层清楚它就行:
clazzObj->accessFlags &=~CLASS_ISPREVERIFIED;
Application.attachBaseContext ->  ContentProvider.onCreate   -> Application.onCreate   ->   Activity.onCreate

资源热修复技术
struct ResChunk_header
{
    uint16_t type;
    uint16_t headerSize;
    uint32_t size;
}

so库热修复技术
public class MainActivity extends Activity {
    static {
        System.loadLibrary("jnitest");
    }
    public static native String stringFromJNI();
    public static native void test;
}

//静态注册stringFromJNI方法
extern "C" jstring Java_com_test_example_MainActivity_stringFromJNI(JNIEnv *env, jclass clazz) {
    std::string hello = "jni stringFrom old....";
    return env->NewStrignUTF(hello.c_str());
}

//动态注册text方法
void test(JNIEnv *env, jclass clazz) {
    LOGD(jni test old .....);
}
JNINativeMethod nativeMethods[] = {
    {"test" , "()V", (void*) test}
};
#define JNIREG_CLASS "com/test/example/MainActivity";
JNIEXPORT jnint JNICALL JNI_OnLoad(javaVM *vm, void *reserved) {
    LOGD("lod JNI_OnLoad");
    ...
    jclass claz = env->FindClass(JNIREG_CLASS);
    if(env->RegisterNatives(claz, nativeMethods, sizeof(nativeMethods)/sizeof(nativeMethods[0])) != JNI_OK)) {
     return JNI_ERR;
  }
  return JNI_VERSION_1_4;
}

上一篇下一篇

猜你喜欢

热点阅读