How to call JNI Function from a method in C library

牧云@^-^@ 提交于 2021-02-10 12:54:09

问题


I'm currently developping an android app with android studio, for this project i need to use a custom library written in c/c++ .

So i in order to do that i needed to use NDK. The library contain methods that i need to implements in order to have access to specifics fonctions in android

My question is : how can I call my jni method inside the method existing in c which will call a java method to store session key in the android system

So for a pratical exemple since it's maybe not clear:

In a file called lib_exemple.c i have three methods

the first one is the initialisation call to the library (jni to library c) -->working

JNIEXPORT void JNICALL
Java_com_example_libExemple_Libs_ExempleLib_lib_1Exemple_1init
(JNIEnv *env, jobject instance) {
lib_Example_init('0'); // c func of the library
}

then the second one is the jni who call a java method (jni to java) -> working

JNIEXPORT jint
JNICALL  Java_com_example_libExemple_Libs_ExempleLib_lib_1Exemple_1store_1session_1key
    (JNIEnv * env, jobject jobject1, jbyte jbyte1, jchar jchar1, jbyte jbyte2){

jclass clazz = (*env)->FindClass(env, "com/example/libExemple/Libs/ExempleLib");
jmethodID mCurrentActivityId = (*env)->GetMethodID(env, clazz, "KeyStoreSessionKey", "(BCB)I");
jint result = (*env)->CallIntMethod(env, jobject1, mCurrentActivityId, jbyte1, jchar1, jbyte2);

return result;
}

and the third method is the one the library c have in it (library C to jni)

int lib_Exemple_store_session_key(uint8_t Session, P_KEY_ST_T pKey, uint8_t    keyType) {
//i want to call jni func here , so the librairy can access the native android function
return 0;
}

then to configure the ndk in a file called ExempleLib.java i have defined

static {
System.loadLibrary("LibExemple");
}

then the prototype of the initialisation of the library

public native void libExemple_init();

the prototype of the first method in lib_exemple.c

public native int lib_Exemple_store_session_key(byte pKey, char key_length, byte keyIndex);

and the java fonction called by it

protected int KeyStoreSessionKey(byte pKey, char key_length, byte keyIndex) {
    ...
}

my mainActivity contain

ExempleLib LibFunc = new ExempleLib();
Log.d(TAG, "init lib" );
LibFunc.lib_Exemple_init();

My goal is that after initialising my library and call an init function , it can call the first method in the second method of lib_exemple.c (the one used in the library )

thanks

EDIT :

The solution of my problem is when i initialize the lib , i need to save jvm so i can access the JNIenv in the library method directly

jobject Savedinstance = NULL;
static JavaVM *cachedJVM;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
cachedJVM = jvm;
return JNI_VERSION_1_6;
}

JNIEnv *AttachJava() {
    JavaVMAttachArgs args = {JNI_VERSION_1_6, 0, 0};
    JNIEnv *java;
    (*cachedJVM)->AttachCurrentThread(cachedJVM, &java, &args);
    return java;
}

JNIEXPORT jint JNICALL
Java_com_example_vgosselin_libExemple_Libs_ExempleLib_lib_1init(JNIEnv *env,
                                                                                jobject instance) {
Savedinstance = instance;
void *element = 0;
jint result;
result = lib_Exemple_init_();
return result;
}

so i can call the java method in

int lib_Exemple_store_session_key(uint8_t Session, P_KEY_ST_T pKey, uint8_t    keyType) {
JNIEnv *NewEnv = AttachJava();
jclass clazz = (*NewEnv)->FindClass(NewEnv,"com/example/vgosselin/libExemple/Libs/ExempleLib");
jmethodID mCurrentActivityId = (*NewEnv)->GetMethodID(NewEnv,clazz,"KeystoreStoreKey","([BCB)I");
jint result;
jbyteArray Data = 0;
jchar Key = 0;
jbyte Type = 0;

result = (*NewEnv)->CallIntMethod(NewEnv, Savedinstance, mCurrentActivityId, Data, Key, Type);
return result;
}

and now i don't need the method in jni syntax anymore

JNIEXPORT jint JNICALL    Java_com_example_libExemple_Libs_ExempleLib_lib_1Exemple_1store_1session_1key

来源:https://stackoverflow.com/questions/37068624/how-to-call-jni-function-from-a-method-in-c-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!