Java Android NDK:在...跳过初始化中找不到JNI_OnLoad:但是有JNI_OnLoad

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24233825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:59:20  来源:igfitidea点击:

Android NDK : No JNI_OnLoad found in ... skipping init : But there is JNI_OnLoad

javaandroidandroid-ndkjava-native-interface

提问by venky

I am asking this question again because I have to.

我再次问这个问题,因为我必须这样做。

I am having this error while running a NDK based application.

运行基于 NDK 的应用程序时出现此错误。

D dalvikvm: No JNI_OnLoad found in /data/app-lib/com.venky-1/libme.so 0xa5082228, skipping init W dalvikvm: No implementation found for native Lcom/venky/Home;.getPermission:(Landroid/app/Activity;)I

D dalvikvm:在 /data/app-lib/com.venky-1/libme.so 0xa5082228 中找不到 JNI_OnLoad,跳过初始化 W dalvikvm:未找到本机 Lcom/venky/Home 的实现;.getPermission:(Landroid/app/Activity ;)一世

I have been through

我经历过

  1. No JNI_OnLoad found in ... skipping init
  2. No JNI_OnLoad found skipping init > Application shutdown
  3. No JNI_Onload() found and VM shutting down
  4. JNI_OnLoad not found
  1. 在 ... 跳过 init 中找不到 JNI_OnLoad
  2. 没有发现 JNI_OnLoad 跳过 init > 应用程序关闭
  3. 找不到 JNI_Onload() 并且 VM 正在关闭
  4. 未找到 JNI_OnLoad

Their problem was either

他们的问题要么是

  1. They were not using JNI_OnLoad because they used JNI specific naming convention (For example Java_com_venky_Home_start()). Hence this message has no significance as there is an alternative.
  2. They were using C++ and there was function name mangling.
  1. 他们没有使用 JNI_OnLoad,因为他们使用了 JNI 特定的命名约定(例如 Java_com_venky_Home_start())。因此,该消息没有意义,因为存在替代方案。
  2. 他们使用的是 C++,并且有函数名修改。

My case is entirely different. And the above two are invalid in my case. I am using C files. So no mangling. And I am not using JNI kind of function names. I am manually registering the functions using JNI_OnLoad. Hence my conclusion is that "No Implementation Found" is due to the fact that the application is somehow unable to find JNI_OnLoad. But why? It is present in the C file.

我的情况完全不同。以上两个在我的情况下是无效的。我正在使用 C 文件。所以没有损坏。而且我没有使用 JNI 类型的函数名称。我正在使用 JNI_OnLoad 手动注册这些函数。因此,我的结论是“未找到实现”是由于应用程序以某种方式无法找到 JNI_OnLoad。但为什么?它存在于 C 文件中。

Relevant sections of code are as follows. Please ask if you need more.

相关代码段如下。请询问您是否需要更多。

jni/Android.mk

jni/Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE := me
LOCAL_SRC_FILE := me.c
include $(BUILD_SHARED_LIBRARY)

com/venky/Home.java

com/venky/Home.java

package com.venky;

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

public class Home extends Activity {

    static {
        System.loadLibrary("me");
    }


    private native int getPermission(Activity activity);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        getPermission(this);
    }
}

jni/me.c

jni/me.c

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <android/log.h>
#include <jni.h>

#define LOG_TAG "com.venky.me"

#define LOG_D(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOG_F(fn_name) __android_log_write(ANDROID_LOG_DEBUG, LOG_TAG, "Called : " fn_name )

static JavaVM *java_vm;

jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    LOG_F ("JNI_OnLoad");
    java_vm = vm;

    // Get JNI Env for all function calls
    JNIEnv* env;
    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        LOG_D ("GetEnv failed.");
        return -1;
    }

    // Find the class calling native function
    jclass NativeUsb = (*env)->FindClass(env, "com/venky/Home");
    if (class_home == NULL) {
        LOG_D ("FindClass failed : No class found.");
        return -1;
    }

    // Register native method for getUsbPermission
    JNINativeMethod nm[1] = {
        { "getPermission", "(Landroid/app/Activity;)I", get_permission}
    };

    if ((*env)->RegisterNatives(env, NativeUsb, nm , 1)) {
         LOG_D ("RegisterNatives Failed.");
         return -1;
    }

    return JNI_VERSION_1_6;
}

int get_permission (jobject activity)
{
    LOG_F ("get_usb_permission");

}

采纳答案by venky

By help of @AlexCohn, I found out my error. And what a silly stupid error it was.

在@AlexCohn 的帮助下,我发现了我的错误。这是多么愚蠢的错误啊。

jni/Android.mk

jni/Android.mk

  include $(CLEAR_VARS)
  LOCAL_MODULE := me
- LOCAL_SRC_FILE := me.c
+ LOCAL_SRC_FILES := me.c
  include $(BUILD_SHARED_LIBRARY)

Because of this, me.c wasn't compiling. Hence it was not included in the built shared library. I had wasted a couple of hours on this at least.

因此,me.c 没有编译。因此它没有包含在构建的共享库中。我至少在这上面浪费了几个小时。