如何使用 gradle 0.7+ 将 .so 文件添加到 android 库项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21255125/
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
How can I add .so files to an android library project using gradle 0.7+
提问by Alec Holmes
Project structure:
项目结构:
App project --> depends on Library project
应用项目 --> 依赖于库项目
Library Project has a folder for the compiled jni libs
库项目有一个用于编译 jni 库的文件夹
jniLibs.srcDirs = ['libs']
And I've tried adding the following to the android element of the build.gradle as per the example app https://android.googlesource.com/platform/tools/build/+/2e1f7810edd76d92cee8d3e06bc4dec0c288adea/tests/ndkSanAngeles/build.gradlehowever android library projects do not support productFlavours and as such the assemble fails with "Could not find method productFlavors() for arguments [dghdhd] on project"
我也尝试添加以下到的build.gradle的Android的元素按照示例应用程序https://android.googlesource.com/platform/tools/build/+/2e1f7810edd76d92cee8d3e06bc4dec0c288adea/tests/ndkSanAngeles/build.gradle然而, android 库项目不支持 productFlavors,因此组装失败,“无法在项目上找到用于参数 [dghdhd] 的方法 productFlavors()”
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
}
arm {
ndk {
abiFilters "armeabi-v7a", "armeabi"
}
}
}
Is there a way to add ndk support to an android library project?
有没有办法为 android 库项目添加 ndk 支持?
回答by Alec Holmes
In the end I didnt need to use product flavours.
最后,我不需要使用产品口味。
For the library project I added the following:
对于库项目,我添加了以下内容:
android {
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
The libs folder had a folder inside called "armeabi-v7a" and as this is my only target it worked a treat.
libs 文件夹中有一个名为“armeabi-v7a”的文件夹,因为这是我唯一的目标,所以它很有效。
The ndk files (.so) are propagated into the android project that is using the android library project.
ndk 文件 (.so) 传播到使用 android 库项目的 android 项目中。
回答by Maks
Example with new android experimental gradle plugin.
带有新的android 实验性 gradle 插件的示例。
Requirements:
要求:
- Android Studio 1.5+
- gradle-2.10
- gradle-experimental:0.6.0-alpha5
- 安卓工作室 1.5+
- gradle-2.10
- gradle-experimental:0.6.0-alpha5
1) You could simply put all shared native libraries in the main / jniLibsfolder, by default.
1)默认情况下,您可以简单地将所有共享本机库放在main / jniLibs文件夹中。
Project structure
项目结构
root folder / android project
根文件夹/android项目
root folder / android_project / app / src / main / jniLibs / x86
根文件夹/android_project/app/src/main/jniLibs/x86
root folder / android_project / app / src / main / jniLibs / armeabi-v7a
根文件夹/android_project/app/src/main/jniLibs/armeabi-v7a
root folder / android_project / app / src / main / jniLibs / ...
根文件夹/android_project/app/src/main/jniLibs/...
Gradle will automatically upload them to the device.
Gradle 会自动将它们上传到设备。
Then you could load the library in an application.
然后您可以在应用程序中加载库。
static {
System.loadLibrary("mylibrary");
}
2) You could also put all shared native libraries in the custom location.
2)您还可以将所有共享的本机库放在自定义位置。
Example with a path to the bin / android / Debugdirectory.
带有bin / android / Debug目录路径的示例。
In that case you should manually set the libraries location in the build.gradlefile.
在这种情况下,您应该在build.gradle文件中手动设置库位置。
Project structure
项目结构
root folder / android project
根文件夹/android项目
root folder / bin / android / Debug / jniLibs / x86
根文件夹/bin/android/Debug/jniLibs/x86
root folder / bin / android / Debug / jniLibs / armeabi-v7a
根文件夹/bin/android/Debug/jniLibs/armeabi-v7a
root folder / bin / android / Debug / jniLibs / ...
根文件夹/bin/android/Debug/jniLibs/...
root folder / android_project / app / build.gradle
根文件夹/android_project/app/build.gradle
apply plugin: 'com.android.model.application'
model {
android {
sources {
main {
jni {
source {
srcDirs = []
}
}
jniLibs {
source {
srcDirs "/../../bin/android/Debug/jniLibs"
}
}
}
}
}
}
回答by Jerry Agin
I'm working with Android Studio 2.1, and I found that adding the sources or sourceSets entry to my build.gradle had no apparent effect. Instead I found that I needed the following:
我正在使用 Android Studio 2.1,我发现将 source 或 sourceSets 条目添加到我的 build.gradle 没有明显效果。相反,我发现我需要以下内容:
android {
defaultConfig {
ndk {
moduleName "libmp3lame"
}
}
回答by Scott Barta
According to this thread:
根据这个线程:
https://groups.google.com/forum/#!topic/adt-dev/nQobKd2Gl_8
https://groups.google.com/forum/#!topic/adt-dev/nQobKd2Gl_8
not being able to add .so files to library projects was a bug that was fixed in v0.8 of the plugin.
无法将 .so 文件添加到库项目是插件 v0.8 中修复的错误。