我尝试使用ndk运行Android应用程序,我可以构建库,但是当我运行时给出异常。
按照代码和错误获取解决方案的帮助
我使用c代码,而不是cpp
primitive.c
#include <jni.h>
#include <android/log.h>
JNIEXPORT jboolean JNICALL Java_cookbook_chapter2_PassingPrimitiveActivity_passBooleanReturnBoolean(JNIEnv *pEnv, jobject pObj, jboolean pBooleanP){
__android_log_print(ANDROID_LOG_INFO, "native", "%d in %d bytes", pBooleanP, sizeof(jboolean));
return (!pBooleanP);}
Acitivity class:PassingPrimitiveActivity.java
public class PassingPrimitiveActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passing_primitive);
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("boolean: ").append(passBooleanReturnBoolean(false))");;
TextView tv = (TextView) findViewById(R.id.display_res);
tv.setText(strBuilder.toString());
}
private native boolean passBooleanReturnBoolean(boolean p);
static {
System.loadLibrary("PassingPrimitive");
} }
logcat文件,有错误。表明没有实现,但在c代码中,有它
06-11 17:08:30.652 29603 29603 E AndroidRuntime: FATAL EXCEPTION: main
06-11 17:08:30.652 29603 29603 E AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.passBooleanReturnBoolean:(Z)Z
06-11 17:08:30.652 29603 29603 E AndroidRuntime: at cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.passBooleanReturnBoolean(Native Method)
06-11 17:08:30.652 29603 29603 E AndroidRuntime: at cookbook.chapter2.passingprimitive.PassingPrimitiveActivity.onCreate(PassingPrimitiveActivity.java:15)
06-11 17:08:30.652 29603 29603 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5184)
我的Android.mk喜欢它:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := PassingPrimitive
LOCAL_SRC_FILES := primitive.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
一切编译好,创建.so文件并复制到我项目下的libs文件夹,但是当我运行时,logcat显示UnsatisfiedLinkError和应用程序崩溃
我可以做些什么来解决它?
答案 0 :(得分:0)
尝试从已编译的java代码生成适当的C头文件,如下所示:
javah -jni -classpath bin/classes com.whatever.PassingPrimitiveActivity
然后在您的C代码中包含该头文件。您在C中定义的函数应与头文件中的函数匹配。映射并不总是你所假设的。
答案 1 :(得分:0)
您的logcat建议本机函数名称应为
Java_cookbook_chapter2_passingprimitive_PassingPrimitiveActivity_passBooleanReturnBoolean