register_android_os_Binder
register_jni_procs(gRegJNI, NELEM(gRegJNI), env)开始注册jni方法。下面重点查看一下gRegJNI数组的register_android_os_Binder类型。
int register_android_os_Binder(JNIEnv* env) {
if (int_register_android_os_Binder(env) < 0)
return -1;
if (int_register_android_os_BinderInternal(env) < 0)
return -1;
if (int_register_android_os_BinderProxy(env) < 0)
return -1;
......
return 0;
}
首先查看int_register_android_os_Binder
static int int_register_android_os_Binder(JNIEnv* env) {
jclass clazz;
// 查找android/os/Binder
clazz = env->FindClass(kBinderPathName);
// 将Java层Binder类保存到mClass变量
gBinderOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
// 将Binder类的execTransact保存到mExecTransact变量
gBinderOffsets.mExecTransact = env->GetMethodID(clazz, "execTransact", "(IIII)Z");
assert(gBinderOffsets.mExecTransact);
// 将Binder类的mObject保存到mObject变量
gBinderOffsets.mObject = env->GetFieldID(clazz, "mObject", "I");
assert(gBinderOffsets.mObject);
return AndroidRuntime::registerNativeMethods(env, kBinderPathName, gBinderMethods, NELEM(gBinderMethods));
}
const char* const kBinderPathName = "android/os/Binder";
上述方法首先将Java层的Binder类的相关信息保存到gBinderOffsets结构体中,然后对jni方法进行注册。
static struct bindernative_offsets_t {
jclass mClass;
jmethodID mExecTransact;
jfieldID mObject;
} gBinderOffsets;
gBinderOffsets保存好Binder类的信息后为jni层访问Java层提供通道,而且因为gBinderOffsets是全局静态结构体,提高了Binder的查询效率,属于空间换时间的做法。
static const JNINativeMethod gBinderMethods[] = {
/* name, signature, funcPtr */
{ "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid },
{ "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid },
{ "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity },
{ "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity },
{ "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy },
{ "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy },
{ "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands },
{ "init", "()V", (void*)android_os_Binder_init },
{ "destroy", "()V", (void*)android_os_Binder_destroy }
};
AndroidRuntime::registerNativeMethods将为gBinderMethods数组中的方法建立了一一映射关系,为Java层访问JNI层提供通道。
下面继续查看int_register_android_os_BinderInternal -------> android_util_Binder.cpp
static int int_register_android_os_BinderInternal(JNIEnv* env) {
jclass clazz;
// 查找com/android/internal/os/BinderInternal类
clazz = env->FindClass(kBinderInternalPathName);
// 将BinderInternal类保存到mClass对象中
gBinderInternalOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
// 将BinderInternal类中的forceBinderGc方法保存在mForceGc变量
gBinderInternalOffsets.mForceGc = env->GetStaticMethodID(clazz, "forceBinderGc", "()V");
// 注册jni方法
return AndroidRuntime::registerNativeMethods(env, kBinderInternalPathName,
gBinderInternalMethods, NELEM(gBinderInternalMethods));
}
const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal";
上述方法将Java层的BinderInternal类保存在gBinderInternalOffsets结构体中,然后进行jni方法注册。
static struct binderinternal_offsets_t {
jclass mClass;
jmethodID mForceGc;
} gBinderInternalOffsets;
static const JNINativeMethod gBinderInternalMethods[] = {
/* name, signature, funcPtr */
{ "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject },
{ "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool },
{ "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling },
{ "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }
};
AndroidRuntime::registerNativeMethods将为gBinderInternalMethods数组中的方法建立了一一映射关系,为Java层访问JNI层提供通道。
下面继续查看int_register_android_os_BinderProxy
static int int_register_android_os_BinderProxy(JNIEnv* env)
{
......
clazz = env->FindClass(kBinderProxyPathName);
gBinderProxyOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
gBinderProxyOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
gBinderProxyOffsets.mSendDeathNotice
= env->GetStaticMethodID(clazz, "sendDeathNotice", "(Landroid/os/IBinder$DeathRecipient;)V");
gBinderProxyOffsets.mObject = env->GetFieldID(clazz, "mObject", "I");
gBinderProxyOffsets.mSelf = env->GetFieldID(clazz, "mSelf", "Ljava/lang/ref/WeakReference;");
gBinderProxyOffsets.mOrgue = env->GetFieldID(clazz, "mOrgue", "I");
clazz = env->FindClass("java/lang/Class");
gClassOffsets.mGetName = env->GetMethodID(clazz, "getName", "()Ljava/lang/String;");
return AndroidRuntime::registerNativeMethods(env, kBinderProxyPathName,
gBinderProxyMethods, NELEM(gBinderProxyMethods));
}
上述方法与前面分析的方法类似,就不在赘述啦。
static const JNINativeMethod gBinderProxyMethods[] = {
/* name, signature, funcPtr */
{"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder},
{"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive},
{"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor},
{"transactNative", "(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void*)android_os_BinderProxy_transact},
{"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath},
{"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath},
{"destroy", "()V", (void*)android_os_BinderProxy_destroy},
};
jni注册的BinderProxy中的方法如上数组所示。