Skip to content

Instantly share code, notes, and snippets.

@Vandalko
Created December 17, 2013 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vandalko/8004598 to your computer and use it in GitHub Desktop.
Save Vandalko/8004598 to your computer and use it in GitHub Desktop.
Android Studio (Gradle) NDK support
//////////////
// NDK Support
//////////////
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK != null) {
def ndkBuild = null
if (System.properties['os.name'].toLowerCase().contains('windows')) {
ndkBuild = new File(System.env.ANDROID_NDK, 'ndk-build.cmd')
} else {
ndkBuild = new File(System.env.ANDROID_NDK, 'ndk-build')
}
commandLine ndkBuild, "--directory", file("src/main/jni").absolutePath, "-B", "NDK_DEBUG=0", "V=1"
doLast {
file('src/main/obj').deleteDir()
}
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}
task copyNativeLibs(type: Copy, dependsOn: 'buildNative') {
doFirst {
new File(buildDir, 'native-libs').mkdirs()
}
from(file('src/main/libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
doLast {
file('src/main/libs').deleteDir()
}
}
task cleanNativeLibs(type: Delete) {
delete 'src/main/obj', 'src/main/libs', 'libs/armeabi'
}
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniDir new File(buildDir, 'native-libs')
}
gradle.projectsEvaluated {
preBuild.dependsOn 'copyNativeLibs'
clean.dependsOn 'cleanNativeLibs'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment