Jni return string

hi,
i will work on a project and return android version number from android to cocos2dx(cpp)
and i will done with the help of jni but it rteurn many time wrong string,and i think
it’s rasion utf format, so if any one return string android to cocos2dx than help me
as soon as possible.

I did it other way around before, java calling c++ pass the string,
http://stackoverflow.com/questions/8940060/java-calling-c-with-jni-why-does-the-jnienv-pointer-get-dereferenced-twice

hi,
i will use
Java:- /* return current version no /
public static String getVersion {
try {
return .getPackageInfo,
0).versionName);
} catch {
return “1.0.0”;
}
}
————————————————————-
cpp:-
char
getVerisonNo(void)
{
#if(CC_TARGET_PLATFORM CC_PLATFORM_WIN32)
{
CCLOG(“version”);
}
#elif(CC_TARGET_PLATFORM CC_PLATFORM_ANDROID)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, “util/AdHelper”, “getVersion”, “()Ljava/lang/String;”)) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
return (char*)JniHelper::jstring2string(str).c_str();
}
}
#endif
return “1.0.0”;
}

it’s work fine but many tim change tha string because java use UTF-16 and cpp use UTF-8.

mohit parihar wrote:

hi,
i will use
Java:- /* return current version no /
public static String getVersion {
try {
return .getPackageInfo,
0).versionName);
} catch {
return “1.0.0”;
}
>
}
————————————————————-
cpp:-
>
char
getVerisonNo(void)
{
#if(CC_TARGET_PLATFORM CC_PLATFORM_WIN32)
{
CCLOG(“version”);
}
#elif(CC_TARGET_PLATFORM CC_PLATFORM_ANDROID)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, “util/AdHelper”, “getVersion”, “()Ljava/lang/String;”)) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
return (char*)JniHelper::jstring2string(str).c_str();
}
}
#endif
return “1.0.0”;
}
>
it’s work fine but many tim change tha string because java use UTF-16 and cpp use UTF-8.

Hi mohit, Are you working on cocos2d-x 3.0? I also want to get app version with jni, but cocos2d-x 3.0 using Native Activity now. I don’t know how to do to get getPackageInfo. I saw you using ‘me.getPackageManager()’ in a static function. How is the AdHelper work? It have troubled me for a few days, can you help me ? Thank you very much!

hi Eric Wo,
AdHelper in a class that’s crated by me, so you create your own file
that contain a method and change the path in jni method and it’s work fine.
other wise :- you create a a method in your main activity file(copy from my
code) and give the path of your package in jni method (JniHelper::getStaticMethodInfo(t, “your path”, “getVersion”, “()Ljava/lang/String;”))

mohit parihar wrote:

hi Eric Wo,
AdHelper in a class that’s crated by me, so you create your own file
that contain a method and change the path in jni method and it’s work fine.
other wise :- you create a a method in your main activity file(copy from my
code) and give the path of your package in jni method (JniHelper::getStaticMethodInfo(t, “your path”, “getVersion”, “()Ljava/lang/String;”))

Thank you, mohit parihar. I am a newbie in Java and now work on cocos2d-x 3.0. Where can I find main activity file in 3.0? Because there is no any java files in MyGame/proj.android/src folder.

hi,
you port your project in android or not…? in src folder you get your projectname_Activity
file, please clean and build your porject in eclipse than if its build successfully than
its generate a file in src. if you have gmail/ fb account give me , we disscuss it.

hi, I have port my project in Eclipse and run it successfully on device. For 2.x, I know there is an file named MyGameName.java in src folder, but I cannot find it on 3.0 alpha which used Native Activity. My gmail/fb account is gmail.com. Hope to chat with you.