java Android NDK应用程序抛出错误未满足链接错误
我正在开发基于NDK的Android应用程序。现在我正在学习使用示例“HelloWorld”应用程序。但它每次都会抛出错误请指出错误所在
注意:我所有的cpp文件都只在jni文件夹中
在LogCat中,调试时,它说
08-19 14:52:59.340: W/dalvikvm(469): No implementation found for native Lcom/example/myfirstndkapp/MainActivity;.concateneMyStringWithCppString (Ljava/lang/String;)Ljava/lang/String;
以下是基本代码:
主要活动。java
package com.example.myfirstndkapp;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.*;
public class MainActivity extends ActionBarActivity {
static {
System.loadLibrary("MyFirstNDKApp");
}
private native String concateneMyStringWithCppString(String myString);
private EditText tvGetValue = null;
private TextView tvSetValue = null;
private Button btnCallCPPMethod = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
String str = concateneMyStringWithCppString("OutputString");
System.out.println("output : "+str);
}
}
Android。mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyFirstNDKApp
LOCAL_SRC_FILES := CoreWrapper.cpp
LOCAL_SRC_FILES += Core.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)
应用程序。mk
APP_STL := gnustl_static
APP_ABI := all
核心。h和核心。cpp
//
// Core.h
#ifndef __MyFistNDKApp__Core__
#define __MyFistNDKApp__Core__
#include <iostream>
const char* concateneMyStringWithCppString(const char* myString);
#endif /* defined(__MyFistNDKApp__Core__) */
// Core.cpp
#include "Core.h"
const char* CPP_BASE_STRING = "cpp says hello world to %s";
const char* concateneMyStringWithCppString(const char* myString) {
char* concatenedString = new char[strlen(CPP_BASE_STRING) + strlen(myString)];
sprintf(concatenedString, CPP_BASE_STRING, myString);
return concatenedString;
}
核心包装器。c
// CoreWrapper.c
#include <string.h>
#include <jni.h>
#include "Core.h"
extern "C" {
JNIEXPORT jstring JNICALL com_example_myfirstndkapp_MainActivity_concateneMyStringWithCppString(JNIEnv* env, jobject thiz, jstring myString) {
return env->NewStringUTF(concateneMyStringWithCppString(env->GetStringUTFChars(myString, 0)));
}
}
但在运行时,它在main活动中的以下行上抛出错误:concateneMyStringWithCppString。java甚至都不进去。上面写着一个未被识别的。以下是错误日志:
在该方法内部调试时:它导航到UnsatifiedLinkError类方法:
public UnsatisfiedLinkError(String detailMessage) {
super(detailMessage);
}
# 1 楼答案
我找到了那个。h类应该使用javah命令自动生成。每次我们都需要一个java类来更新这个文件
根据jni生成的头文件,我们需要创建它的cpp文件,以便编写代码逻辑
我发现这个教程非常有用。它帮助我发现了我的错误
此外,它还包含清晰的逐步信息
希望这有助于其他正在寻找此类服务的人