之前面试百度,那个小哥哥问我,你知道安卓jni有几种方式生成,是怎么实现的,你知道实现的逻辑吗?问题大概是这样。我只回答了JNI怎么实现,实现的逻辑没有看过搭不上来。后面想想也是,随便一个做安卓三年的JNI都可以用的很溜的,你想跟别人有点不一样,还是要看看源码,看看实现的逻辑。深入一点你会得到无限乐趣,不论在哪方面,别只长了年纪没长技能。
结合着chromium来看看安卓的JNI实现。
上一篇介绍过,在AwShellActivity onCreate的时候,会加载so,最后会调用到loadWithSystemLinkerAlreadyLocked函数,通过调用System.loadLibrary跟System.load进行加载,着两个都是系统函数,先看看这两个函数的区别。因为我的debug板是安卓4.4.2的,所以看的安卓源码是安卓4.4.2。以下涉及系统源码的代码路径都是指安卓4.4.2下的路径
private void loadWithSystemLinkerAlreadyLocked(ApplicationInfo appInfo, boolean inZygote) {
......
for (String library : NativeLibraries.LIBRARIES) {
if (!isInZipFile()) {
System.loadLibrary(library);
} else {
// Load directly from the APK.
boolean is64Bit = ApiHelperForM.isProcess64Bit();
String zipFilePath = appInfo.sourceDir;
boolean crazyPrefix = forceSystemLinker(); // See comment in this function.
String fullPath = zipFilePath + "!/"
+ makeLibraryPathInZipFile(library, crazyPrefix, is64Bit);
Log.i(TAG, "libraryName: %s", fullPath);
System.load(fullPath);
}
}
}
先看看System.loadLibrary跟System.load的实现:
/**
* Loads and links the library with the specified name. The mapping of the
* specified library name to the full path for loading the library is
* implementation-dependent.
*
* @param libName
* the name of the library to load.
* @throws UnsatisfiedLinkError
* if the library could not be loaded.
*/
public static void loadLibrary(String libName) {
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
}
/**
* Loads and links the dynamic library that is identified through the
* specified path. This method is similar to {@link #loadLibrary(String)},
* but it accepts a full path specification whereas {@code loadLibrary} just
* accepts the name of the library to load.
*
* @param pathName
* the path of the file to be loaded.
*/
public static void load(String pathName) {
Runtime.getRuntime().load(pathName, VMStack.getCallingClassLoader());
}
代码路径:libcore/luni/src/main/java/java/lang/System.java
从注释上看System.loadLibrary跟System.load的差别,是入参的差别,System.load的入参是全路径,而System.loadLibrary跟的是so不含lib跟so的名称,同样加载libstandalonelibwebviewchromium.so,入参差别如下:
eg:System.loadLibrary(standalonelibwebviewchromium);
System.load(/data/app/org.chromium.android_webview.shell-2/lib/arm/libstandalonelibwebviewchromium.so);
System.loadLibrary函数和System.load函数仅仅在调用参数上有一些区别(java层的代码实现上有一些差别),具体的底层函数实现是一样的,System.loadLibrary函数和System.load函数最终底层都是调用的Native函数Runtime.nativeLoad来实现。两者调用关系如 下,终究是殊途同归。所以还是看看nativeLoad函数做了什么。
可以在dalvik/vm/native/java_lang_Runtime.cpp文件找到nativeLoad函数对应的JNI函数。
const DalvikNativeMethod dvm_java_lang_Runtime[] = {
{ "freeMemory", "()J",
Dalvik_java_lang_Runtime_freeMemory },
{ "gc", "()V",
Dalvik_java_lang_Runtime_gc },
{ "maxMemory", "()J",
Dalvik_java_lang_Runtime_maxMemory },
{ "nativeExit", "(I)V",
Dalvik_java_lang_Runtime_nativeExit },
{ "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;",
Dalvik_java_lang_Runtime_nativeLoad },
{ "totalMemory", "()J",
Dalvik_java_lang_Runtime_totalMemory },
{ NULL, NULL, NULL },
};
如果有兴趣,可以根据下图,看看dvm_java_lang_Runtime的注册流程:
代码路径是:
dalvik/dalvikvm/main.cpp
dalvik/vm/Jni.cpp
dalvik/vm/Init.cpp
dalvik/vm/native/InternalNative.cpp
在dvmInternalNativeStartup进行了虚拟机的基本方法的注册。
接着看nativeLoad干什么了
(1)取需要加载的so库文件的绝对路径;
(2)判断加载的依赖so库文件的文件目录路径是否为空;
(3) 调用dvmLoadNativeCode函数加载目标so库文件。dvmLoadNativeCode函数主要实现是先调用dlopen函数加载目标so库文件,然后通过获取的句柄,调用dlsym到处函数JNI_OnLoad,实现jni函数的注册以及其他的初始化操作等。
static void Dalvik_java_lang_Runtime_nativeLoad(const u4* args,
JValue* pResult)
{
StringObject* fileNameObj = (StringObject*) args[0];
Object* classLoader = (Object*) args[1];
StringObject* ldLibraryPathObj = (StringObject*) args[2];
assert(fileNameObj != NULL);
char* fileName = dvmCreateCstrFromString(fileNameObj);
if (ldLibraryPathObj != NULL) {
char* ldLibraryPath = dvmCreateCstrFromString(ldLibraryPathObj);
void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH");
if (sym != NULL) {
typedef void (*Fn)(const char*);
Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym);
(*android_update_LD_LIBRARY_PATH)(ldLibraryPath);
} else {
ALOGE("android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!");
}
free(ldLibraryPath);
}
StringObject* result = NULL;
char* reason = NULL;
bool success = dvmLoadNativeCode(fileName, classLoader, &reason);
if (!success) {
const char* msg = (reason != NULL) ? reason : "unknown failure";
result = dvmCreateStringFromCstr(msg);
dvmReleaseTrackedAlloc((Object*) result, NULL);
}
free(reason);
free(fileName);
RETURN_PTR(result);
}
如下是dvmLoadNativeCode的实现
(1)判断so是否已经加载过了,如果是,就直接return了;
(2)dlopen so获取句柄,将so Entry添加到动态库的哈希tab种;
(3)根据2中获取的句柄,调用dlsym获取JNI_OnLoad函数的地址,根据获取的地址进行初始化。
到这也就可以明白,加载so是怎么调用到JNI_OnLoad进而实现native函数的动态注册的了。
bool dvmLoadNativeCode(const char* pathName, Object* classLoader,
char** detail)
{
SharedLib* pEntry;
void* handle;
bool verbose;
/* reduce noise by not chattering about system libraries */
verbose = !!strncmp(pathName, "/system", sizeof("/system")-1);
verbose = verbose && !!strncmp(pathName, "/vendor", sizeof("/vendor")-1);
if (verbose)
ALOGD("Trying to load lib %s %p", pathName, classLoader);
*detail = NULL;
/*
* See if we've already loaded it. If we have, and the class loader
* matches, return successfully without doing anything.
*/
pEntry = findSharedLibEntry(pathName);
if (pEntry != NULL) {
if (pEntry->classLoader != classLoader) {
ALOGW("Shared lib '%s' already opened by CL %p; can't open in %p",
pathName, pEntry->classLoader, classLoader);
return false;
}
if (verbose) {
ALOGD("Shared lib '%s' already loaded in same CL %p",
pathName, classLoader);
}
if (!checkOnLoadResult(pEntry))
return false;
return true;
}
/*
* Open the shared library. Because we're using a full path, the system
* doesn't have to search through LD_LIBRARY_PATH. (It may do so to
* resolve this library's dependencies though.)
*
* Failures here are expected when java.library.path has several entries
* and we have to hunt for the lib.
*
* The current version of the dynamic linker prints detailed information
* about dlopen() failures. Some things to check if the message is
* cryptic:
* - make sure the library exists on the device
* - verify that the right path is being opened (the debug log message
* above can help with that)
* - check to see if the library is valid (e.g. not zero bytes long)
* - check config/prelink-linux-arm.map to ensure that the library
* is listed and is not being overrun by the previous entry (if
* loading suddenly stops working on a prelinked library, this is
* a good one to check)
* - write a trivial app that calls sleep() then dlopen(), attach
* to it with "strace -p <pid>" while it sleeps, and watch for
* attempts to open nonexistent dependent shared libs
*
* This can execute slowly for a large library on a busy system, so we
* want to switch from RUNNING to VMWAIT while it executes. This allows
* the GC to ignore us.
*/
Thread* self = dvmThreadSelf();
ThreadStatus oldStatus = dvmChangeStatus(self, THREAD_VMWAIT);
handle = dlopen(pathName, RTLD_LAZY);
dvmChangeStatus(self, oldStatus);
if (handle == NULL) {
*detail = strdup(dlerror());
ALOGE("dlopen(\"%s\") failed: %s", pathName, *detail);
return false;
}
/* create a new entry */
SharedLib* pNewEntry;
pNewEntry = (SharedLib*) calloc(1, sizeof(SharedLib));
pNewEntry->pathName = strdup(pathName);
pNewEntry->handle = handle;
pNewEntry->classLoader = classLoader;
dvmInitMutex(&pNewEntry->onLoadLock);
pthread_cond_init(&pNewEntry->onLoadCond, NULL);
pNewEntry->onLoadThreadId = self->threadId;
/* try to add it to the list */
SharedLib* pActualEntry = addSharedLibEntry(pNewEntry);
if (pNewEntry != pActualEntry) {
ALOGI("WOW: we lost a race to add a shared lib (%s CL=%p)",
pathName, classLoader);
freeSharedLibEntry(pNewEntry);
return checkOnLoadResult(pActualEntry);
} else {
if (verbose)
ALOGD("Added shared lib %s %p", pathName, classLoader);
bool result = true;
void* vonLoad;
int version;
vonLoad = dlsym(handle, "JNI_OnLoad");
if (vonLoad == NULL) {
ALOGD("No JNI_OnLoad found in %s %p, skipping init",
pathName, classLoader);
} else {
/*
* Call JNI_OnLoad. We have to override the current class
* loader, which will always be "null" since the stuff at the
* top of the stack is around Runtime.loadLibrary(). (See
* the comments in the JNI FindClass function.)
*/
OnLoadFunc func = (OnLoadFunc)vonLoad;
Object* prevOverride = self->classLoaderOverride;
self->classLoaderOverride = classLoader;
oldStatus = dvmChangeStatus(self, THREAD_NATIVE);
if (gDvm.verboseJni) {
ALOGI("[Calling JNI_OnLoad for \"%s\"]", pathName);
}
version = (*func)(gDvmJni.jniVm, NULL);
dvmChangeStatus(self, oldStatus);
self->classLoaderOverride = prevOverride;
if (version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 &&
version != JNI_VERSION_1_6)
{
ALOGW("JNI_OnLoad returned bad version (%d) in %s %p",
version, pathName, classLoader);
/*
* It's unwise to call dlclose() here, but we can mark it
* as bad and ensure that future load attempts will fail.
*
* We don't know how far JNI_OnLoad got, so there could
* be some partially-initialized stuff accessible through
* newly-registered native method calls. We could try to
* unregister them, but that doesn't seem worthwhile.
*/
result = false;
} else {
if (gDvm.verboseJni) {
ALOGI("[Returned from JNI_OnLoad for \"%s\"]", pathName);
}
}
}
if (result)
pNewEntry->onLoadResult = kOnLoadOkay;
else
pNewEntry->onLoadResult = kOnLoadFailed;
pNewEntry->onLoadThreadId = 0;
/*
* Broadcast a wakeup to anybody sleeping on the condition variable.
*/
dvmLockMutex(&pNewEntry->onLoadLock);
pthread_cond_broadcast(&pNewEntry->onLoadCond);
dvmUnlockMutex(&pNewEntry->onLoadLock);
return result;
}
}
好了我们来看看chromium的JNI的实现吧,我看的是WebViewInstrumentation.apk也就以它为例。
WebViewInstrumentation.apk的JNI_OnLoad在./android_webview/lib/webview_entry_point.cc文件中进行重写。
// This is called by the VM when the shared library is first loaded.
// Most of the initialization is done in LibraryLoadedOnMainThread(), not here.
JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
base::android::InitVM(vm);
#if defined(WEBVIEW_INCLUDES_WEBLAYER)
if (!weblayer::MaybeRegisterNatives())
return -1;
#endif
base::android::SetNativeInitializationHook(&NativeInit);
return JNI_VERSION_1_4;
}
看看JNI_OnLoad做了什么有兴趣的可以根据下图去跟踪,不同线程起来的时候也会有JNI的注册,可以分线程去看。
如果有兴趣的,也可以想想JNI Java定义的native函数跟c层一一对应是如何实现的。