41

Project structure:

App project --> depends on Library project

Library Project has a folder for the compiled jni libs

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.gradle however android library projects do not support productFlavours and as such the assemble fails with "Could not find method productFlavors() for arguments [dghdhd] on project"

productFlavors {
    x86 {
        ndk {
            abiFilter "x86"
        }
    }
    arm {
        ndk {
            abiFilters "armeabi-v7a", "armeabi"
        }
    }
}

Is there a way to add ndk support to an android library project?

Alec Holmes
  • 3,625
  • 4
  • 22
  • 23

4 Answers4

77

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.

Alec Holmes
  • 3,625
  • 4
  • 22
  • 23
  • 1
    While trying to build an older project that only had an "armeabi" folder, the .so files were not found. Copying them into a "armeabi-v7a" folder solved the issue. Not sure why it wouldn't fall back on "armeabi" in that case, but that's Gradle! – pents90 Apr 14 '15 at 20:20
  • 3
    Let me just clarify - if someone is using Android Studio with the experimental Gradle 2.5 there is the following syntax: android.sources { main { jniLibs { source { srcDirs 'libs' } } } } – tochkov Aug 07 '15 at 15:28
  • @pents90 you saved me a day! "armeabi" folder not worked, so I changed it to "armeabi-v7a" it's worked! I really don't know why this happened. – Jishi Chen Jul 13 '16 at 08:50
  • 2
    Is there official doc for the place to put native libraries? – nn0p Aug 27 '16 at 06:29
  • I am able to generate the x86_64 only not armeabi, armeabi-v7a, x86. Can you please help me in this – Amit Thaper Dec 05 '16 at 12:27
  • @nn0p I wonder aswell, been searching for this, but can't find any documentation anywhere. You can find it in the hello-libs android ndk example and in this answer. It could have been mentioned somewhere in the "Add C and C++ Code to Your Project" guide page on android developers.. but it isn't afaik – Emile Vrijdags Jan 30 '17 at 19:28
  • I can't edit my comment anymore, but I found something: http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.api.AndroidSourceSet.html . And if you then learn that groovy functions can be called without parenthesis it becomes more clear, at least to me. – Emile Vrijdags Jan 30 '17 at 19:44
  • where exactly was the libs dir? was it project/libs or project/app/libs or maybe project/app/src/main/libs? – bph Jun 15 '17 at 14:02
  • This only helps if you are already building on a NDK based project. What if you want to pull the dependency? – New Guy Apr 03 '19 at 16:29
13

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"
                    }
                }
            }
        }
    }
}
Maks
  • 2,808
  • 4
  • 30
  • 31
0

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.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
0

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"
        }
    }
Jerry Agin
  • 629
  • 1
  • 5
  • 15