I am new in ndk development in android.I have gone through the file system of ndk android. Here, explaining what i have done. 1) i have created a folder named "jni" then 开发者_运维技巧create 2 file named Android.mk and ndkfoo.c.
In Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c
include $(BUILD_SHARED_LIBRARY)
and in ndkfoo.c
#include <string.h>
#include <jni.h>
jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
then i have created NdkFooActivity class, in which i have written
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("ndkfoo");
}
But now when i build from cygwin in xp it creates .so file successfully then i run as android application. It gives me java.lang.UnsatisfiedLinkError in LOGCAT.
So, Please let me know where i am wrong.
Thanks in Advance,
I think you forgot to change the package name.
Java_com_mindtherobot_samples_ndkfoo
It should be your package what you have specified creating project.
Also(just ran into this issue), please note that System.loadLibrary() will always throw an exception if you are testing on the Intel Atom x86 emulator. It works just fine on regular Android emulators and debugging on a physical device.
Although this has not been the OP's problem, I had the same java.lang.UnsatisfiedLinkError because of missing
static {
System.loadLibrary("mylibraryname");
}
There's a good chance the signature is wrong, as others have mentioned.
If you run the javah utility, you can find the exact signature. From the bin folder in your project, where the .apk is and the root of the Java class hierarchy is generated, run:
javah -o jni_sig.h com.mindtherobot.whatever.your.package.is.NdkFooActivity
...and, if you got the package name and class name correct, it will write out a header (called jni_sig.h) with the correct function signature(s) for any native functions. Copy that to your header and .c file, adding parameters as needed, and it should work correctly.
Maybe not relevant anymore but as far as I know you also need to add the "lib" prefix to your native library name. In your case you need to change the Android.mk to "LOCAL_MODULE := libndkfoo" and keep "System.loadLibrary("ndkfoo");" as it is. Check the ndk sample code.
I'm pretty sure that should be:
JNIEXPORT jstring JNICALL Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
Which SDK are you targeting and which version of the NDK do you have? Does the error you're getting say that it couldn't load the library at all or that there was an unimplemented method? Either way make sure you don't have android:hasCode="false" set on the application tag in your manifest.
You can also open up the APK file after a build using winrar or something similar to make sure that the libndkfoo.so file is actually being included with the package.
Either way if you aren't declaring the native function in NdkFooActivity you will get that error, i.e.
public static native String invokeNativeFunction();
JNIEXPORT jstring JNICALL Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
the problem is that you compile for a target processor and execute in other. if you compile in ARM(armeabi) then execute in armeabi based emulator. create a file called application.mk in same folder as Android.mk and put inside it one of this:
- APP_ABI := x86
- APP_ABI := armeabi
- APP_ABI := mips
- APP_ABI := armeabi x86 mips //to compile in all target and you will get 3 *.so files
then compile->run. it should work.
Create a file Application.mk in jni folder.Copy following line and paste it to Application.mk and save.Now build the project with your cgywin and run again
APP_ABI := armeabi armeabi-v7a
The method name Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction
may be not same as that of your package name or class name.
To make this naming of method exactly same you must use javah
.
This will make a header file which will be having the same method name that is required.To make this header file go to the classes folder in the bin of your project(make sure you have created the java file with static method and build it properly) by this command in your terminal
~/workspace/Android_Example2/bin/classes$
In this directory write the following command
sudo javah -jni com.NDK.android_example2.MainActivity
Change the package name and class name according to your project.This will create a com_NDK_android_example2_MainActivity.h in your classes folder.
Simply move this file into your jni
folder. In this file, there will be static methods that you have created in the MainActivity.java file but they are just declared not implemented that you will implement in your C
file.
NOTE: While Coping the method check that the method parameters are need to be declared, so make them declare in your C
file.
Hope this help.
Replace this
Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction
With
Java_your_packege_name_your_Activity_Name_invokeNativeFunction
Example if your package is com.pack and Activity Name name is MainActivity then
Java_com_pack1_MainActivity_invokeNativeFunction
Don't forget to add reference in Activity.
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("ndkfoo");
}
public native String invokeNativeFunction();
Repeat all these step it should work :)
I also had a java.lang.UnsatisfiedLinkError
error. I verified everything mention in above answers but was still getting the error. I eventually discovered that the JNI method names cannot have underscores.
Example:
Java_com_example_app_NativeLib_print_out_stuff
<- generates java.lang.UnsatisfiedLinkError: print_out_stuff
Rename the print_out_stuff function to something without underscores:
Java_com_example_app_NativeLib_printOutStuff
<- works
here's a tutorial how to use native code: here
make sure you dont have any spaces in your project path. also you cant use an underscore in your package or project name.
精彩评论