2
0

decode.gradle 3.5 KB

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