Skip to content

Instantly share code, notes, and snippets.

@khernyo
Created December 6, 2012 18:39
Show Gist options
  • Save khernyo/4226923 to your computer and use it in GitHub Desktop.
Save khernyo/4226923 to your computer and use it in GitHub Desktop.
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
Copy link
Author

khernyo commented Apr 8, 2013

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

@nickcaballero
Copy link

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

@brimanning
Copy link

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
Copy link

can't work on android gradle 0.7.+

@anderkonzen
Copy link

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
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
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
Copy link

@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
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
Copy link

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

@SeanZoR
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
Copy link

@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