gpt4 book ai didi

android - 找不到 JNI so 文件

转载 作者:行者123 更新时间:2023-11-30 04:05:43 29 4
gpt4 key购买 nike

我想用一些代码创建 jni 示例,但它在 logcate 中显示错误

07-30 18:34:48.720: W/dalvikvm(3484): Exception Ljava/lang/UnsatisfiedLinkError; thrown during Lcom/org/HelloNDK/HelloNDK;.<clinit>
07-30 18:34:48.740: W/dalvikvm(3484): Class init failed in newInstance call (Lcom/org/HelloNDK/HelloNDK;)
07-30 18:34:48.740: D/AndroidRuntime(3484): Shutting down VM
07-30 18:34:48.760: W/dalvikvm(3484): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 18:34:48.810: E/AndroidRuntime(3484): FATAL EXCEPTION: main
07-30 18:34:48.810: E/AndroidRuntime(3484): java.lang.ExceptionInInitializerError
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.Class.newInstanceImpl(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.Class.newInstance(Class.java:1429)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.os.Looper.loop(Looper.java:123)
07-30 18:34:48.810: E/AndroidRuntime(3484): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 18:34:48.810: E/AndroidRuntime(3484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 18:34:48.810: E/AndroidRuntime(3484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 18:34:48.810: E/AndroidRuntime(3484): at dalvik.system.NativeStart.main(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484): Caused by: java.lang.UnsatisfiedLinkError: Library libndkfoo not found
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.Runtime.loadLibrary(Runtime.java:461)
07-30 18:34:48.810: E/AndroidRuntime(3484): at java.lang.System.loadLibrary(System.java:557)
07-30 18:34:48.810: E/AndroidRuntime(3484): at com.org.HelloNDK.HelloNDK.<clinit>(HelloNDK.java:12)
07-30 18:34:48.810: E/AndroidRuntime(3484): ... 15 more

代码是:

主要 Activity (java文件)

package com.org.HelloNDK;

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

public class HelloNDK extends Activity {
/** Called when the activity is first created. */

static {
System.out.println("Lib 1");
System.loadLibrary("libndkfoo");
System.out.println("Lib 2");
}

private native String invokeNativeFunction();

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

try {

// this is where we call the native code
String hello = invokeNativeFunction();
new AlertDialog.Builder(this).setMessage(hello).show();

} catch (Exception e) {
// TODO: handle exception
System.out.println("Error in On Create == >"+e.toString());
}
}
}

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)

ndkfoo.c文件代码

#include <string.h>
#include <jni.h>
JNIEXPORT jstring JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj)
{
return (*env)->NewStringUTF(env, "Hello from native code!");
}

.SO 文件是用

创建的
infoware@infoware-PC /cygdrive/d/SecondWorkspace/HelloNDK
$ /cygdrive/d/android-ndk-r8b/ndk-build
Compile thumb : ndkfoo <= ndkfoo.c
SharedLibrary : libndkfoo.so
Install : libndkfoo.so => libs/armeabi/libndkfoo.so

结构项目屏幕

enter image description here

最佳答案

  1. 在您的 Activity 中:重命名 private native String invokeNativeFunction();
    private native String sayHello();
  2. 在你的c文件中:重命名
    JNIEXPORT jstring JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj)

    JNIEXPORT jstring JNICALL Java_com_org_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj)

关于android - 找不到 JNI so 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722045/

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