Recently, I want to adjust a project, which uses JNI, and make a note. The environment is: Windows7+eclipse.
Reference sticker:
http://blog.csdn.net/shulianghan/article/details/18964835
Process:
1.Down the simplest project using JNI
https://github.com/han1202012/NDKHelloworld.git
2. Add a batch file to the above project
common problem:
1. The include <string> is missing and the prompt cannot be found
Solution:
http://stackoverflow.com/questions/10373788/c-unresolved-inclusion-iostream
2. Down a project from the company server, there are C++ related parts in the right-click properties, but down a project from the Internet, but can't see the shadow of the CDT plug-in? Why is this?
Later, I copied a .project file from the company's project, and that's it! Thinking about it, it seems that my computer has not installed the CDT plug-in. It is estimated that there is something in this .project file.
3. The program crashes inexplicably, and after searching for a long time, I found out that a function is declared in the java code
native public static boolean sendMessageToOneMobile(String mobile,String msg);
When defining a function in c++, the first two parameters (JNIEnv *env, jobject thiz) must be taken with
Because when calling, the actual parameters will be passed! ! !
JNIEXPORT bool JNICALL Java_shuliang_han_ndkhelloworld_MainActivity_sendMessageToOneMobile
(JNIEnv *env,jobject thiz,jstring mobile,jstring msg ){}
4.native reads the String[] passed by java and stores it as char**
Reference sticker:
http://liuxp0827.blog.51cto.com/5013343/1378277
JNIEXPORT void JNICALL Java_shuliang_han_ndkhelloworld_MainActivity_nativeSendMessageToMobiles(
JNIEnv *env,jobject thiz,jobjectArray mobiles,jstring msg ){
jstring jstr;
jsize arraysize = env->GetArrayLength(mobiles);
const char **pstr = (const char **) malloc(arraysize*sizeof(char *));
for (int i=0 ; i<arraysize; i++) {
jstr = (jstring)env->GetObjectArrayElement(mobiles, i);
pstr[i] = (char *)env->GetStringUTFChars(jstr, 0);
}
const char *nativemsg = env->GetStringUTFChars(msg, 0);
DataTransDemo::sendMessageToMobiles(pstr,arraysize,nativemsg);
env->ReleaseStringUTFChars(msg,nativemsg);
for(int i=0;i<arraysize;i++){
jstr = (jstring)env->GetObjectArrayElement(mobiles, i);
env->ReleaseStringUTFChars(jstr,pstr[i]);
env->DeleteLocalRef(jstr);
}
free(pstr);
}
5.native replaces char** with String[] and passes it to java
Reference sticker:
https://coderanch.com/t/326467/java/Returning-String-array-program-Java
http://zhiweiofli.iteye.com/blog/1830319
bool DataTransDemo::sendMessageToMobiles(const char* mobiles[],const char* msg){
SGJniMethodInfo t;
bool bRlt = true;
if (SGSDKJniHelper::getStaticMethodInfo(t, CLASS_NAME, "sendMessageToMobiles","([Ljava/lang/Object;Ljava/lang/String;)Z")) {
SGLOGD("DataTransDemo::sendMessageToMobiles begin");
jstring strMessage = t.env->NewStringUTF(msg);
jobjectArray mobilearray;
mobilearray= (jobjectArray)t.env->NewObjectArray(size,t.env->FindClass("java/lang/String"),NULL);
for(int i=0;i<size;i++){
jstring mobilestring = t.env->NewStringUTF(mobiles[i]);
t.env->SetObjectArrayElement(mobilearray,i,mobilestring);
t.env->DeleteLocalRef(mobilestring);
}
bRlt = t.env->CallStaticBooleanMethod(t.classID, t.methodID, mobilearray, strMessage);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(strMessage);
t.env->DeleteLocalRef(mobilearray);
SGLOGD("DataTransDemo::sendMessageToMobiles ok");
}else{
SGLOGD("DataTransDemo::sendMessageToMobiles getStaticMethodInfo error");
bRlt = false;
}
return bRlt;
}
6.关于jni里释放资源