String tremorZipFile = "TremorAndroid.zip" String stbiUrl = 'https://raw.githubusercontent.com/nothings/stb/master/stb_image.h' // Working directories for the ndk build. String decodeBuildDir = "${buildDir}" + File.separator + 'decode' String decodeBuildJniDir = decodeBuildDir + File.separator + 'jni' String decodeBuildLibsDir = decodeBuildDir + File.separator + 'libs' // Pre-compiled libs directory String decodePreCompiledLibsDir = 'libs' + File.separator + 'decode' // jME Android Native source files path String decodeSourceDir = 'src/native/jme_decode' task downloadStbImage(type: MyDownload) { sourceUrl = stbiUrl target = file(decodeBuildDir + File.separator + 'stb_image.h') } // Copy stb_image.h to the jni directory. task copyStbiFiles(type: Copy) { def sourceDir = file(decodeBuildDir + File.separator + 'stb_image.h') def outputDir = file(decodeBuildJniDir + File.separator + "STBI") from sourceDir into outputDir } copyStbiFiles.dependsOn { def stbiFile = file(decodeBuildDir + File.separator + 'stb_image.h') if (!stbiFile.exists()) { downloadStbImage } } // Copy libtremor source to the jni directory. task copyTremorFiles(type: Copy) { def zipFile = file(tremorZipFile) def outputDir = file(decodeBuildJniDir + File.separator + "Tremor") from (zipTree(zipFile)) { include '*.c' include '*.h' } into outputDir } // Generate headers via javac -h task generateJavahHeaders(type: Exec) { def files0 = fileTree("src/main/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def files1 = fileTree("src/common/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def files2 = fileTree("../jme3-core/src/main/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def files3 = fileTree("../jme3-core/src/plugins/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def files4 = fileTree("../jme3-core/src/tools/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def files5 = fileTree("../jme3-terrain/src/main/java/").filter { it.isFile() && it.getName().endsWith(".java") }.files def filesList = "\"" + files0.join("\"\n\"") + "\"\n\"" + files1.join("\"\n\"") + "\"\n\"" + files2.join("\"\n\"") + "\"\n\"" + files3.join("\"\n\"") + "\"\n\"" + files4.join("\"\n\"") + "\"\n\"" + files5.join("\"\n\"") + "\"" new File("$projectDir/java_classes.jtxt").text = filesList.replaceAll(java.util.regex.Pattern.quote("\\"), java.util.regex.Matcher.quoteReplacement("/")) executable org.gradle.internal.jvm.Jvm.current().getExecutable('javac') args '-h', decodeSourceDir args "@$projectDir/java_classes.jtxt" } // Copy jME Android native files to jni directory task copySourceToBuild(type: Copy, dependsOn:[copyTremorFiles, copyStbiFiles, generateJavahHeaders]) { def sourceDir = file(decodeSourceDir) def outputDir = file(decodeBuildJniDir) from sourceDir into outputDir } task buildNativeLib(type: Exec, dependsOn: copySourceToBuild) { workingDir decodeBuildDir executable rootProject.ndkCommandPath args "-j" + Runtime.runtime.availableProcessors() } task updatePreCompiledLibs(type: Copy, dependsOn: buildNativeLib) { def sourceDir = new File(decodeBuildLibsDir) def outputDir = new File(decodePreCompiledLibsDir) from sourceDir into outputDir } // Copy pre-compiled libs to build directory (when not building new libs) task copyPreCompiledLibs(type: Copy) { def sourceDir = file(decodePreCompiledLibsDir) def outputDir = file(decodeBuildLibsDir) from sourceDir into outputDir } // ndkExists is a boolean from the build.gradle in the root project // buildNativeProjects is a string set to "true" if (ndkExists && buildNativeProjects == "true") { // build native libs and update stored pre-compiled libs to commit compileJava.dependsOn { updatePreCompiledLibs } } else { // use pre-compiled native libs (not building new ones) compileJava.dependsOn { copyPreCompiledLibs } } jar.into("lib") { from decodeBuildLibsDir } // Helper class to wrap ant dowload task class MyDownload extends DefaultTask { @Input String sourceUrl @OutputFile File target @TaskAction void download() { ant.get(src: sourceUrl, dest: target) } }