简体   繁体   English

如何在Android Studio 1.3.2中包括预构建的.so库

[英]How to include prebuilt .so libs in Android Studio 1.3.2

I am having difficulties integrating a native library I have already built using ndk-build into my Android Studio project. 我在将已经使用ndk-build的本机库集成到Android Studio项目时遇到困难。 The problem is that I am stuck in a transitional period where the Gradle syntax is brand new, but documentation and online help still reference the old syntax. 问题是我陷入了Gradle语法是全新的过渡时期,但是文档和在线帮助仍引用旧的语法。

My library is in this location on my project: app/libs/(armeabi and others)/libsample.so , for a total of 7 variants of the same library for different architectures. 我的库位于项目的以下位置: app/libs/(armeabi and others)/libsample.so ,用于同一库的不同体系结构的总共7个变体。 I also have the header files for this library in app/src/main/jni/headers , and the C wrapper file which calls functions defined in this library on app/src/main/jni/wrapper.c . 我还在app/src/main/jni/headers有此库的头文件,还有在app/src/main/jni/wrapper.c调用此库中定义的函数的C包装器文件。 The wrapper file has a #include "headers/sample.h" . 包装器文件具有#include "headers/sample.h"

The main issue is that I don't know how to set up my Gradle build to use the prebuilt shared library. 主要问题是我不知道如何设置Gradle构建以使用预构建的共享库。 I have a working Android.mk file which can successfully compile my wrapper to use the shared library, but I can't use it in Android Studio, since Gradle generates it's own makefile and uses that instead. 我有一个有效的Android.mk文件,该文件可以成功编译我的包装器以使用共享库,但是我不能在Android Studio中使用它,因为Gradle会生成它自己的makefile并使用它。 Anyone has any idea how to proceed from here? 有人知道如何从这里继续吗?

You should be able to simply drop the pre-built .so files into (project module)/src/main/jniLibs/(target abi), and they'll be included in the final apk that Android Studio generates. 您应该能够简单地将预构建的.so文件拖放到(项目模块)/ src / main / jniLibs /(目标abi)中,并且它们将包含在Android Studio生成的最终apk中。

One caveat : I'm not 100% certain, but it looks like Android's installer can choose to throw out some libraries from abi folders that it decides are a poorer fit for the current device than other abi folders in the apk (ex. armeabi and armeabi-v7a may both be compatible, but armeabi-v7a may be a better fit) if said libraries are only present in the poorer-fit folder. 一个警告 :我不是100%肯定的,但是看起来Android的安装程序可以选择从abi文件夹中抛出一些库,它确定它比当前apk中其他abi文件夹(例如armeabi和armeabi-v7a可能都兼容,但是armeabi-v7a可能更适合)(如果仅在较差适应性文件夹中存在上述库)。 This becomes important if you don't have the source code for the native library. 如果您没有本机库的源代码,这将变得很重要。 You can work around this either by specifying a single abi in your module's build.gradle (see the answer to the linked question) that matches the abi your native library was compiled for or by disabling Android Studio's ndk-build process by setting jni.srcDirs = [] in android.sourceSets.main 您可以通过在模块的build.gradle中指定一个与您的本机库编译的abi匹配的单个abi来解决此问题,或者通过设置jni.srcDirs来禁用Android Studio的ndk生成过程= []在android.sourceSets.main中

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

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