Instantly share code, notes, and snippets.

What would you like to do?
Gradle Android configuration with .so hack
// This hack works with, won't work in later version without modification
apply plugin: 'android'
targetCompatibility = 1.6
sourceCompatibility = 1.6
android {
target = 'android-14'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDir 'src'
res.srcDir 'res'
assets.srcDir 'assets'
resources.srcDir 'src'
test {
java.srcDir 'tests/src'
dependencies {
compile project(':main')
compile fileTree(dir: '../main/libs', include: '*.jar')
compile fileTree(dir: '../thirdparty/actionbar-sherlock/library/libs', include: '*.jar')
task copyNativeLibs(type: Copy) {
from(new File(project(':main').buildDir, 'native-libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType( { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')

This comment has been minimized.


khernyo commented Apr 8, 2013

for gradle android plugin v0.3 use ""


This comment has been minimized.

nickcaballero commented Sep 20, 2013

Compile task has been deprecated in Gradle. Use JavaCompile instead.


This comment has been minimized.

brimanning commented Dec 18, 2013

For anyone seeing this now - make sure you move all of your generated libraries to native-libs and that you check the paths/project structure you're using.

In my case, the copyNativeLibs block needed to be changed to:

task copyNativeLibs(type: Copy) {
    from(new File(getProjectDir(), 'src/main/native-libs')) { include '**/*.so' }
    into new File(buildDir, 'native-libs')

Hopefully that saves someone else some time in the future.


This comment has been minimized.

googolmo commented Dec 19, 2013

can't work on android gradle 0.7.+


This comment has been minimized.

anderkonzen commented Dec 24, 2013

On Android gradle 0.7.+ (Android Studio 0.4) you can use this instead:

tasks.withType( { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))


This comment has been minimized.

Leandros commented Jan 5, 2014

As of Android Gradle 0.7.2 you can put your native libraries in src/main/jniLibs and they'll be packaged correctly.



This comment has been minimized.

jjhesk commented Jan 22, 2014


That is just a small piece of information. Are there any sample code in gradle and some setup code to be able to compile the so files or we will not need to write custom the compile code in gradle anymore??

okay.. I have finally found that sample code on the bottom of the page.. The jniLibs are demonstrated in this file..


This comment has been minimized.

thomasgravina commented Apr 4, 2014

@Leandros, indeed that works, but any ideas why Gradle includes them twice in the APK?
It makes the final package twice heavier. Didn't figure out how to avoid that.


This comment has been minimized.

dhesson commented Jun 27, 2014

@anderkonzen I'm trying to build an AAR. Does the PackageApplication task only kick in if your building an APK?

Currently I build the ndk libs and copy them over to src/main/jniLibs prior to AAR packaging, but I would much rather copy the jni libs into the build dir and have them packaged up from there


This comment has been minimized.

Avinash-Bhat commented Aug 1, 2014

@jjhesk, also take a look at murphy's sample:


This comment has been minimized.

SeanZoR commented Dec 9, 2014

There's a fix in the new Android Studio 1.0.0 / Gradle 1.0.0:


This comment has been minimized.

trinadhkoya commented Mar 18, 2016

@brimanning it works great ||Thanks a ton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment