JNI interface for passing string to Native c++?

3,800 views
Skip to first unread message

Abhi

unread,
Jul 29, 2010, 4:17:17 PM7/29/10
to android-ndk
Hi

I am using Native C++ code in my application for which I need to pass
a filename to the Native main() function. I have previously used C
without any issues but c++ implementation is giving me
UnsatisfiedLinkError on the main function. I am not familiar with the
JNI interface to be used with c++. This is how I have it:

From within the android java code:

String filename = 'some filename';
......
main(filename);

..... ....
public native String main(String fileName);

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

}

On Native c++ side, JNI wrapper:

void Java_com_trial_filesys_SimpleTest_main(JNIEnv *env, jobject obj,
jstring filename)
{
const char *fnameptr = env->GetStringUTFChars(filename, NULL); /*
Get the file name */

..... rest of the code

env->ReleaseStringUTFChars(filename, fnameptr);

} //end of main function

The logcat on eclipse shows:


07-29 15:55:17.512: ERROR/AndroidRuntime(9009): Uncaught handler:
thread main exiting due to uncaught exception
07-29 15:55:17.512: ERROR/AndroidRuntime(9009):
java.lang.UnsatisfiedLinkError: main
07-29 15:55:17.512: ERROR/AndroidRuntime(9009): at
com.trial.filesys.SimpleTest.main(Native Method)
........................................

What am I doing wrong?

Sharma

Pedro Lamarão

unread,
Jul 29, 2010, 4:28:04 PM7/29/10
to android-ndk
Abhi wrote:

> On Native c++ side, JNI wrapper:
>
> void Java_com_trial_filesys_SimpleTest_main(JNIEnv *env, jobject obj,
> jstring filename)
> {
> const char *fnameptr = env->GetStringUTFChars(filename, NULL); /*
> Get the file name */
>
> ..... rest of the code
>
> env->ReleaseStringUTFChars(filename, fnameptr);
>
> } //end of main function


A C++ function must be annotated like below to conform to JNI calling
conventions:

extern "C"
void Java_com_trial_filesys_SimpleTest_foo(JNIEnv *env, jobject obj)
{ /* ... */ }

--
P.

Christian Linne

unread,
Jul 30, 2010, 2:46:14 AM7/30/10
to andro...@googlegroups.com
Normally, that should not be the problem, the header you have to include already contains

#ifdef

__cplusplus

extern

"C" {

#endif

//...

#ifdef

__cplusplus

}

#endif

 The problem in your case is a mismatch of the method-signatures:
 
public native String main(String fileName);
 
needs a string to be returned, but
 
 void Java_com_trial_filesys_SimpleTest_main(JNIEnv *env, jobject obj,
jstring filename)
 
returns nothing ( void).
 
It would really surprise me if that has ever worked before.

_________
Regards,

christian.l

Abhi

unread,
Aug 2, 2010, 3:18:01 PM8/2/10
to android-ndk
@Pedro, Thanks a ton. That worked for me!

@Christian, I don't know if that is a problem coz it works for me.

Abhi

ost12666

unread,
Sep 8, 2012, 3:35:11 PM9/8/12
to andro...@googlegroups.com
We just use SWIG to generate all the JNI code and it works great with C and C++

On Friday, September 7, 2012 9:59:58 AM UTC+3, amit wrote:
http://hoodaandroid.blogspot.in/2012/07/working-with-android-ndk-native.html

found this usefully blog that lets you kw how to pass parameters and integer operations on ndk
Reply all
Reply to author
Forward
0 new messages