decode.gradle 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. String tremorZipFile = "TremorAndroid.zip"
  2. String stbiUrl = 'https://raw.githubusercontent.com/nothings/stb/master/stb_image.h'
  3. // Working directories for the ndk build.
  4. String decodeBuildDir = "${buildDir}" + File.separator + 'decode'
  5. String decodeBuildJniDir = decodeBuildDir + File.separator + 'jni'
  6. String decodeBuildLibsDir = decodeBuildDir + File.separator + 'libs'
  7. // Pre-compiled libs directory
  8. String decodePreCompiledLibsDir = 'libs' + File.separator + 'decode'
  9. // jME Android Native source files path
  10. String decodeSourceDir = 'src/native/jme_decode'
  11. task downloadStbImage(type: MyDownload) {
  12. sourceUrl = stbiUrl
  13. target = file(decodeBuildDir + File.separator + 'stb_image.h')
  14. }
  15. // Copy stb_image.h to the jni directory.
  16. task copyStbiFiles(type: Copy) {
  17. def sourceDir = file(decodeBuildDir + File.separator + 'stb_image.h')
  18. def outputDir = file(decodeBuildJniDir + File.separator + "STBI")
  19. from sourceDir
  20. into outputDir
  21. }
  22. copyStbiFiles.dependsOn {
  23. def stbiFile = file(decodeBuildDir + File.separator + 'stb_image.h')
  24. if (!stbiFile.exists()) {
  25. downloadStbImage
  26. }
  27. }
  28. // Copy libtremor source to the jni directory.
  29. task copyTremorFiles(type: Copy) {
  30. def zipFile = file(tremorZipFile)
  31. def outputDir = file(decodeBuildJniDir + File.separator + "Tremor")
  32. from (zipTree(zipFile)) {
  33. include '*.c'
  34. include '*.h'
  35. }
  36. into outputDir
  37. }
  38. // Generate headers via javah
  39. task generateJavahHeaders(type: Exec) {
  40. executable org.gradle.internal.jvm.Jvm.current().getExecutable('javah')
  41. args '-d', decodeSourceDir
  42. args '-classpath', project.projectClassPath
  43. args "com.jme3.audio.plugins.NativeVorbisFile"
  44. args "com.jme3.texture.plugins.AndroidNativeImageLoader"
  45. }
  46. // Copy jME Android native files to jni directory
  47. task copySourceToBuild(type: Copy, dependsOn:[copyTremorFiles, copyStbiFiles, generateJavahHeaders]) {
  48. def sourceDir = file(decodeSourceDir)
  49. def outputDir = file(decodeBuildJniDir)
  50. from sourceDir
  51. into outputDir
  52. }
  53. task buildNativeLib(type: Exec, dependsOn: copySourceToBuild) {
  54. workingDir decodeBuildDir
  55. executable rootProject.ndkCommandPath
  56. args "-j" + Runtime.runtime.availableProcessors()
  57. }
  58. task updatePreCompiledLibs(type: Copy, dependsOn: buildNativeLib) {
  59. def sourceDir = new File(decodeBuildLibsDir)
  60. def outputDir = new File(decodePreCompiledLibsDir)
  61. from sourceDir
  62. into outputDir
  63. }
  64. // Copy pre-compiled libs to build directory (when not building new libs)
  65. task copyPreCompiledLibs(type: Copy) {
  66. def sourceDir = file(decodePreCompiledLibsDir)
  67. def outputDir = file(decodeBuildLibsDir)
  68. from sourceDir
  69. into outputDir
  70. }
  71. // ndkExists is a boolean from the build.gradle in the root project
  72. // buildNativeProjects is a string set to "true"
  73. if (ndkExists && buildNativeProjects == "true") {
  74. // build native libs and update stored pre-compiled libs to commit
  75. compileJava.dependsOn { updatePreCompiledLibs }
  76. } else {
  77. // use pre-compiled native libs (not building new ones)
  78. compileJava.dependsOn { copyPreCompiledLibs }
  79. }
  80. jar.into("lib") { from decodeBuildLibsDir }
  81. // Helper class to wrap ant dowload task
  82. class MyDownload extends DefaultTask {
  83. @Input
  84. String sourceUrl
  85. @OutputFile
  86. File target
  87. @TaskAction
  88. void download() {
  89. ant.get(src: sourceUrl, dest: target)
  90. }
  91. }