native函數(shù)沒有指針數(shù)組哦,我看了一下android Q里邊的實現(xiàn),在注冊native函數(shù)的時候,通過反射獲取到j(luò)ava方法地址后,直接將native 函數(shù)的地址,寫入到 對應(yīng)java 方法地址開始往后的4個字節(jié)或8個字節(jié)的內(nèi)存空間里邊。
具體實現(xiàn)在: android/art/runtime/art_method.h
template<typename T>
ALWAYS_INLINE void SetNativePointer(MemberOffset offset, T new_value, PointerSize pointer_size) {
static_assert(std::is_pointer<T>::value, "T must be a pointer type");
const auto addr = reinterpret_cast<uintptr_t>(this) + offset.Uint32Value();
///這邊將native函數(shù)地址進(jìn)行寫入哦
if (pointer_size == PointerSize::k32) {
uintptr_t ptr = reinterpret_cast<uintptr_t>(new_value);
*reinterpret_cast<uint32_t*>(addr) = dchecked_integral_cast<uint32_t>(ptr);
} else {
*reinterpret_cast<uint64_t*>(addr) = reinterpret_cast<uintptr_t>(new_value);
}
}
大致調(diào)用棧為:
SetNativePointer()
SetDataPtrSize()
SetEntryPointFromJniPtrSize()
SetEntryPointFromJni()
RegisterNative()
RegisterNatives()
jniRegisterNativeMethods()
registerNativeMethods()
RegisterMethodsOrDie()
Android JNI(一)——NDK與JNI基礎(chǔ)本系列文章如下: Android JNI(一)——NDK與JNI基礎(chǔ)Android JNI學(xué)習(xí)(二)——實戰(zhàn)JNI之“hello world”Android JNI學(xué)習(xí)(三)...