1

I am using android NDK to use one c++ library in my application. When I run my application, I always got a error message which is java.lang.UnsatisfiedLinkError: Native method not found. I checked my code many times but I did not find anything. Moreover I got another error message which is No rule to make target all.

LOCAL_C_INCLUDES += ${NDKROOT}/sources/cxx-stl/stlport/stlport

LOCAL_PATH := $(call my-dir)

LOCAL_ALLOW_UNDEFINED_SYMBOLS = true
include $(CLEAR_VARS)
LOCAL_MODULE := my-module-jni
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*/*.cpp)
include $(BUILD_SHARED_LIBRARY)

extern "C" {
JNIEXPORT jstring JNICALL
Java_com_test_ndk_MyClassName_test
(JNIEnv *env, jobject obj)
{
    return env->NewStringUTF("Hello from C++ over JNI!");
}
    }
    public class MyClassName extends Activity{
    public static native String test();
    ......
    }

In my jni folder I have many c++ classes and folders. I think problem is my android.mk file. What should I change in my android.mk file?

SavasCinar
  • 667
  • 3
  • 8
  • 25

2 Answers2

0

Looks like the name of the method is not correct. Can you show us the name of the method you are calling from java and the name of the method in C/C++?

EDIT: This may contain more info: http://www.netmite.com/android/mydroid/2.0/development/ndk/docs/ANDROID-MK.TXT The specification states LOCAL_PATH should be in the first line. Also look for hidden white spaces.

Trax
  • 1,890
  • 12
  • 15
  • Even problem is not function name, I added them. Please focus on android.mk – SavasCinar Dec 19 '12 at 05:20
  • I already put extern "C" but I forgot to write it in question – SavasCinar Dec 19 '12 at 05:58
  • Ok those where the obvious ones. I edited the answer with another try ;) – Trax Dec 19 '12 at 06:23
  • Trax thank you for useful infirmation. I put the local path into first line. But still I got a no target message. Can you help me about white space? – SavasCinar Dec 19 '12 at 06:41
  • If you don't have many .cpp files, try putting them all in the LOCAL_SRC_FILES instead of using a wildcard. If that doesn't help then I'm out of ideas. About the white spaces sometimes there are some white spaces somewhere that break the file. – Trax Dec 19 '12 at 06:47
  • Also, is you .mk file part of an ant build or are you running you .mk from the command line yourself? – Trax Dec 19 '12 at 07:02
  • I used same jni folder in small activity and it works but I put into big project, It did not work – SavasCinar Dec 19 '12 at 12:12
0

Given that you mention in the comments that your code works in a smaller test application but not a larger application make sure the larger application is actually loading the library in the Java Activity like so:

// Load the .so
static {
    System.loadLibrary("libmy-jni-module");
}
Ryan Maloney
  • 552
  • 4
  • 5
  • when I try System.loadLibrary("libmylib.so") , I got error message "Libray can not find". If I try System.loadLibrary("mylib"), I got error message " native method not found". I can see libmylib.so file in my project directory – SavasCinar Dec 19 '12 at 17:31
  • Good catch, I freehanded the .so in their incorrectly from memory. Can you post the declaration of the native method as it appears in your Java source file including the name and package of the class in which it resides? – Ryan Maloney Dec 19 '12 at 17:37
  • Sorry I didnt understand. What should I post? I wrote method declaration in question. – SavasCinar Dec 19 '12 at 18:27
  • My mistake, you already had most of the information I just overlooked it. Just to confirm MyClassName lives in the package com.test.ndk? – Ryan Maloney Dec 19 '12 at 18:36
  • Yes it is in the class. I checked many times.I have many packages like; com.test.ndk, com.test.ndk.helpers, com.test.ndk.drawtings. Do you think it can be problem? – SavasCinar Dec 19 '12 at 18:42
  • I don't think so, based on your answers I'm concerned it's a problem with building. Check to see if the native function is exported from your libmylib.so file like so: nm -D libmylib.so | grep MyClassName_test – Ryan Maloney Dec 19 '12 at 19:08
  • I tried nm -D libmylib.so, I got "invalid argument D". I tried nm -g libmylib.so ,I got nothing like empty, Can you give some advice how can I see inside in mac terminal – SavasCinar Dec 19 '12 at 20:17
  • Looking at the man page for nm on OS X it looks like D is unnecessary and it will work on shared objects by just running nm libmylib.so. I suspect what might be happening is that your Android.mk file is not including the relevant source or header files. If the file containing extern "C" { JNIEXPORT jstring JNICALL Java_com_test_ndk_MyClassName_test (JNIEnv *env, jobject obj) { return env->NewStringUTF("Hello from C++ over JNI!"); } is a header file you should add the containing folder to LOCAL_C_INCLUDES. You may also want to explicitly list all of your LOCAL_SRC_FILES instead of wildcard – Ryan Maloney Dec 19 '12 at 20:28
  • I will type all classes(90-100) instead of using wildcard. – SavasCinar Dec 19 '12 at 20:34
  • 90-100 entries - that is a lot of work, you should probably start by checking to make sure the header file is being included properly and then creating a small test with one header file/source file containing the native method in question and get that working in both projects. – Ryan Maloney Dec 19 '12 at 21:11
  • When I remove wildcard and added files separately, it worked. – SavasCinar Dec 20 '12 at 01:45