gpt4 book ai didi

java - 未找到 native 方法 : Package. methodName:()Ljava/lang/String;

转载 作者:行者123 更新时间:2023-12-02 04:41:07 25 4
gpt4 key购买 nike

我在 android ndk 开发上遇到了麻烦。我对 ndk 很陌生并且正在学习教程。在编译时没有出现错误,但是当我将应用程序上传到设备中时,它给我一个错误:未找到 native 方法。我已附上我的包裹的代码和快照。

AT ndk-build 我得到了这个:

[arm64-v8a] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[arm64-v8a] SharedLibrary : libcom_testing_ndk_FibLib.so
[arm64-v8a] Install : libcom_testing_ndk_FibLib.so => libs/arm64-v8a/libcom_testing_ndk_FibLib.so
[x86_64] Compile : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[x86_64] SharedLibrary : libcom_testing_ndk_FibLib.so
[x86_64] Install : libcom_testing_ndk_FibLib.so => libs/x86_64/libcom_testing_ndk_FibLib.so
[mips64] Compile : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[mips64] SharedLibrary : libcom_testing_ndk_FibLib.so
[mips64] Install : libcom_testing_ndk_FibLib.so => libs/mips64/libcom_testing_ndk_FibLib.so
[armeabi-v7a] Compile thumb : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[armeabi-v7a] SharedLibrary : libcom_testing_ndk_FibLib.so
[armeabi-v7a] Install : libcom_testing_ndk_FibLib.so => libs/armeabi-v7a/libcom_testing_ndk_FibLib.so
[armeabi] Compile thumb : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[armeabi] SharedLibrary : libcom_testing_ndk_FibLib.so
[armeabi] Install : libcom_testing_ndk_FibLib.so => libs/armeabi/libcom_testing_ndk_FibLib.so
[x86] Compile : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[x86] SharedLibrary : libcom_testing_ndk_FibLib.so
[x86] Install : libcom_testing_ndk_FibLib.so => libs/x86/libcom_testing_ndk_FibLib.so
[mips] Compile : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
[mips] SharedLibrary : libcom_testing_ndk_FibLib.so
[mips] Install : libcom_testing_ndk_FibLib.so => libs/mips/libcom_testing_ndk_FibLib.so

FibLib.java

 package com.testing.ndk;

public class FibLib {
static {
System.loadLibrary("com_testing_ndk_FibLib"); // Load native library at runtime
// hello.dll (Windows) or libhello.so (Unixes)
}

// Declare a native method sayHello() that receives nothing and returns void
public static native String sayHello();

// Test Driver
public static void main(String[] args) {
new FibLib().sayHello(); // invoke the native method
}
}

com_testing_ndk_FibLib.c

  #include <jni.h>
#include <stdio.h>
#include "com_testing_ndk_FibLib.h"
// Implementation of native method sayHello() of HelloJNI class

JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}

com_testing_ndk_FibLib.h

    /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_testing_ndk_FibLib */

#ifndef _Included_com_testing_ndk_FibLib
#define _Included_com_testing_ndk_FibLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_testing_ndk_FibLib
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

应用程序.mk

APP_PLATFORM := android-17
APP_ABI :=all

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES :=com_testing_ndk_FibLib.c
LOCAL_MODULE :=com_testing_ndk_FibLib
include $(BUILD_SHARED_LIBRARY)

错误日志:

[arm64-v8a] Compile        : com_testing_ndk_FibLib <= com_testing_ndk_FibLib.c
jni/com_testing_ndk_FibLib.c:6:24: error: conflicting types for 'Java_com_testing_ndk_FibLib_sayHello'
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
^
In file included from jni/com_testing_ndk_FibLib.c:3:0:
jni/com_testing_ndk_FibLib.h:15:27: note: previous declaration of 'Java_com_testing_ndk_FibLib_sayHello' was here
JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_sayHello
^
make: *** [obj/local/arm64-v8a/objs/com_testing_ndk_FibLib/com_testing_ndk_FibLib.o] Error 1

enter image description here

最佳答案

JNI 使用其名称执行 native 方法搜索。为了确保找到的导出函数是正确的,必须根据以下方案命名方法:

Java_package_name_className_methodName

例如,在您的情况下, native 函数的名称必须是 Java_com_testing_ndk_FibLib_sayHello,但在您的 com_testing_ndk_FibLib.c 中没有具有该名称的函数。

如果将以下代码 fragment 放入此文件中,应该没问题。

com_testing_ndk_FibLib.c

#include <jni.h>
#include <stdio.h>
#include "com_testing_ndk_FibLib.h"

// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}

<小时/>要解决您的问题,请更改 com_testing_ndk_FibLib.h 第 15 行的签名返回类型

...
JNIEXPORT void JNICALL Java_com_testing_ndk_FibLib_sayHello
(JNIEnv *, jclass);
...

在 FibLib.java 中的第 10 行:

...
public static native String sayHello();
...

在此处了解 JNI:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html

关于java - 未找到 native 方法 : Package. methodName:()Ljava/lang/String;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164870/

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