如何在 Android Studio 中包含 *.so 库?

问题描述:

我阅读了很多关于如何将 *.so 库添加到 Android Studio 的帖子,但它们都不起作用,尤其是在文本方面:这不适用于较新的 xxx(Android Studio、gradle、...)

I read many threads how to add a *.so library to Android Studio, but none of them works, especially when it comes to the point of text: This does not work with the newer xxx (Android Studio, gradle, ...)

我们可以重新开始吗?我得到:

Can we make a fresh start please. I got:

Android Studio 0.6.0

Android Studio 0.6.0

从项目结构我看到:

SDK 位置:

/usr/share/android-studio/data/sdk
/usr/lib/jvm/default-java

项目:

Gradle version 1.10
Android Plugin Version 0.11.+

模块/应用程序:属性:

Modules/app: Properties:

编译 SDK 版本 19构建工具版本 19.1.0

Compile Sdk Version 19 Build Tools Version 19.1.0

依赖:

{dir=libs, include=[*.jar]} Compile

{dir=libs, include=[*.so]}  Provided

m com.android.support: appcompat -v7:19.+   Compile

我预编译了 *.so 文件,并且在演示应用程序中它们正在运行.我必须更改应用程序的源代码,因此我需要使用相同的 *.so 文件重新构建.

I got the *.so files pre-compiled and at the demo app they are working. I have to change the source code of the app, so I need to rebuild with the same *.so files.

当前解决方案

创建文件夹 project/app/src/main/jniLibs,然后将您的 *.so 文件放在该位置的 abi 文件夹中.例如,

Create the folder project/app/src/main/jniLibs, and then put your *.so files within their abi folders in that location. E.g.,

project/
├──libs/
|  └── *.jar       <-- if your library has jar files, they go here
├──src/
   └── main/
       ├── AndroidManifest.xml
       ├── java/
       └── jniLibs/ 
           ├── arm64-v8a/                       <-- ARM 64bit
           │   └── yourlib.so
           ├── armeabi-v7a/                     <-- ARM 32bit
           │   └── yourlib.so
           └── x86/                             <-- Intel 32bit
               └── yourlib.so

已弃用的解决方案

在您的模块 gradle.build 文件中添加两个代码片段作为依赖项:

Add both code snippets in your module gradle.build file as a dependency:

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

如何创建这个自定义 jar:

How to create this custom jar:

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

同样的答案也可以在相关问题中找到:在android studio的apk中包含.so库

Same answer can also be found in related question: Include .so library in apk in android studio