找不到本机方法:Package.methodName :()Ljava / lang / String;

时间:2015-05-11 10:08:34

标签: java android android-ndk

我在android ndk开发上遇到了麻烦。我是ndk的新手并且遵循教程。在编译时,没有错误,但是当我在设备中上传应用程序时,它给我一个错误Native Method Not Found。我附上了我的包的代码和快照。

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

Application.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

1 个答案:

答案 0 :(得分:2)

JNI使用其名称进行本机方法搜索。为了确保它找到的导出函数是正确的,必须按照以下方案命名方法:

Java_package_name_className_methodName

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

如果您将代码段放在此文件中,则以下代码段应该没问题。

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;
}

<小时/> 要修复问题,请在第15行的com_testing_ndk_FibLib.h中更改签名的返回类型

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

和第10行的FibLib.java:

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

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