Skip to content

Instantly share code, notes, and snippets.

@ming4883
Created May 13, 2015 12:00
Show Gist options
  • Save ming4883/5ac51378d0353447c664 to your computer and use it in GitHub Desktop.
Save ming4883/5ac51378d0353447c664 to your computer and use it in GitHub Desktop.
Manual invoke ndk-build for each BuildType in Android Studio
def readLocalProp () {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
return properties
}
def invokeNDKBuild (debuggable, cleaning) {
def properties = readLocalProp()
def ndkDir = properties.getProperty('ndk.dir')
def osName = System.getProperty('os.name').toLowerCase()
def args = new ArrayList<List>()
if (osName.contains ('windows')) {
args.add('cmd')
args.add('/c')
args.add(ndkDir + '/ndk-build.cmd')
}
else {
args.add(ndkDir + '/ndk-build')
}
args.add (debuggable ? 'NDK_DEBUG=1' : 'NDK_DEBUG=0')
if (cleaning)
args.add('clean')
def jniPath = file(file('jni/').absolutePath + '/')
def pb = new ProcessBuilder(args);
pb.redirectErrorStream(true)
pb.directory(jniPath)
def process = pb.start()
def stdout = process.getInputStream()
def reader = new BufferedReader(new InputStreamReader(stdout))
def line
while ((line = reader.readLine()) != null) {
println line
}
if (0 != process.exitValue())
return false
return true
}
android.buildTypes.each { theBuildType ->
def buildName = theBuildType.name.capitalize()
task ("compile${buildName}Native") {
doLast {
if (!invokeNDKBuild (theBuildType.debuggable, false))
throw new StopExecutionException()
}
}
task ("clean${buildName}Native") {
doLast {
if (!invokeNDKBuild (theBuildType.debuggable, true))
throw new StopExecutionException()
}
}
clean.dependsOn ("clean${buildName}Native")
tasks.whenTaskAdded{ theTask ->
if(theTask.name == "compile${buildName}Ndk") {
theTask.dependsOn("compile${buildName}Native")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment