Skip to content

Instantly share code, notes, and snippets.

@ph0b

ph0b/build.gradle

Last active Feb 22, 2016
Embed
What would you like to do?
build gradle with ndk build tasks that are using an alternative configuration or Application.mk for debug builds, using the new model.
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = rootProject.ext.compileSdkVersion
buildToolsVersion = rootProject.ext.buildToolsVersion
defaultConfig.with {
minSdkVersion.apiLevel = 14
targetSdkVersion.apiLevel = 23
}
}
android.ndk { // keeping it to make symbols look up happy.
moduleName = "mymodule"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("src/main/jni/prebuilts/include")}".toString()
stl = 'gnustl_shared'
platformVersion = 15
}
android.sources {
main.jni {
source {
srcDirs = ['src/main/none']
}
}
main.jniLibs {
source {
srcDirs = ['src/main/libs']
}
}
}
}
// call regular ndk-build(.cmd) script from app directory, in either debug or release mode
tasks.withType(JavaCompile) {
compileTask ->
def tmpFile = file('src/main/jni/.debugbuild.tmp') //used to persist the last build type, to avoid triggering rebuilds all the time.
def ndkBuildExt = Os.isFamily(Os.FAMILY_WINDOWS) ? ".cmd" : ""
def debugMode = compileTask.name.contains("Debug")
def taskName = "ndk${debugMode ? "Debug" : "Release"}Build"
def extraParameter = debugMode ? "NDK_DEBUG=1" : "" // other possibility for debug build:
// referencing an other Application.mk that contains APP_OPTIM:=debug and other options
// -> "NDK_APPLICATION_MK=" + file('src/main/jni/Application-debug.mk').absolutePath
def task = tasks.findByPath(taskName);
if (task == null) {
task = tasks.create(name: taskName) { //create ndkReleaseBuild and ndkDebugBuild tasks
doLast {
exec { //execute all this block when the task is executed. (A Build task gets only the commandLine to be executed, so here we're using doLast.exec instead)
def rebuildFlag = ""
if (!tmpFile.exists()) tmpFile.createNewFile()
if (tmpFile.text != String.valueOf(debugMode)) {
rebuildFlag = "-B" //ndk build type has changed, triggering full rebuild.
}
commandLine "ndk-build${ndkBuildExt}", rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
tmpFile.write(String.valueOf(debugMode))
}
}
}
}
compileTask.dependsOn task
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-annotations:23.0.1'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment