第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

怎么創(chuàng)建一個(gè)動(dòng)態(tài)注冊(cè)項(xiàng)目?

怎么創(chuàng)建一個(gè)動(dòng)態(tài)注冊(cè)項(xiàng)目?

正在回答

1 回答

JNIDynamicLoad.java
public?class?JNIDynamicLoad?{
????static?{
????????System.loadLibrary("dynamic-lib");
????}

????public?native?int?getRandomNum();

????public?native?String?getNativeString();
}


jni_dynamic_load.cpp

//
//?Created?by?admin?on?2020-04-27.
//

#include?<jni.h>
#include?<cassert>
#include?<cstdlib>
#include?<iostream>

using?namespace?std;

jint?get_random_num(JNIEnv?*env)?{
????return?rand();
}

jstring?get_native_string(JNIEnv?*env)?{
????return?env->NewStringUTF("jni動(dòng)態(tài)加載的字符串,嘿嘿嘿");
}

/**
?*?需要注冊(cè)的函數(shù)列表,放在JNINativeMethod類型的數(shù)組中,以后如果需要增加函數(shù),只需在這里添加就行了
?*?參數(shù):
?*?1、java代碼中用native關(guān)鍵字聲明的函數(shù)名字符串
?*?2、簽名(傳進(jìn)來(lái)參數(shù)類型和返回值類型的說(shuō)明)
?*?3、C/C++中對(duì)應(yīng)函數(shù)的函數(shù)名(地址)
?*/
static?JNINativeMethod?getMethods[]?=?{
????????{"getRandomNum",????"()I",??????????????????(void?*)?get_random_num},
????????{"getNativeString",?"()Ljava/lang/String;",?(void?*)?get_native_string}
};

/**
?*?此函數(shù)通過(guò)調(diào)用JNI中?RegisterNatives?方法來(lái)注冊(cè)我們的函數(shù)
?*?@param?env
?*?@param?className
?*?@param?methods
?*?@param?nMethods
?*?@return
?*/
static?int?registerNativeMethods(JNIEnv?*env,?const?char?*className,?const?JNINativeMethod?*methods,
?????????????????????????????????jint?nMethods)?{
????jclass?clazz;
????//找到聲明native方法的類
????clazz?=?env->FindClass(className);
????if?(clazz?==?NULL)?{
????????return?JNI_FALSE;
????}
????if?(env->RegisterNatives(clazz,?methods,?nMethods)?<?0)?{
????????return?JNI_FALSE;
????}
????return?JNI_TRUE;
}

static?int?registerNatives(JNIEnv?*env)?{
????const?char?*className?=?"com/example/jnidemo3/load/JNIDynamicLoad";
????return?registerNativeMethods(env,?className,?getMethods,
?????????????????????????????????sizeof(getMethods)?/?sizeof(getMethods[0]));
}

JNIEXPORT?jint?JNICALL?JNI_OnLoad(JavaVM?*vm,?void?*reserved)?{
????JNIEnv?*env?=?NULL;
????//判斷虛擬機(jī)狀態(tài)是否有問(wèn)題
????if?(vm->GetEnv((void?**)?&env,?JNI_VERSION_1_6)?!=?JNI_OK)?{
????????return?-1;
????}
????assert(env?!=?NULL);
????if?(!registerNatives(env))?{
????????return?-1;
????}
????return?JNI_VERSION_1_6;
}

CMakeList.txt

add_library(
????????dynamic-lib
????????SHARED
????????jni/jni_dynamic_load.cpp
)

target_link_libraries(
????????dynamic-lib
????????${log-lib})

如果還有疑惑,可以看我練習(xí)的代碼
https://github.com/shuiyouwen/JniDemo3

2 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

怎么創(chuàng)建一個(gè)動(dòng)態(tài)注冊(cè)項(xiàng)目?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)