Android 没有发现 JNI_OnLoad 跳过 init > 应用程序关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12626067/
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-20 11:15:05  来源:igfitidea点击:

No JNI_OnLoad found skipping init > Application shutdown

androidandroid-ndkjava-native-interface

提问by Bhuvnesh Pratap

Folks,

各位,

I am working on an android application where I need a third party .so library. I built this third party library (with ndk-build) as per their instructions and was then looking to include this .so in to my Android project.

我正在开发一个需要第三方 .so 库的 android 应用程序。我按照他们的说明构建了这个第三方库(使用 ndk-build),然后希望将这个 .so 包含到我的 Android 项目中。

Therefore I followed the steps described in docs/PREBUILTS.html and successfully build the new .so in the jni/prebuilt directory. Now I tried leveraging the .so facilities by using it in a simple test android app. So what i do is :

因此,我按照 docs/PREBUILTS.html 中描述的步骤在 jni/prebuilt 目录中成功构建了新的 .so。现在我尝试通过在一个简单的测试 android 应用程序中使用它来利用 .so 设施。所以我要做的是:

static {
  Log.i("load so > ","load so");
  System.loadLibrary("xyz");
   }
/* The native functions */
private static native int openFile(String filename);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try{
        String path =  getPathForDownloadDirectoryFile();
        Log.i("file path> ", path);
        int num= openFile(path);
    }catch(Exception e){
        Log.e(">", "could not open the file");
    }
}

Now when I run my app I get a debug message saying : No JNI_OnLoad found in /data/data/com.example.myfirstapp/lib/xyz.so 0x411e6738, skipping init and then the application shuts down.

现在,当我运行我的应用程序时,我收到一条调试消息:在 /data/data/com.example.myfirstapp/lib/xyz.so 0x411e6738 中找不到 JNI_OnLoad,跳过 init,然后应用程序关闭。

For More Info, Here is the error log :

有关更多信息,这是错误日志:

No JNI_OnLoad found in /data/data/com.example.mysecondapp/lib/xyz.so 0x411e67a0,   skipping init
W/dalvikvm(  570): No implementation found for native    Lcom/example/mysecondapp/MainActivity;.openFile:(Ljava/lang/String;)I
D/AndroidRuntime(  570): Shutting down VM
W/dalvikvm(  570): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime(  570): FATAL EXCEPTION: main
E/AndroidRuntime(  570): java.lang.UnsatisfiedLinkError: Native method not found:  com.example.mysecondapp.MainActivity.openFile:(Ljava/lang/String;)I
E/AndroidRuntime(  570):    at com.example.mysecondapp.MainActivity.openFile(Native  Method)
E/AndroidRuntime(  570):    at   com.example.mysecondapp.MainActivity.onCreate(MainActivity.java:31)
E/AndroidRuntime(  570):    at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(  570):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(  570):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime(  570):    at    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(  570):    at android.app.ActivityThread.access0(ActivityThread.java:130)
E/AndroidRuntime(  570):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(  570):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  570):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  570):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(  570):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  570):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(  570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(  570):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  146):   Force finishing activity com.example.mysecondapp/.MainActivity

As I could see that native implementation for the openFile() method was not found but the same xyz.so lib worked pretty neat with the original sample app from the third party. I am pretty much a starter with Android-ndk world.

正如我所看到的,没有找到 openFile() 方法的本机实现,但相同的 xyz.so 库与来自第三方的原始示例应用程序一起工作得非常好。我几乎是 Android-ndk 世界的初学者。

Java-Android-NDK Ninjas ..any guess on what I might be missing ? I'll highly appreciate any help here :)

Java-Android-NDK Ninjas .. 猜猜我可能遗漏了什么?我将非常感谢这里的任何帮助:)

回答by Sunny Kumar Aditya

As guycole said "No JNI_OnLoad" is just a warning , your problem lies elsewhere .

正如 Guycole 所说的“No JNI_OnLoad”只是一个警告,你的问题出在别处。

As you mentioned you successfully compiled your "so" file , the problem may lie in your function signatures inside your c/C ++ code it should be something like this

正如您所提到的,您成功编译了“so”文件,问题可能在于您的 c/C++ 代码中的函数签名,它应该是这样的

JNIEXPORT jint JNICALL Java_com_your_package_class_method(JNIEnv *d, jobject e, jstring f)
{
//some action

}

The function signatures comes from the header file which is generated using javah tool.You need to generate header file and use the function signature with your package name. For different package and class names the header file and corresponding function signature will change .

函数签名来自使用javah工具生成的头文件。您需要生成头文件并将函数签名与您的包名一起使用。对于不同的包名和类名,头文件和相应的函数签名会发生变化。

worked pretty neat with the original sample app from the third party

This might be the reason its running on the sample app and not on your app.

这可能是它在示例应用程序上运行而不是在您的应用程序上运行的原因。

refer: https://thenewcircle.com/s/post/49/using_ndk_to_call_c_code_from_android_apps

参考:https: //thenewcircle.com/s/post/49/using_ndk_to_call_c_code_from_android_apps

回答by guycole

The "No JNI_OnLoad" message is just a warning. JNI_OnLoad is an optional initialization hook.

“No JNI_OnLoad”消息只是一个警告。JNI_OnLoad 是一个可选的初始化钩子。

I guess your problem is inside the openFile() method. Try commenting out the call from Java and see how far you get.

我猜你的问题是在 openFile() 方法中。尝试注释掉来自 Java 的调用,看看你得到了多远。

I have a blog post about JNI and some sample code at http://guycole.blogspot.com/2012/03/yet-another-android-ndk-blog-posting.html- perhaps you will find it useful.

我在http://guycole.blogspot.com/2012/03/yet-another-android-ndk-blog-posting.html 上有一篇关于 JNI 的博客文章和一些示例代码- 也许你会发现它很有用。

Good luck.

祝你好运。

回答by user1749902

It also comes with this log

它还带有这个日志

??-?? ??:??:??.???: INFO/(): java.lang.UnsatisfiedLinkError: Couldn't load *: findLibrary returned null

??-?? ??:??:??.???: INFO/(): java.lang.UnsatisfiedLinkError: 无法加载*: findLibrary 返回 null

right??

对??

I think it's the problem of android.mk files. 1:try to swith to armabi v7. 2:load funciton will call open(). check permission of the so.

我认为这是android.mk文件的问题。1:尝试切换到 armabi v7。2:load 函数会调用 open()。检查so的许可。

回答by JK_S

As mentioned in the previous answers, No JNI_OnLoad is only a warning.

正如前面的答案中提到的,No JNI_OnLoad 只是一个警告。

I had got similar problem, I figured the problem is because of file operations.

我遇到了类似的问题,我认为问题是因为文件操作。

My app was not having external storage write permission.After adding the below code in manifest it was working fine

我的应用程序没有外部存储写入权限。在清单中添加以下代码后,它工作正常