Kur*_*ran 15 android android-ndk
我正在尝试使用本机代码构建android应用程序,所以我想测试ndk是否成功运行.当我尝试运行我的第一个hello world项目日志cat时,
01-21 23:30:06.780: E/AndroidRuntime(939): FATAL EXCEPTION: main
01-21 23:30:06.780: E/AndroidRuntime(939): java.lang.UnsatisfiedLinkError:
Native method not found: com.example.ndktesting.MainActivity.invokeNativeFunction:()Ljava/lang/String;
Run Code Online (Sandbox Code Playgroud)
我检查了一些stackoverflow的答案,但找不到我的答案.这是我的java代码,我正在使用android ndk r8d版本.
//ndktest.c
#include <string.h>
#include <jni.h>
extern "C"{
JNIEXPORT jstring JNICALL Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject thiz)
};
JNIEXPORT jstring JNICALL Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject thiz){
return (*env)->NewStringUTF(env, "Hello from native code!");
}
Run Code Online (Sandbox Code Playgroud)
这是我的MainActivity java代码
package com.example.ndktesting;
public class MainActivity extends Activity {
//declare the native code function - must match ndktest.c
private native String invokeNativeFunction();
public native String unimplementedinvokeNativeFunction();
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("ndktest");
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is where we call the native code
String hello = invokeNativeFunction();
new AlertDialog.Builder(this).setMessage(hello).show();
}
}
Run Code Online (Sandbox Code Playgroud)
Android make文件代码:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := ndktest.c
include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)
Chr*_*ton 35
您的包/类名称不匹配.
JNIEXPORT jstring JNICALL Java_com_example_ndktesting_ndktest_MainActivity_invokeNativeFunction(JNIEnv* env, jobject thiz)
Run Code Online (Sandbox Code Playgroud)
将是班上的一种方法
com.example.ndktesting.ndktest.MainActivity
Run Code Online (Sandbox Code Playgroud)
但是你的实际代码
package com.example.ndktesting;
public class MainActivity extends Activity
Run Code Online (Sandbox Code Playgroud)
导致它寻找
com.example.ndktesting.MainActivity.invokeNativeFunction
Run Code Online (Sandbox Code Playgroud)
没有"ndktest"
一旦你的名字匹配,它应该工作,或暴露下一个问题.
| 归档时间: |
|
| 查看次数: |
27619 次 |
| 最近记录: |