Home > Mobile >  Android RPC call Java methods through JNI reverse
Android RPC call Java methods through JNI reverse

Time:02-21

Bosses look where code wrong

#include
#include

# define the TAG "xc"
# define LOGD (... __android_log_print (ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)

JavaVM * JavaVM;//global JVM
JNIEnv * JNIEnv;//global ENV
Jclass globalClass1;//global Jclass1
Jclass globalClass2;//global Jclass2
Jclass jclass1;//global Jclass1
Jclass jclass2;//global Jclass2
JmethodID jmid1;//global method 1
JmethodID jmid2;//global method 1


//method 1 for RPC calls error
Void TestFun1 (char * Message) {
LOGD (" TestFun1 ");
If (javaVm!=NULL) {
LOGD (" TestFun1 obtain JVM success ");
}

If (globalClass1!=NULL) {
LOGD (" TestFun1 class success ");
}

If (jmid1!=NULL) {
LOGD (" TestFun1 jmid1 success ");
}

Int status=(* javaVm) - & gt; AttachCurrentThread (javaVm, & amp; JniEnv, NULL);
LOGD (" TestFun1 get JNIENV ");
If (status=JNI_OK) {
LOGD (" TestFun1 for env success ");
JniEnv jstring STR=(*) - & gt; NewStringUTF (jniEnv, "Testfun1 call");
(jniEnv *) - & gt; CallStaticVoidMethod (jniEnv, globalClass1 jmid1, STR);
}
}



//initialize the success
JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) {
LOGD (" JNI_OnLoad loading ");
JavaVm=vm;
LOGD (" JNI_OnLoadvm obtain complete ");
Int status=(* vm) - & gt; The GetEnv (vm, (void * *) & amp; JniEnv JNI_VERSION_1_6);
If (status==JNI_OK)
{
LOGD (" JNI_OnLoad for env success ");
JniEnv jclass1=(*) - & gt; FindClass (jniEnv, "com/zidong/sotest/MainActivity");
JniEnv globalClass1=(jclass) (*) - & gt; NewGlobalRef (jniEnv jclass1);
JniEnv jmid1=(*) - & gt; GetStaticMethodID (jniEnv jclass1, "showmessage1", "(Ljava/lang/String;) V ");
If (globalClass1!=NULL) {
LOGD (" JNI_OnLoad class success ");
}

If (jmid1!=NULL) {
LOGD (" JNI_OnLoad jmid1 success ");
}
//failure
TestFun1 (" initialization test called ");
}
Return JNI_VERSION_1_6;
}

Void JNI_OnUnload (JavaVM * vm, void * reserved) {
LOGD (" JNI uninstall ");
}

CodePudding user response:

Some Java layer, basic have jni, why reflection adjustable Java interface?

CodePudding user response:

You need to call some JAVA layer function
  • Related