How can I add .so files to an android library project using gradle 0.7+

别等时光非礼了梦想. 提交于 2019-11-27 07:05:16

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.

The ndk files (.so) are propagated into the android project that is using the android library project.

Example with new android experimental gradle plugin.


Requirements:

  • Android Studio 1.5+
  • gradle-2.10
  • gradle-experimental:0.6.0-alpha5

1) You could simply put all shared native libraries in the main / jniLibs folder, by default.

Project structure

root folder / android project

root folder / android_project / app / src / main / jniLibs / x86

root folder / android_project / app / src / main / jniLibs / armeabi-v7a

root folder / android_project / app / src / main / jniLibs / ...

Gradle will automatically upload them to the device.

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.

Example with a path to the bin / android / Debug directory.

In that case you should manually set the libraries location in the build.gradle file.

Project structure

root folder / android project

root folder / bin / android / Debug / jniLibs / x86

root folder / bin / android / Debug / jniLibs / armeabi-v7a

root folder / bin / android / Debug / jniLibs / ...

root folder / 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"
                    }
                }
            }
        }
    }
}

According to this thread:

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.

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 {
    defaultConfig {
        ndk {
            moduleName "libmp3lame"
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!