-
-
Save khernyo/4226923 to your computer and use it in GitHub Desktop.
// 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') | |
} |
Compile task has been deprecated in Gradle. Use JavaCompile instead.
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.
can't work on android gradle 0.7.+
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
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
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
@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.
@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
@jjhesk, also take a look at murphy's sample: https://github.com/commonsguy/sqlcipher-gradle
There's a fix in the new Android Studio 1.0.0 / Gradle 1.0.0:
https://gist.github.com/SeanZoR/cd2ecd22be3fdef7aa09/294bf0dd17a3fe58f23e37bbbe6b4d8d2e6efc37
@brimanning it works great ||Thanks a ton
for gradle android plugin v0.3 use "com.android.build.gradle.tasks.PackageApplication"