UnsatisfiedLinkError when trying to call native code

691 views
Skip to first unread message

Luca Carlon

unread,
Jul 9, 2010, 4:56:52 AM7/9/10
to android-ndk
Hi! I tried many times to create a project which calls a native
method. I wrote a native method following the samples:

jstring
Java_com_example_helloluca_HelloLuca_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}

Then, in my java file, I loaded the library (no error, and I checked
the library is correctly placed in the package), and declared the
method:

public native String stringFromJNI();

This is the log:

07-09 08:55:48.043: WARN/dalvikvm(762): No implementation found for
native Lcom/example/helloluca/HelloLuca;.stringFromJNI ()Ljava/lang/
String;

I even checked using arm-eabi-nm and this is what I get:

luca@ubuntu:~/workspace/HelloLuca/libs/armeabi$ ../../../../Android/
android-ndk-r4/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin/arm-eabi-nm
libhello-jni.so
00000b90 T Java_com_example_helloluca_HelloLuca_stringFromJNI
00003140 a _DYNAMIC
00003200 a _GLOBAL_OFFSET_TABLE_
00001b60 T _Unwind_Backtrace
00000d5c T _Unwind_Complete
00000d60 T _Unwind_DeleteException
00001b3c T _Unwind_ForcedUnwind
00000d54 T _Unwind_GetCFA
00001be4 T _Unwind_GetDataRelBase
00000c10 t _Unwind_GetGR
00001bec t _Unwind_GetGR
00001c1c T _Unwind_GetLanguageSpecificData
00001c38 T _Unwind_GetRegionStart
00001bdc T _Unwind_GetTextRelBase
00001ad0 T _Unwind_RaiseException
00001af4 T _Unwind_Resume
00001b18 T _Unwind_Resume_or_Rethrow
00000c84 t _Unwind_SetGR
00000bc4 T _Unwind_VRS_Get
00001670 T _Unwind_VRS_Pop
00000c38 T _Unwind_VRS_Set
00000bb0 t _Unwind_decode_target2
00001b60 T ___Unwind_Backtrace
00001b3c T ___Unwind_ForcedUnwind
00001ad0 T ___Unwind_RaiseException
00001af4 T ___Unwind_Resume
00001b18 T ___Unwind_Resume_or_Rethrow
00001140 T __aeabi_unwind_cpp_pr0
00001138 W __aeabi_unwind_cpp_pr1
00001130 W __aeabi_unwind_cpp_pr2
00003220 A __bss_end__
00003220 A __bss_start
00003220 A __bss_start__
w __cxa_begin_cleanup
w __cxa_call_unexpected
w __cxa_type_match
00003220 D __data_start
00003220 A __end__
00002140 A __exidx_end
00002030 A __exidx_start
000012e8 T __gnu_Unwind_Backtrace
w __gnu_Unwind_Find_exidx
000014cc T __gnu_Unwind_ForcedUnwind
00001540 T __gnu_Unwind_RaiseException
000019f0 T __gnu_Unwind_Restore_VFP
00001a00 T __gnu_Unwind_Restore_VFP_D
00001a10 T __gnu_Unwind_Restore_VFP_D_16_to_31
00001aa8 T __gnu_Unwind_Restore_WMMXC
00001a20 T __gnu_Unwind_Restore_WMMXD
00001604 T __gnu_Unwind_Resume
000015e4 T __gnu_Unwind_Resume_or_Rethrow
000019f8 T __gnu_Unwind_Save_VFP
00001a08 T __gnu_Unwind_Save_VFP_D
00001a18 T __gnu_Unwind_Save_VFP_D_16_to_31
00001abc T __gnu_Unwind_Save_WMMXC
00001a64 T __gnu_Unwind_Save_WMMXD
00001c48 T __gnu_unwind_execute
00001fc4 T __gnu_unwind_frame
00000d80 t __gnu_unwind_pr_common
000019dc T __restore_core_regs
00003220 A _bss_end__
00003220 A _edata
00003220 A _end
00080000 N _stack
U abort
00001148 t get_eit_entry
U memcpy
00001b84 t next_unwind_byte
000019dc T restore_core_regs
0000127c t restore_non_core_regs
00000cc8 t search_EIT_table
00000cb0 t selfrel_offset31
00001c14 t unwind_UCB_from_context
000014e8 t unwind_phase2
000013b0 t unwind_phase2_forced

Any idea why my application crashes reporting that exception?
Thanks!

Christian Linne

unread,
Jul 9, 2010, 12:22:03 PM7/9/10
to andro...@googlegroups.com
Your native code appears like you did not generate a header-file - did you?
The warning tells about not having found a matching function for the given method-signature. It seems that you've implemented one - so just the link to it is missing.

2010/7/9 Luca Carlon <carlo...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
_________
Regards,
christian.l

fadden

unread,
Jul 9, 2010, 1:08:30 PM7/9/10
to android-ndk
On Jul 9, 1:56 am, Luca Carlon <carlon.l...@gmail.com> wrote:
> 07-09 08:55:48.043: WARN/dalvikvm(762): No implementation found for
> native Lcom/example/helloluca/HelloLuca;.stringFromJNI ()Ljava/lang/
> String;
...
> 00000b90 T Java_com_example_helloluca_HelloLuca_stringFromJNI

The declarations and generated symbols look right. Are you sure the
library is getting loaded before first use of the method? Put a
JNI_OnLoad function in the shared lib and write some log messages from
there to make sure it's getting loaded.

Luca Carlon

unread,
Jul 9, 2010, 7:41:44 PM7/9/10
to android-ndk
I solved this problem but I don't know how I did it. It seems that
recompiling the C sources was not sufficient to run the program in the
simulator correctly. It seems to me it is necessary to clear the
project completely after recompiling the C sources and then running
it. This way I'm not having that problem anymore...

Thanks for your answers!

David Turner

unread,
Jul 9, 2010, 8:24:55 PM7/9/10
to andro...@googlegroups.com
You need to rebuild the sources with ndk-build, then rebuild the .apk with ant or Eclipse, then reinstall it into the emulator.

We plan to integrate the NDK into the SDK in the future, which means that you won't have the separate native / .apk steps anymore.
Until then, however, you'll have to use this.

Hope this helps

Luca Carlon

unread,
Jul 10, 2010, 6:58:40 AM7/10/10
to android-ndk
Ok, this is clearer, but now I'm finding myself in a similar situation
and doing this is not solving. I added to my Android.mk another
library, which compiles correctly. This is my Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Neptune
LOCAL_SRC_FILES := ...
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := NeptuneWrapper
LOCAL_SRC_FILES := neptunewrapper.cpp
#LOCAL_SHARED_LIBRARIES := Neptune
include $(BUILD_SHARED_LIBRARY)

I would like to create NeptuneWrapper so that I can access the
functions from my java application. I created a simple function just
like the one I had before (taken from the samples):

jstring Java_com_example_helloluca_HelloLuca_stringFromJNI(JNIEnv*
env, jobject thiz) {
return env->NewStringUTF("Hello World!\n");
}

I compiled this with the C++ compiler and not the C compiler as I want
this library to be able to use the other which is C++.
Now, the debugger says the library NeptuneWrapper is correctly found
(and in fact its there), but that it cannot find an implementation for
stringFromJNI. And again the same exception I reported before. Again
this symbol is correctly found:

00000bac T
_Z50Java_com_example_helloluca_HelloLuca_stringFromJNIP7_JNIEnvP8_jobject

Any idea what can I made wrong this time?

Thanks to everyone for the answers!

On Jul 10, 2:24 am, David Turner <di...@android.com> wrote:
> You need to rebuild the sources with ndk-build, then rebuild the .apk with
> ant or Eclipse, then reinstall it into the emulator.
>
> We plan to integrate the NDK into the SDK in the future, which means that
> you won't have the separate native / .apk steps anymore.
> Until then, however, you'll have to use this.
>
> Hope this helps
>
> On Fri, Jul 9, 2010 at 4:41 PM, Luca Carlon <carlon.l...@gmail.com> wrote:
> > I solved this problem but I don't know how I did it. It seems that
> > recompiling the C sources was not sufficient to run the program in the
> > simulator correctly. It seems to me it is necessary to clear the
> > project completely after recompiling the C sources and then running
> > it. This way I'm not having that problem anymore...
>
> > Thanks for your answers!
>
> > On Jul 9, 7:08 pm, fadden <fad...@android.com> wrote:
> > > On Jul 9, 1:56 am, Luca Carlon <carlon.l...@gmail.com> wrote:
>
> > > > 07-09 08:55:48.043: WARN/dalvikvm(762): No implementation found for
> > > > native Lcom/example/helloluca/HelloLuca;.stringFromJNI ()Ljava/lang/
> > > > String;
> > >  ...
> > > > 00000b90 T Java_com_example_helloluca_HelloLuca_stringFromJNI
>
> > > The declarations and generated symbols look right.  Are you sure the
> > > library is getting loaded before first use of the method?  Put a
> > > JNI_OnLoad function in the shared lib and write some log messages from
> > > there to make sure it's getting loaded.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

Angus Lees

unread,
Jul 10, 2010, 8:48:36 AM7/10/10
to andro...@googlegroups.com
On Sat, Jul 10, 2010 at 20:58, Luca Carlon <carlo...@gmail.com> wrote:
_Z50Java_com_example_helloluca_HelloLuca_stringFromJNIP7_JNIEnvP8_jobject

This is a mangled C++ symbol.  JNI/dalvik only looks for the plain "Java_com_example_helloluca_HelloLuca_stringFromJNI" symbol.  You need to add 'extern "C"' to make the compiler export the symbol without the C++ mangling:

extern "C" jstring Java_com_example_helloluca_HelloLuca_stringFromJNI(JNIEnv*
env, jobject thiz) {
   return env->NewStringUTF("Hello World!\n");
}


(Aside: It is a pity the dalvik jni.h JNIEXPORT macro isn't more useful.  Imo, it should be:
 #define JNIEXPORT __attribute__ ((visibility("default"))) extern "C"
)

 - Gus

Luca Carlon

unread,
Jul 12, 2010, 6:26:49 PM7/12/10
to android-ndk
Works like a charm! I also learned something about mangled symbols :-)
Thanks!

On 10 Lug, 14:48, Angus Lees <al...@google.com> wrote:
Reply all
Reply to author
Forward
0 new messages