Problem running c++ hello-jni sample code

607 views
Skip to first unread message

fosimoes

unread,
Aug 27, 2009, 9:49:28 AM8/27/09
to android-ndk
Hello,

i tried to compile and run the "hello-jni" sample as C++ instead of C.
In order to do so, I had to:

1) rename the hello-jni.c to hello-jni-cpp

2) change the native source code: The original code was:
jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz ){
return (*env)->NewStringUTF(env, "Hello 1");}
and here is the modified one:
extern C{
jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv*
env,
jobject thiz ){
return (env)->NewStringUTF("Hello 1");}
}

It compiles, but when trying to execut the example, I get the
following message from the VM:
"The application HelloJni (process.com.example.hellojin) has stopped
unexpectedly. Please try again."
If I run the origina C code, everything works fine.
Any idea of what is going on?

fadden

unread,
Aug 27, 2009, 3:27:08 PM8/27/09
to android-ndk
On Aug 27, 6:49 am, fosimoes <fosimoe...@gmail.com> wrote:
> It compiles, but when trying to execut the example, I get the
> following message from the VM:
> "The application HelloJni (process.com.example.hellojin) has stopped
> unexpectedly. Please try again."
> If I run the origina C code, everything works fine.

What does the logcat output show?

fosimoes

unread,
Aug 28, 2009, 8:14:11 AM8/28/09
to android-ndk


On Aug 27, 4:27 pm, fadden <fad...@android.com> wrote:
>
> What does the logcat output show?

Here is what the logcat says:
08-28 09:02:20.371: INFO/ActivityManager(570): Start proc
com.example.hellojni for activity com.example.hellojni/.HelloJni:
pid=746 uid=10020 gids={}
08-28 09:02:21.212: DEBUG/dalvikvm(746): Trying to load lib /data/data/
com.example.hellojni/lib/libtestelib.so 0x43597cc0
08-28 09:02:21.221: DEBUG/dalvikvm(746): Added shared lib /data/data/
com.example.hellojni/lib/libtestelib.so 0x43597cc0
08-28 09:02:21.221: DEBUG/dalvikvm(746): No JNI_OnLoad found in /data/
data/com.example.hellojni/lib/libtestelib.so 0x43597cc0
08-28 09:02:21.281: DEBUG/dalvikvm(746): +++ not scanning '/system/lib/
libwebcore.so' for 'stringFromJNI' (wrong CL)
08-28 09:02:21.281: DEBUG/dalvikvm(746): +++ not scanning '/system/lib/
libmedia_jni.so' for 'stringFromJNI' (wrong CL)
08-28 09:02:21.281: WARN/dalvikvm(746): No implementation found for
native Lcom/example/hellojni/HelloJni;.stringFromJNI ()V
08-28 09:02:21.281: DEBUG/AndroidRuntime(746): Shutting down VM
08-28 09:02:21.281: WARN/dalvikvm(746): threadid=3: thread exiting
with uncaught exception (group=0x4000fe70)
08-28 09:02:21.281: ERROR/AndroidRuntime(746): Uncaught handler:
thread main exiting due to uncaught exception
08-28 09:02:21.312: ERROR/AndroidRuntime(746):
java.lang.UnsatisfiedLinkError: stringFromJNI
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
com.example.hellojni.HelloJni.stringFromJNI(Native Method)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
com.example.hellojni.HelloJni.onCreate(HelloJni.java:42)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1123)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2231)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2284)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.os.Looper.loop(Looper.java:123)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
android.app.ActivityThread.main(ActivityThread.java:3948)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
java.lang.reflect.Method.invokeNative(Native Method)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
java.lang.reflect.Method.invoke(Method.java:521)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:782)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-28 09:02:21.312: ERROR/AndroidRuntime(746): at
dalvik.system.NativeStart.main(Native Method)

Apparently, the VM coud not find the native function implementation.
That would be expected if i did not declare it with the extern "C"
directive, but that is not the case. Am I missing something?

David Turner

unread,
Aug 28, 2009, 1:15:11 PM8/28/09
to andro...@googlegroups.com
that is strange, can you use arm-eabi-nm to dump the list exported by your shared library?
maybe the C++ compiler is mangling the name after all

fadden

unread,
Aug 28, 2009, 3:55:05 PM8/28/09
to android-ndk
On Aug 28, 5:14 am, fosimoes <fosimoe...@gmail.com> wrote:
> 08-28 09:02:21.281: WARN/dalvikvm(746): No implementation found for
> native Lcom/example/hellojni/HelloJni;.stringFromJNI ()V
[...]
> Apparently, the VM coud not find the native function implementation.
> That would be expected if i did not declare it with the extern "C"
> directive, but that is not the case. Am I missing something?

Looking at the original HelloJni.java, stringFromJNI is declared to
return a String, not void. Did you change that?

fosimoes

unread,
Aug 31, 2009, 1:25:01 PM8/31/09
to android-ndk
In fact, I changed the function (now it does nothinbg at all), just to
be sure that the string assignment inside the function was not causing
the runtime error.
About the arm-eabi-nm suggestion... I will try to use it and will post
the results as soon as I can.
Reply all
Reply to author
Forward
0 new messages