decode.gradle 3.3 KB

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