Android NativeBridge过程分析

Art的NativeBridge使用hodini作为callback实际去加载类库

其中做了少量的一些操作

1 Zoygote fork应用进程的时候

frameworks/base/core/jni/com_android_internal_os_Zygote.cpp

ForkAndSpecializeCommon函数

bool use_native_bridge = !is_system_server && (instructionSet != NULL)
    && android::NativeBridgeAvailable();
if (use_native_bridge) {
    ScopedUtfChars isa_string(env, instructionSet);
    use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
}

这里判断如果开启了native_bridge 的话 使用函数NeedsNativeBridge替换指令集为当前平台的指令级,然后传递给art虚拟机运行时

2 jvm动态库链接的时候

art/runtime/java_vm_ext.cc

LoadNativeLabrary函数 :

void* handle = dlopen(path_str, RTLD_NOW);
bool needs_native_bridge = false;
if (handle == nullptr) {
    if (android::NativeBridgeIsSupported(path_str)) {
        handle = android::NativeBridgeLoadLibrary(path_str, RTLD_NOW);
        needs_native_bridge = true;
    }
}

先使用dlopen加载,如果加载失败,再尝试使用NativeBridgeLoadLibrary加载(libnb.so) 实则背后工作的是houdini.so

static NativeBridgeCallbacks *get_callbacks()
{
    static NativeBridgeCallbacks *callbacks = nullptr;

    if (!callbacks) { 
        const char *libnb = "/system/" 
        #ifdef __LP64__ 
            "lib64/arm64/"
        #else 
            "lib/arm/" 
        #endif 
        "libhoudini.so"; 
        if (!native_handle) { 
            native_handle = dlopen(libnb, RTLD_LAZY); 
            if (!native_handle) { 
               ALOGE("Unable to open %s", libnb); 
               return nullptr; 
            } 
       }
       callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle,    "NativeBridgeItf"));
    }
    return callbacks;
}

两个重要的参数

ro.dalvik.vm.isa.arm=x86
ro.enable.native.bridge.exec=1
ro.dalvik.vm.native.bridge=libnb.so

你可能感兴趣的:(android,android,android,studio,android-studio)