Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gradle Android configuration with .so hack
// This hack works with com.android.tools.build:gradle:0.2, 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(com.android.build.gradle.PackageApplicationTask) { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')
}
@khernyo

This comment has been minimized.

Copy link
Owner Author

khernyo commented Apr 8, 2013

for gradle android plugin v0.3 use "com.android.build.gradle.tasks.PackageApplication"

@nickcaballero

This comment has been minimized.

Copy link

nickcaballero commented Sep 20, 2013

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

@brimanning

This comment has been minimized.

Copy link

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.

@googolmo

This comment has been minimized.

Copy link

googolmo commented Dec 19, 2013

can't work on android gradle 0.7.+

@anderkonzen

This comment has been minimized.

Copy link

anderkonzen commented Dec 24, 2013

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

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}

https://plus.google.com/+AndroidDevelopers/posts/BN8mx2pGk9h

@Leandros

This comment has been minimized.

Copy link

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.

Source: https://groups.google.com/forum/#!msg/adt-dev/nQobKd2Gl_8/ctDp9viWaxoJ

@jjhesk

This comment has been minimized.

Copy link

jjhesk commented Jan 22, 2014

@Leandros

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..
http://tools.android.com/tech-docs/new-build-system/gradle-samples-0.7.3.zip?attredirects=0&d=1

@thomasgravina

This comment has been minimized.

Copy link

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.

@dhesson

This comment has been minimized.

Copy link

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

@Avinash-Bhat

This comment has been minimized.

Copy link

Avinash-Bhat commented Aug 1, 2014

@jjhesk, also take a look at murphy's sample: https://github.com/commonsguy/sqlcipher-gradle

@SeanZoR

This comment has been minimized.

Copy link

SeanZoR commented Dec 9, 2014

There's a fix in the new Android Studio 1.0.0 / Gradle 1.0.0:
https://gist.github.com/SeanZoR/cd2ecd22be3fdef7aa09/294bf0dd17a3fe58f23e37bbbe6b4d8d2e6efc37

@trinadhkoya

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.