react.gradle 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. import org.apache.tools.ant.taskdefs.condition.Os
  8. def config = project.hasProperty("react") ? project.react : [:];
  9. def detectEntryFile(config) {
  10. if (System.getenv('ENTRY_FILE')) {
  11. return System.getenv('ENTRY_FILE')
  12. } else if (config.entryFile) {
  13. return config.entryFile
  14. } else if ((new File("${projectDir}/../../index.android.js")).exists()) {
  15. return "index.android.js"
  16. }
  17. return "index.js";
  18. }
  19. /**
  20. * Detects CLI location in a similar fashion to the React Native CLI
  21. */
  22. def detectCliPath(config) {
  23. if (config.cliPath) {
  24. return config.cliPath
  25. }
  26. if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) {
  27. return "${projectDir}/../../node_modules/react-native/cli.js"
  28. }
  29. throw new Exception("Couldn't determine CLI location. " +
  30. "Please set `project.ext.react.cliPath` to the path of the react-native cli.js");
  31. }
  32. def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js"
  33. def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
  34. def entryFile = detectEntryFile(config)
  35. def bundleCommand = config.bundleCommand ?: "bundle"
  36. def reactRoot = file(config.root ?: "../../")
  37. def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
  38. def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;
  39. def enableVmCleanup = config.enableVmCleanup == null ? true : config.enableVmCleanup
  40. def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%OS-BIN%/hermesc"
  41. def reactNativeDevServerPort() {
  42. def value = project.getProperties().get("reactNativeDevServerPort")
  43. return value != null ? value : "8081"
  44. }
  45. def reactNativeInspectorProxyPort() {
  46. def value = project.getProperties().get("reactNativeInspectorProxyPort")
  47. return value != null ? value : reactNativeDevServerPort()
  48. }
  49. def getHermesOSBin() {
  50. if (Os.isFamily(Os.FAMILY_WINDOWS)) return "win64-bin";
  51. if (Os.isFamily(Os.FAMILY_MAC)) return "osx-bin";
  52. if (Os.isOs(null, "linux", "amd64", null)) return "linux64-bin";
  53. throw new Exception("OS not recognized. Please set project.ext.react.hermesCommand " +
  54. "to the path of a working Hermes compiler.");
  55. }
  56. // Make sure not to inspect the Hermes config unless we need it,
  57. // to avoid breaking any JSC-only setups.
  58. def getHermesCommand = {
  59. // If the project specifies a Hermes command, don't second guess it.
  60. if (!hermesCommand.contains("%OS-BIN%")) {
  61. return hermesCommand
  62. }
  63. // Execution on Windows fails with / as separator
  64. return hermesCommand
  65. .replaceAll("%OS-BIN%", getHermesOSBin())
  66. .replace('/' as char, File.separatorChar);
  67. }
  68. // Set enableHermesForVariant to a function to configure per variant,
  69. // or set `enableHermes` to True/False to set all of them
  70. def enableHermesForVariant = config.enableHermesForVariant ?: {
  71. def variant -> config.enableHermes ?: false
  72. }
  73. android {
  74. buildTypes.all {
  75. resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort()
  76. resValue "integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort()
  77. }
  78. }
  79. afterEvaluate {
  80. def isAndroidLibrary = plugins.hasPlugin("com.android.library")
  81. def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants
  82. variants.all { def variant ->
  83. // Create variant and target names
  84. def targetName = variant.name.capitalize()
  85. def targetPath = variant.dirName
  86. // React js bundle directories
  87. def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
  88. def resourcesDir = file("$buildDir/generated/res/react/${targetPath}")
  89. def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
  90. def jsSourceMapsDir = file("$buildDir/generated/sourcemaps/react/${targetPath}")
  91. def jsIntermediateSourceMapsDir = file("$buildDir/intermediates/sourcemaps/react/${targetPath}")
  92. def jsPackagerSourceMapFile = file("$jsIntermediateSourceMapsDir/${bundleAssetName}.packager.map")
  93. def jsCompilerSourceMapFile = file("$jsIntermediateSourceMapsDir/${bundleAssetName}.compiler.map")
  94. def jsOutputSourceMapFile = file("$jsSourceMapsDir/${bundleAssetName}.map")
  95. // Additional node and packager commandline arguments
  96. def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
  97. def cliPath = detectCliPath(config)
  98. def execCommand = []
  99. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  100. execCommand.addAll(["cmd", "/c", *nodeExecutableAndArgs, cliPath])
  101. } else {
  102. execCommand.addAll([*nodeExecutableAndArgs, cliPath])
  103. }
  104. def enableHermes = enableHermesForVariant(variant)
  105. def currentBundleTask = tasks.create(
  106. name: "bundle${targetName}JsAndAssets",
  107. type: Exec) {
  108. group = "react"
  109. description = "bundle JS and assets for ${targetName}."
  110. // Create dirs if they are not there (e.g. the "clean" task just ran)
  111. doFirst {
  112. jsBundleDir.deleteDir()
  113. jsBundleDir.mkdirs()
  114. resourcesDir.deleteDir()
  115. resourcesDir.mkdirs()
  116. jsIntermediateSourceMapsDir.deleteDir()
  117. jsIntermediateSourceMapsDir.mkdirs()
  118. jsSourceMapsDir.deleteDir()
  119. jsSourceMapsDir.mkdirs()
  120. }
  121. // Set up inputs and outputs so gradle can cache the result
  122. inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
  123. outputs.dir(jsBundleDir)
  124. outputs.dir(resourcesDir)
  125. // Set up the call to the react-native cli
  126. workingDir(reactRoot)
  127. // Set up dev mode
  128. def devEnabled = !(config."devDisabledIn${targetName}"
  129. || targetName.toLowerCase().contains("release"))
  130. def extraArgs = config.extraPackagerArgs ?: [];
  131. if (bundleConfig) {
  132. extraArgs = extraArgs.clone()
  133. extraArgs.add("--config");
  134. extraArgs.add(bundleConfig);
  135. }
  136. commandLine(*execCommand, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
  137. "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir,
  138. "--sourcemap-output", enableHermes ? jsPackagerSourceMapFile : jsOutputSourceMapFile, *extraArgs)
  139. if (enableHermes) {
  140. doLast {
  141. def hermesFlags;
  142. def hbcTempFile = file("${jsBundleFile}.hbc")
  143. exec {
  144. if (targetName.toLowerCase().contains("release")) {
  145. // Can't use ?: since that will also substitute valid empty lists
  146. hermesFlags = config.hermesFlagsRelease
  147. if (hermesFlags == null) hermesFlags = ["-O", "-output-source-map"]
  148. } else {
  149. hermesFlags = config.hermesFlagsDebug
  150. if (hermesFlags == null) hermesFlags = []
  151. }
  152. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  153. commandLine("cmd", "/c", getHermesCommand(), "-emit-binary", "-out", hbcTempFile, jsBundleFile, *hermesFlags)
  154. } else {
  155. commandLine(getHermesCommand(), "-emit-binary", "-out", hbcTempFile, jsBundleFile, *hermesFlags)
  156. }
  157. }
  158. ant.move(
  159. file: hbcTempFile,
  160. toFile: jsBundleFile
  161. );
  162. if (hermesFlags.contains("-output-source-map")) {
  163. ant.move(
  164. // Hermes will generate a source map with this exact name
  165. file: "${jsBundleFile}.hbc.map",
  166. tofile: jsCompilerSourceMapFile
  167. );
  168. exec {
  169. // TODO: set task dependencies for caching
  170. // Set up the call to the compose-source-maps script
  171. workingDir(reactRoot)
  172. if (Os.isFamily(Os.FAMILY_WINDOWS)) {
  173. commandLine("cmd", "/c", *nodeExecutableAndArgs, composeSourceMapsPath, jsPackagerSourceMapFile, jsCompilerSourceMapFile, "-o", jsOutputSourceMapFile)
  174. } else {
  175. commandLine(*nodeExecutableAndArgs, composeSourceMapsPath, jsPackagerSourceMapFile, jsCompilerSourceMapFile, "-o", jsOutputSourceMapFile)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. enabled config."bundleIn${targetName}" != null
  182. ? config."bundleIn${targetName}"
  183. : config."bundleIn${variant.buildType.name.capitalize()}" != null
  184. ? config."bundleIn${variant.buildType.name.capitalize()}"
  185. : targetName.toLowerCase().contains("release")
  186. }
  187. // Expose a minimal interface on the application variant and the task itself:
  188. variant.ext.bundleJsAndAssets = currentBundleTask
  189. currentBundleTask.ext.generatedResFolders = files(resourcesDir).builtBy(currentBundleTask)
  190. currentBundleTask.ext.generatedAssetsFolders = files(jsBundleDir).builtBy(currentBundleTask)
  191. // registerGeneratedResFolders for Android plugin 3.x
  192. if (variant.respondsTo("registerGeneratedResFolders")) {
  193. variant.registerGeneratedResFolders(currentBundleTask.generatedResFolders)
  194. } else {
  195. variant.registerResGeneratingTask(currentBundleTask)
  196. }
  197. variant.mergeResourcesProvider.get().dependsOn(currentBundleTask)
  198. // packageApplication for Android plugin 3.x
  199. def packageTask = variant.hasProperty("packageApplication")
  200. ? variant.packageApplicationProvider.get()
  201. : tasks.findByName("package${targetName}")
  202. if (variant.hasProperty("packageLibrary")) {
  203. packageTask = variant.packageLibrary
  204. }
  205. // pre bundle build task for Android plugin 3.2+
  206. def buildPreBundleTask = tasks.findByName("build${targetName}PreBundle")
  207. def resourcesDirConfigValue = config."resourcesDir${targetName}"
  208. if (resourcesDirConfigValue) {
  209. def currentCopyResTask = tasks.create(
  210. name: "copy${targetName}BundledResources",
  211. type: Copy) {
  212. group = "react"
  213. description = "copy bundled resources into custom location for ${targetName}."
  214. from(resourcesDir)
  215. into(file(resourcesDirConfigValue))
  216. dependsOn(currentBundleTask)
  217. enabled(currentBundleTask.enabled)
  218. }
  219. packageTask.dependsOn(currentCopyResTask)
  220. if (buildPreBundleTask != null) {
  221. buildPreBundleTask.dependsOn(currentCopyResTask)
  222. }
  223. }
  224. def currentAssetsCopyTask = tasks.create(
  225. name: "copy${targetName}BundledJs",
  226. type: Copy) {
  227. group = "react"
  228. description = "copy bundled JS into ${targetName}."
  229. if (config."jsBundleDir${targetName}") {
  230. from(jsBundleDir)
  231. into(file(config."jsBundleDir${targetName}"))
  232. } else {
  233. into ("$buildDir/intermediates")
  234. into ("assets/${targetPath}") {
  235. from(jsBundleDir)
  236. }
  237. // Workaround for Android Gradle Plugin 3.2+ new asset directory
  238. into ("merged_assets/${variant.name}/merge${targetName}Assets/out") {
  239. from(jsBundleDir)
  240. }
  241. // Workaround for Android Gradle Plugin 3.4+ new asset directory
  242. into ("merged_assets/${variant.name}/out") {
  243. from(jsBundleDir)
  244. }
  245. }
  246. // mergeAssets must run first, as it clears the intermediates directory
  247. dependsOn(variant.mergeAssetsProvider.get())
  248. enabled(currentBundleTask.enabled)
  249. }
  250. // mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+.
  251. // This ensures to copy the bundle file before mergeResources task starts
  252. def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
  253. mergeResourcesTask.dependsOn(currentAssetsCopyTask)
  254. packageTask.dependsOn(currentAssetsCopyTask)
  255. if (buildPreBundleTask != null) {
  256. buildPreBundleTask.dependsOn(currentAssetsCopyTask)
  257. }
  258. // Delete the VM related libraries that this build doesn't need.
  259. // The application can manage this manually by setting 'enableVmCleanup: false'
  260. //
  261. // This should really be done by packaging all Hermes releated libs into
  262. // two separate HermesDebug and HermesRelease AARs, but until then we'll
  263. // kludge it by deleting the .so files out of the /transforms/ directory.
  264. def isRelease = targetName.toLowerCase().contains("release")
  265. def libDir = "$buildDir/intermediates/transforms/"
  266. def vmSelectionAction = {
  267. fileTree(libDir).matching {
  268. if (enableHermes) {
  269. // For Hermes, delete all the libjsc* files
  270. include "**/libjsc*.so"
  271. if (isRelease) {
  272. // Reduce size by deleting the debugger/inspector
  273. include '**/libhermes-inspector.so'
  274. include '**/libhermes-executor-debug.so'
  275. } else {
  276. // Release libs take precedence and must be removed
  277. // to allow debugging
  278. include '**/libhermes-executor-release.so'
  279. }
  280. } else {
  281. // For JSC, delete all the libhermes* files
  282. include "**/libhermes*.so"
  283. }
  284. }.visit { details ->
  285. def targetVariant = ".*/transforms/[^/]*/${targetPath}/.*"
  286. def path = details.file.getAbsolutePath().replace(File.separatorChar, '/' as char)
  287. if (path.matches(targetVariant) && details.file.isFile()) {
  288. details.file.delete()
  289. }
  290. }
  291. }
  292. if (enableVmCleanup) {
  293. def task = tasks.findByName("package${targetName}")
  294. task.doFirst(vmSelectionAction)
  295. }
  296. }
  297. }