2012-07-28 78 views
2

我是NDK的新手。Android-NDK「java.lang.UnsatisfiedLinkError」

我有具有以下功能

/* This is a trivial JNI example where we use a native method 
* to return a new VM String. See the corresponding Java source 
* file located at: 
* 
* apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java 
*/ 
JNIEXPORT jstring JNICALL 
Java_com_some_player_MainActivity_stringFromJNI(JNIEnv* env, 
                jobject thiz) 
{ 
    return env->NewStringUTF("Hello from JNI!"); 
} 

調用它的Java類

package com.some.player; 
public class MainActivity extends Activity { 
    public native String stringFromJNI(); 
    static { 
     System.loadLibrary("hello-jni"); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TextView tv = (TextView) findViewById(R.id.textView); 
     tv.setText(stringFromJNI()); 
    } 
} 

make文件

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS) 

LOCAL_MODULE := hello-jni 
LOCAL_SRC_FILES := hello-jni.cpp 

include $(BUILD_SHARED_LIBRARY) 

的問題是,一個CPP文件時,我調用本地函數我得到了

07-28 23:42:34.256: E/AndroidRuntime(32398): java.lang.UnsatisfiedLinkError: stringFromJNI 
+0

看看http://stackoverflow.com/questions/4813336/java-lang-unsatisfiedlinkerror – user827992 2012-07-28 21:54:01

+0

謝謝,我已經看到它。 – mohamede1945 2012-07-28 22:09:44

回答

6

其實我想通了,我需要添加

extern "C" { 
    JNIEXPORT jstring JNICALL Java_com_some_player_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz) 
}; 
0

有錯誤的本地代碼方法paremers

return env->NewStringUTF("Hello from JNI!");

更換

return (*env)->NewStringUTF(env, "Hello from JNI !");

+0

謝謝。這只是另一個項目的複製粘貼錯誤。但這不是錯誤。 – mohamede1945 2012-07-28 22:10:02

0

ŧ Ø使用C調用約定在CPP 你可以圍繞方法與

extern "C" { /*methods*/ }; 

如:

#include <jni.h> 
#include <string> 
extern "C" { 
JNIEXPORT jstring JNICALL 
Java_com_test_ndk_ndktest_MainActivity_stringFromJNI(
     JNIEnv *env, 
     jobject /* this */) { 
    std::string hello = "Hello from C++"; 
    return env->NewStringUTF(hello.c_str()); 
} 
JNIEXPORT jint JNICALL 
Java_com_test_ndk_ndktest_MainActivity_add(JNIEnv *env, jobject instance, jint a, jint b) { 
    return (a + b); 
} 
}; 
相關問題