frameworks/base/core/jni/AndroidRuntime.cpp的startReg方法

//extern 关键字的作用是告诉编译器一个变量或函数已经在其他地方定义过了,可以直接使用。
extern int register_android_os_Binder(JNIEnv* env);

1428 static const RegJNIRec gRegJNI[] = {
         ...
1450     REG_JNI(register_android_os_Binder),        
         ...
    }

register_android_os_Binder方法

register_android_os_Binder方法定义在frameworks/base/core/jni/android_util_Binder.cpp类中

1466 int register_android_os_Binder(JNIEnv* env)
1467 {
1468     if (int_register_android_os_Binder(env) < 0)
1469         return -1;
1470     if (int_register_android_os_BinderInternal(env) < 0)
1471         return -1;
1472     if (int_register_android_os_BinderProxy(env) < 0)
1473         return -1;
1474 
1475     jclass clazz = FindClassOrDie(env, "android/util/Log");
1476     gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1477     gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e",
1478             "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I");
1479 
1480     clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor");
1481     gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1482     gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>",
1483                                                                  "(Ljava/io/FileDescriptor;)V");
1484 
1485     clazz = FindClassOrDie(env, "android/os/StrictMode");
1486     gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1487     gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz,
1488             "onBinderStrictModePolicyChange", "(I)V");
1489 
1490     clazz = FindClassOrDie(env, "java/lang/Thread");
1491     gThreadDispatchOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1492     gThreadDispatchOffsets.mDispatchUncaughtException = GetMethodIDOrDie(env, clazz,
1493             "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
1494     gThreadDispatchOffsets.mCurrentThread = GetStaticMethodIDOrDie(env, clazz, "currentThread",
1495             "()Ljava/lang/Thread;");
1496 
1497     return 0;
1498 }

→int_register_android_os_Binder

 961 static const JNINativeMethod gBinderMethods[] = {
 962      /* name, signature, funcPtr */
 963     // @CriticalNative
 964     { "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid },
 965     // @CriticalNative
 966     { "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid },
 967     // @CriticalNative
 968     { "isHandlingTransaction", "()Z", (void*)android_os_Binder_isHandlingTransaction },
 969     // @CriticalNative
 970     { "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity },
 971     { "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity },
 972     // @CriticalNative
 973     { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy },
 974     // @CriticalNative
 975     { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy },
 976     // @CriticalNative
 977     { "setCallingWorkSourceUid", "(I)J", (void*)android_os_Binder_setCallingWorkSourceUid },
 978     // @CriticalNative
 979     { "getCallingWorkSourceUid", "()I", (void*)android_os_Binder_getCallingWorkSourceUid },
 980     // @CriticalNative
 981     { "clearCallingWorkSource", "()J", (void*)android_os_Binder_clearCallingWorkSource },
 982     { "restoreCallingWorkSource", "(J)V", (void*)android_os_Binder_restoreCallingWorkSource },
 983     { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands },
 984     { "getNativeBBinderHolder", "()J", (void*)android_os_Binder_getNativeBBinderHolder },
 985     { "getNativeFinalizer", "()J", (void*)android_os_Binder_getNativeFinalizer },
 986     { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable }
 987 };

 989 const char* const kBinderPathName = "android/os/Binder";
 990 
 991 static int int_register_android_os_Binder(JNIEnv* env)
 992 {
 993     jclass clazz = FindClassOrDie(env, kBinderPathName);
 994 
 995     gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
 996     gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z");
 997     gBinderOffsets.mGetInterfaceDescriptor = GetMethodIDOrDie(env, clazz, "getInterfaceDescriptor",
 998         "()Ljava/lang/String;");
 999     gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
1000 
1001     return RegisterMethodsOrDie(
1002         env, kBinderPathName,
1003         gBinderMethods, NELEM(gBinderMethods));
1004 }

→int_register_android_os_BinderInternal

→int_register_android_os_BinderProxy


0 条评论

发表回复

您的电子邮箱地址不会被公开。