react.gradle 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. import org.apache.tools.ant.taskdefs.condition.Os
  5. def config = project.hasProperty("react") ? project.react : [];
  6. def cliPath = config.cliPath ?: "node_modules/react-native/cli.js"
  7. def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
  8. def entryFile = config.entryFile ?: "index.android.js"
  9. def bundleCommand = config.bundleCommand ?: "bundle"
  10. def reactRoot = file(config.root ?: "../../")
  11. def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
  12. def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;
  13. afterEvaluate {
  14. def isAndroidLibrary = plugins.hasPlugin("com.android.library")
  15. def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants
  16. variants.all { def variant ->
  17. // Create variant and target names
  18. def targetName = variant.name.capitalize()
  19. def targetPath = variant.dirName
  20. // React js bundle directories
  21. def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
  22. def resourcesDir = file("$buildDir/generated/res/react/${targetPath}")
  23. def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
  24. // Additional node and packager commandline arguments
  25. def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
  26. def extraPackagerArgs = config.extraPackagerArgs ?: []
  27. def currentBundleTask = tasks.create(
  28. name: "bundle${targetName}JsAndAssets",
  29. type: Exec) {
  30. group = "react"
  31. description = "bundle JS and assets for ${targetName}."
  32. // Create dirs if they are not there (e.g. the "clean" task just ran)
  33. doFirst {
  34. jsBundleDir.deleteDir()
  35. jsBundleDir.mkdirs()
  36. resourcesDir.deleteDir()
  37. resourcesDir.mkdirs()
  38. }
  39. // Set up inputs and outputs so gradle can cache the result
  40. inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
  41. outputs.dir(jsBundleDir)
  42. outputs.dir(resourcesDir)
  43. // Set up the call to the react-native cli
  44. workingDir(reactRoot)
  45. // Set up dev mode
  46. def devEnabled = !(config."devDisabledIn${targetName}"
  47. || targetName.toLowerCase().contains("release"))
  48. def extraArgs = extraPackagerArgs;
  49. if (bundleConfig) {
  50. extraArgs = extraArgs.clone()
  51. extraArgs.add("--config");
  52. extraArgs.add(bundleConfig);
  53. }
  54. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  55. commandLine("cmd", "/c", *nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
  56. "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
  57. } else {
  58. commandLine(*nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
  59. "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
  60. }
  61. enabled config."bundleIn${targetName}" ||
  62. config."bundleIn${variant.buildType.name.capitalize()}" ?:
  63. targetName.toLowerCase().contains("release")
  64. }
  65. // Expose a minimal interface on the application variant and the task itself:
  66. variant.ext.bundleJsAndAssets = currentBundleTask
  67. currentBundleTask.ext.generatedResFolders = files(resourcesDir).builtBy(currentBundleTask)
  68. currentBundleTask.ext.generatedAssetsFolders = files(jsBundleDir).builtBy(currentBundleTask)
  69. // registerGeneratedResFolders for Android plugin 3.x
  70. if (variant.respondsTo("registerGeneratedResFolders")) {
  71. variant.registerGeneratedResFolders(currentBundleTask.generatedResFolders)
  72. } else {
  73. variant.registerResGeneratingTask(currentBundleTask)
  74. }
  75. variant.mergeResourcesProvider.get().dependsOn(currentBundleTask)
  76. // packageApplication for Android plugin 3.x
  77. def packageTask = variant.hasProperty("packageApplication")
  78. ? variant.packageApplicationProvider.get()
  79. : tasks.findByName("package${targetName}")
  80. if (variant.hasProperty("packageLibrary")) {
  81. packageTask = variant.packageLibrary
  82. }
  83. // pre bundle build task for Android plugin 3.2+
  84. def buildPreBundleTask = tasks.findByName("build${targetName}PreBundle")
  85. def resourcesDirConfigValue = config."resourcesDir${targetName}"
  86. if (resourcesDirConfigValue) {
  87. def currentCopyResTask = tasks.create(
  88. name: "copy${targetName}BundledResources",
  89. type: Copy) {
  90. group = "react"
  91. description = "copy bundled resources into custom location for ${targetName}."
  92. from(resourcesDir)
  93. into(file(resourcesDirConfigValue))
  94. dependsOn(currentBundleTask)
  95. enabled(currentBundleTask.enabled)
  96. }
  97. packageTask.dependsOn(currentCopyResTask)
  98. if (buildPreBundleTask != null) {
  99. buildPreBundleTask.dependsOn(currentCopyResTask)
  100. }
  101. }
  102. def currentAssetsCopyTask = tasks.create(
  103. name: "copy${targetName}BundledJs",
  104. type: Copy) {
  105. group = "react"
  106. description = "copy bundled JS into ${targetName}."
  107. if (config."jsBundleDir${targetName}") {
  108. from(jsBundleDir)
  109. into(file(config."jsBundleDir${targetName}"))
  110. } else {
  111. into ("$buildDir/intermediates")
  112. into ("assets/${targetPath}") {
  113. from(jsBundleDir)
  114. }
  115. // Workaround for Android Gradle Plugin 3.2+ new asset directory
  116. into ("merged_assets/${variant.name}/merge${targetName}Assets/out") {
  117. from(jsBundleDir)
  118. }
  119. // Workaround for Android Gradle Plugin 3.4+ new asset directory
  120. into ("merged_assets/${variant.name}/out") {
  121. from(jsBundleDir)
  122. }
  123. }
  124. // mergeAssets must run first, as it clears the intermediates directory
  125. dependsOn(variant.mergeAssetsProvider.get())
  126. enabled(currentBundleTask.enabled)
  127. }
  128. packageTask.dependsOn(currentAssetsCopyTask)
  129. if (buildPreBundleTask != null) {
  130. buildPreBundleTask.dependsOn(currentAssetsCopyTask)
  131. }
  132. }
  133. }