Skip to content

Instantly share code, notes, and snippets.

@Jween
Last active August 29, 2015 14:06
Show Gist options
  • Save Jween/fd5ce929c25141195f4b to your computer and use it in GitHub Desktop.
Save Jween/fd5ce929c25141195f4b to your computer and use it in GitHub Desktop.
package so files for android module when building with gradle
// requires gradleAfter from [gradle_after](https://gist.github.com/Jween/039f4625b69d598b856d) ahead
//
// for packaging so files in command line
// invoke hint:
// > gradle -p someModule -P [unpackageSo=ture|false]
// -unpackageSo optional, false by default, to package apk with so files inside
// if set to true, the package task will skip packaging so files into apk file
//
if (gradleAfter(1.12) ) {
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
def unpackageSo = 'true' == project.properties.get('unpackageSo')
if (unpackageSo) {
println "So files is not packaged due to project property: 'unpackageSo' is true"
android.sourceSets.main.jniLibs.srcDirs = []
} else {
println "Start setting up environment for packaging so files "
android.sourceSets.main.jniLibs.srcDirs =
android.sourceSets.main.jniLibs.srcDirs << ['libs']
}
}
} else {
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: '**/*.so') into 'build/libs'
}
//@Deprecated Compile class is deprecated in android gradle plugin 0.13.+ / gradle 2.1
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
def unpackageSo = 'true' == project.properties.get('unpackageSo')
if (gradleAfter(1.8)) {
if (unpackageSo) {
println "So files is not packaged due to project property: 'unpackageSo' is true"
android.sourceSets.main.jniLibs.srcDirs = []
} else {
println "Start setting up environment for packaging so files "
android.sourceSets.main.jniLibs.srcDirs =
android.sourceSets.main.jniLibs.srcDirs << ['libs']
}
} else {
if (unpackageSo) {
println "So files is not packaged due to project property: 'unpackageSo' is true"
return;
}
println "Start setting up environment for packaging so files "
def jni;
if (pkgTask.metaClass.hasProperty(pkgTask, 'jniDir')) {
jni = "jniDir"
// pkgTask.jniDir file('build/libs')
pkgTask."${jni}" file('build/libs')
} else if (pkgTask.metaClass.hasProperty(pkgTask, 'jniFolders')) {
jni = 'jniFolders'
// pkgTask.jniFolders = new HashSet<File>()
// pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))
pkgTask."${jni}" = new HashSet<File>()
pkgTask."${jni}".add(new File(projectDir, 'build/libs'))
} else {
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment