简体   繁体   English

如何在另一个Android应用程序中调用Android ndk build .so文件?

[英]How to call a Android ndk build .so file in another Android application?

I have an requirement which i have to build .so file using NDK build in Android and I have to call some of the methods from the .so file in my other Android application. 我有一个要求,我必须使用Android中的NDK构建来构建.so文件,并且必须在其他Android应用程序中调用.so文件中的某些方法。

What I have tried so far. 到目前为止我尝试过的。

I have taken the .so file from the lib folder of my Android project and put in my other application libs folder where I should call that .so file. 我已经从Android项目的lib文件夹中提取了.so文件,并将其放到我应调用该.so文件的其他应用程序li​​bs文件夹中。 I have used the below code: 我使用了以下代码:

Note: I am using ECLIPSE 注意:我正在使用ECLIPSE

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

and also tried with full path of library 并且尝试了完整的库路径

static {
        System.loadLibrary("fullpath\NativeCode");
    }

both the cases its giving unsatisfiedLinkError that mean it's not getting the path of my .so file. 两种情况都给出unsatisfiedLinkError,这表示它没有获取我的.so文件的路径。

You need to have your libNativeCode.so in the jni folder of your root, and not in the libs or lib folder 您需要将libNativeCode.so放在根目录的jni文件夹中,而不是在libslib文件夹中

Here is a nice tutorial : http://www3.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html 这是一个很好的教程: http : //www3.ntu.edu.sg/home/ehchua/programming/android/android_ndk.html

Alternatively, you can use following commands: 另外,您可以使用以下命令:

    adb shell
    $echo $PATH

What it will do is display a path like this : 它会做的是显示如下路径:

/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin / sbin目录:/供应商/ bin中:/系统/ sbin目录:/系统/ bin中:/系统/ XBIN

You can have your .so in any of these folders like /system , /system/bin , /system/sbin . 您可以将.so放在/system/system/bin/system/sbin等任何文件夹中。 (Remember, but you would need to manually copy it, and it is not a good idea to do so for an app) (请记住,但是您需要手动复制它,对于应用程序这样做不是一个好主意)

Edit : You can not call any JNI function in your app, because the signature. 编辑:由于签名,您不能在应用程序中调用任何JNI函数。 You need to have a JNI wrapper (a .so JNI lib that in turn uses the .h header file to call the JNI function of the other library). 您需要有一个JNI包装器(一个.so JNI库,该库又使用.h头文件来调用另一个库的JNI函数)。 This is because JNI is very sensitive to signatures. 这是因为JNI对签名非常敏感。 So, as I recommended earlier, follow the tutorial ! 因此,按照我之前的建议,请按照本教程操作!

In order to get your .so library to be loaded using loadLibrary() you'll have to copy the libNativeCode.so lib into the jniLibs/architecture folder. 为了使您的.so库可以使用loadLibrary()加载,您必须将libNativeCode.so库复制到jniLibs / architecture文件夹中。

The folder should be located under src/main/jniLibs/armeabi-v7a of your Android Application module (assuming you're using Android Studio). 该文件夹应位于Android应用程序模块的src / main / jniLibs / armeabi-v7a下(假设您使用的是Android Studio)。

After you copy the libraries, add the following line to your build.gradle located at the app module folder: 复制库后,将以下行添加到位于app模块文件夹的build.gradle中:

assert file("./src/main/jniLibs/armeabi-v7a/libNativeCode.so").exists()

After you'll complete the above steps it should load the lib properly. 完成上述步骤后,应正确加载lib。

If the libNativeCode.so is located at the wrong path, the Gradle build will fail. 如果libNativeCode.so位于错误的路径,则Gradle构建将失败。

Good luck, 祝好运,

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

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