react-native-navigation的迁移库

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import org.apache.tools.ant.taskdefs.condition.Os
  2. def config = project.hasProperty("react") ? project.react : [];
  3. def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
  4. def entryFile = config.entryFile ?: "index.android.js"
  5. // because elvis operator
  6. def elvisFile(thing) {
  7. return thing ? file(thing) : null;
  8. }
  9. def reactRoot = elvisFile(config.root) ?: file("../../")
  10. def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
  11. void runBefore(String dependentTaskName, Task task) {
  12. Task dependentTask = tasks.findByPath(dependentTaskName);
  13. if (dependentTask != null) {
  14. dependentTask.dependsOn task
  15. }
  16. }
  17. gradle.projectsEvaluated {
  18. // Grab all build types and product flavors
  19. def buildTypes = android.buildTypes.collect { type -> type.name }
  20. def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
  21. // When no product flavors defined, use empty
  22. if (!productFlavors) productFlavors.add('')
  23. productFlavors.each { productFlavorName ->
  24. buildTypes.each { buildTypeName ->
  25. // Create variant and target names
  26. def targetName = "${productFlavorName.capitalize()}${buildTypeName.capitalize()}"
  27. def targetPath = productFlavorName ?
  28. "${productFlavorName}/${buildTypeName}" :
  29. "${buildTypeName}"
  30. // React js bundle directories
  31. def jsBundleDirConfigName = "jsBundleDir${targetName}"
  32. def jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
  33. file("$buildDir/intermediates/assets/${targetPath}")
  34. def resourcesDirConfigName = "jsBundleDir${targetName}"
  35. def resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
  36. file("$buildDir/intermediates/res/merged/${targetPath}")
  37. def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
  38. // Bundle task name for variant
  39. def bundleJsAndAssetsTaskName = "bundle${targetName}JsAndAssets"
  40. def currentBundleTask = tasks.create(
  41. name: bundleJsAndAssetsTaskName,
  42. type: Exec) {
  43. group = "react"
  44. description = "bundle JS and assets for ${targetName}."
  45. // Create dirs if they are not there (e.g. the "clean" task just ran)
  46. doFirst {
  47. jsBundleDir.mkdirs()
  48. resourcesDir.mkdirs()
  49. }
  50. // Set up inputs and outputs so gradle can cache the result
  51. inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
  52. outputs.dir jsBundleDir
  53. outputs.dir resourcesDir
  54. // Set up the call to the react-native cli
  55. workingDir reactRoot
  56. // Set up dev mode
  57. def devEnabled = !targetName.toLowerCase().contains("release")
  58. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  59. commandLine "cmd", "/c", "react-native", "bundle", "--platform", "android", "--dev", "${devEnabled}",
  60. "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
  61. } else {
  62. commandLine "react-native", "bundle", "--platform", "android", "--dev", "${devEnabled}",
  63. "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
  64. }
  65. enabled config."bundleIn${targetName}" ||
  66. config."bundleIn${buildTypeName.capitalize()}" ?:
  67. targetName.toLowerCase().contains("release")
  68. }
  69. // Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process
  70. currentBundleTask.dependsOn("merge${targetName}Resources")
  71. currentBundleTask.dependsOn("merge${targetName}Assets")
  72. runBefore("processArmeabi-v7a${targetName}Resources", currentBundleTask)
  73. runBefore("processX86${targetName}Resources", currentBundleTask)
  74. runBefore("processUniversal${targetName}Resources", currentBundleTask)
  75. runBefore("process${targetName}Resources", currentBundleTask)
  76. }
  77. }
  78. }