gpt4 book ai didi

java - 在 android : UnsatisfiedLinking Error and Force closing Application 中使用 jni/ndk

转载 作者:行者123 更新时间:2023-11-30 11:39:14 25 4
gpt4 key购买 nike

我正在 ndk/jni android 上试用 Hello World 程序。

这是我的java代码:

package com.example.myndkapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
// load the library - name matches jni/Android.mk
static {
Log.d("TAG1","Before Load");
System.loadLibrary("ndkfoo");
Log.d("TAG2","After Load");
}

// declare the native code function - must match ndkfoo.c
private native String invokeNativeFunction();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("TAG3","Set Content View Called");
// this is where we call the native code
try{
String hello = invokeNativeFunction();
Log.d("TAG4","InvokeFunc Called..");
new AlertDialog.Builder(this).setMessage(hello).show();
}catch(Exception e){
e.printStackTrace();
}


}
}

ndkfoo.c代码:

#include <string.h>
#include <jni.h>

jstring Java_com_example_ndkfoo_MAinActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}

我的 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)

我的 AndroidMainfest.xml 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myndkapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

我收到错误 UnsatisfiedLinking Error 并且应用程序强制关闭。另外,我得到了 no implementation found for native function。

最佳答案

您的包名称不完全相同。将 ndkfoo.c 中的名称更改为

 #include <string.h>
#include <jni.h>

jstring Java_com_example_myndkapp_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}

关于java - 在 android : UnsatisfiedLinking Error and Force closing Application 中使用 jni/ndk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com