简体   繁体   English

无法使用NDK从Android Studio中的.so文件调用本机方法(库加载成功)

[英]Unable to call Native method from .so file (Loading of library is success ) in Android studio With NDK

  1. I had created the sample project(HellWorldJNI) that runs with Android Ndk and prints "Hello world from Jni" message from the native C. 我创建了一个示例项目(HellWorldJNI),该项目与Android Ndk一起运行,并从本地C打印“ Jni的Hello world”消息。

  2. And then i extract the HelloWorldJNI.apk file of the sample from the Output/apk directory of the project. 然后我从项目的Output / apk目录中提取示例的HelloWorldJNI.apk文件。

  3. So i got the all the .so files from different devices(like arm64-v8a,armeabi,x86..etc) 所以我从不同的设备(例如arm64-v8a,armeabi,x86..etc)获得了所有.so文件。

  4. Now i created a new project (CallJNIFromSoFile) and created jniLibs folder inside main folder , and copied the all .so files of HelloWorldJNI app into this new project. 现在,我创建了一个新项目(CallJNIFromSoFile),并在主文件夹中创建了jniLibs文件夹,并将HelloWorldJNI应用程序的所有.so文件复制到了该新项目中。

  5. After that i load the library and the function name of the HelloWorldJNI App like this 之后,我像这样加载库和HelloWorldJNI App的函数名称

     static { System.loadLibrary("hello-android-jni"); } public native static String getMsgFromJni(); 
  6. Here i got strucked, i am able load the "hello-android-jni" library without any problem. 在这里,我很震惊,我可以毫无问题地加载“ hello-android-jni”库。 But the native method getMsgFromJni() from the above is showing in RED color and not able call that method. 但是上面的本机方法getMsgFromJni()显示为红色,无法调用该方法。

    Not at all receiving the message "Hello world from Jni" 根本没有收到消息“来自Jni的Hello world”

    This is my Logcat Output: 这是我的Logcat输出:

     java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String at com.example.hari.myopencvvsample.MainActivity.getMsgFromJni(Native Method) 

Please suggest am i missing anything other than the above steps. 请建议我是否缺少上述步骤之外的任何内容。 I tried many ways like regenerating the .so files and then put it again,but didn't worked . 我尝试了多种方法,例如重新生成.so文件,然后再次放入它,但没有成功。 Sorry for my english. 对不起我的英语不好。

Thanks in advance. 提前致谢。

You need to provide a JNI implementation of the method getMsgFromJni() . 您需要提供方法getMsgFromJni()的JNI实现。 The .so files are shared libraries, which you can link to and use from C or C++ code, but you need to provide an interface for any C or C++ methods you plan to call from Java, hence J ava N ative I nterface. 的.so文件是共享库,其可以链接到和从C或C ++代码中使用,但需要为计划从Java调用任何C或C ++方法提供一个接口,因此的J avaÑative 覆盖整个院落。

If you look at the src/main/jni folder in your HelloWorldJNI project, you will see a C or C++ file which implements any native methods called from Java in that project. 如果查看HelloWorldJNI项目中的src / main / jni文件夹,将会看到一个C或C ++文件,该文件实现了该项目中从Java调用的任何本机方法。 You can also refer to the NDK samples for more help. 您也可以参考NDK示例以获得更多帮助。 The hello-libs example is a good place to start for your question. hello-libs示例是开始您的问题的好地方。

This is completely bad idea to copy *so files between Android projects. 在Android项目之间复制* so文件完全是个主意。 JNI will find your function by the name which depends from the name of your Android application java-class hierarchy. JNI将通过名称找到您的函数,该名称取决于您的Android应用程序Java类层次结构的名称。 Accordingly your description you compiled HelloWorldJni apk first and you have symbols there something like: 因此,您的描述是您首先编译了HelloWorldJni apk的,并且那里有一些符号,例如:

T Java_com_example_helloworldjni_MainActivity_getMsgFromJni

As you see there is your java-class hierarchy included in name of this function. 如您所见,此函数的名称中包含Java类层次结构。 And then, you are creating your own application with different class-names and your program looks for something like: 然后,您将使用不同的类名创建自己的应用程序并且程序将查找类似以下内容的内容:

Java_com_example_hari_myopencvvsample_MainActivity_getMsgFromJni

inside your library, but as you know - it is not there. 在您的资料库中,但是如您所知-它不存在。 I'd suggest you to read the article https://developer.android.com/ndk/samples/sample_hellojni.html with its part about c implementation carefully, and repeat all the steps for your new CallJNIFromSoFile project. 我建议您仔细阅读文章https://developer.android.com/ndk/samples/sample_hellojni.html及其有关c实现的部分,并对新的CallJNIFromSoFile项目重复所有步骤。

Use targetSdkVersion less than 23 it will be working fine.. 使用targetSdkVersion小于23它将正常工作。

         defaultConfig {
             versionCode 1
             versionName "1.0.0"

             minSdkVersion 15
             targetSdkVersion 22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM