build.gradle 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. apply plugin: "com.android.application"
  2. import com.android.build.OutputFile
  3. import org.apache.tools.ant.taskdefs.condition.Os
  4. /**
  5. * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
  6. * and bundleReleaseJsAndAssets).
  7. * These basically call `react-native bundle` with the correct arguments during the Android build
  8. * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
  9. * bundle directly from the development server. Below you can see all the possible configurations
  10. * and their defaults. If you decide to add a configuration block, make sure to add it before the
  11. * `apply from: "../../node_modules/react-native/react.gradle"` line.
  12. *
  13. * project.ext.react = [
  14. * // the name of the generated asset file containing your JS bundle
  15. * bundleAssetName: "index.android.bundle",
  16. *
  17. * // the entry file for bundle generation. If none specified and
  18. * // "index.android.js" exists, it will be used. Otherwise "index.js" is
  19. * // default. Can be overridden with ENTRY_FILE environment variable.
  20. * entryFile: "index.android.js",
  21. *
  22. * // https://reactnative.dev/docs/performance#enable-the-ram-format
  23. * bundleCommand: "ram-bundle",
  24. *
  25. * // whether to bundle JS and assets in debug mode
  26. * bundleInDebug: false,
  27. *
  28. * // whether to bundle JS and assets in release mode
  29. * bundleInRelease: true,
  30. *
  31. * // whether to bundle JS and assets in another build variant (if configured).
  32. * // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
  33. * // The configuration property can be in the following formats
  34. * // 'bundleIn${productFlavor}${buildType}'
  35. * // 'bundleIn${buildType}'
  36. * // bundleInFreeDebug: true,
  37. * // bundleInPaidRelease: true,
  38. * // bundleInBeta: true,
  39. *
  40. * // whether to disable dev mode in custom build variants (by default only disabled in release)
  41. * // for example: to disable dev mode in the staging build type (if configured)
  42. * devDisabledInStaging: true,
  43. * // The configuration property can be in the following formats
  44. * // 'devDisabledIn${productFlavor}${buildType}'
  45. * // 'devDisabledIn${buildType}'
  46. *
  47. * // the root of your project, i.e. where "package.json" lives
  48. * root: "../../",
  49. *
  50. * // where to put the JS bundle asset in debug mode
  51. * jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
  52. *
  53. * // where to put the JS bundle asset in release mode
  54. * jsBundleDirRelease: "$buildDir/intermediates/assets/release",
  55. *
  56. * // where to put drawable resources / React Native assets, e.g. the ones you use via
  57. * // require('./image.png')), in debug mode
  58. * resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
  59. *
  60. * // where to put drawable resources / React Native assets, e.g. the ones you use via
  61. * // require('./image.png')), in release mode
  62. * resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
  63. *
  64. * // by default the gradle tasks are skipped if none of the JS files or assets change; this means
  65. * // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
  66. * // date; if you have any other folders that you want to ignore for performance reasons (gradle
  67. * // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
  68. * // for example, you might want to remove it from here.
  69. * inputExcludes: ["android/**", "ios/**"],
  70. *
  71. * // override which node gets called and with what additional arguments
  72. * nodeExecutableAndArgs: ["node"],
  73. *
  74. * // supply additional arguments to the packager
  75. * extraPackagerArgs: []
  76. * ]
  77. */
  78. project.ext.react = [
  79. enableHermes: true, // clean and rebuild if changing
  80. ]
  81. apply from: "../../node_modules/react-native/react.gradle"
  82. /**
  83. * Set this to true to create two separate APKs instead of one:
  84. * - An APK that only works on ARM devices
  85. * - An APK that only works on x86 devices
  86. * The advantage is the size of the APK is reduced by about 4MB.
  87. * Upload all the APKs to the Play Store and people will download
  88. * the correct one based on the CPU architecture of their device.
  89. */
  90. def enableSeparateBuildPerCPUArchitecture = false
  91. /**
  92. * Run Proguard to shrink the Java bytecode in release builds.
  93. */
  94. def enableProguardInReleaseBuilds = false
  95. /**
  96. * The preferred build flavor of JavaScriptCore.
  97. *
  98. * For example, to use the international variant, you can use:
  99. * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
  100. *
  101. * The international variant includes ICU i18n library and necessary data
  102. * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
  103. * give correct results when using with locales other than en-US. Note that
  104. * this variant is about 6MiB larger per architecture than default.
  105. */
  106. //def jscFlavor = 'org.webkit:android-jsc:+'
  107. def jscFlavor = 'org.webkit:android-jsc-intl:+'
  108. /**
  109. * Whether to enable the Hermes VM.
  110. *
  111. * This should be set on project.ext.react and that value will be read here. If it is not set
  112. * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
  113. * and the benefits of using Hermes will therefore be sharply reduced.
  114. */
  115. def enableHermes = project.ext.react.get("enableHermes", false);
  116. /**
  117. * Architectures to build native code for.
  118. */
  119. def reactNativeArchitectures() {
  120. def value = project.getProperties().get("reactNativeArchitectures")
  121. return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
  122. }
  123. android {
  124. ndkVersion rootProject.ext.ndkVersion
  125. compileSdkVersion rootProject.ext.compileSdkVersion
  126. aaptOptions.cruncherEnabled = false
  127. aaptOptions.useNewCruncher = false
  128. defaultConfig {
  129. applicationId app_id
  130. minSdkVersion rootProject.ext.minSdkVersion
  131. targetSdkVersion rootProject.ext.targetSdkVersion
  132. versionCode 1
  133. versionName "1.0"
  134. buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  135. if (isNewArchitectureEnabled()) {
  136. // We configure the CMake build only if you decide to opt-in for the New Architecture.
  137. externalNativeBuild {
  138. cmake {
  139. arguments "-DPROJECT_BUILD_DIR=$buildDir",
  140. "-DREACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
  141. "-DREACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
  142. "-DNODE_MODULES_DIR=$rootDir/../node_modules",
  143. "-DANDROID_STL=c++_shared"
  144. }
  145. }
  146. if (!enableSeparateBuildPerCPUArchitecture) {
  147. ndk {
  148. abiFilters (*reactNativeArchitectures())
  149. }
  150. }
  151. }
  152. }
  153. if (isNewArchitectureEnabled()) {
  154. // We configure the NDK build only if you decide to opt-in for the New Architecture.
  155. externalNativeBuild {
  156. cmake {
  157. path "$projectDir/src/main/jni/CMakeLists.txt"
  158. }
  159. }
  160. def reactAndroidProjectDir = project(':ReactAndroid').projectDir
  161. def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
  162. dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
  163. from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
  164. into("$buildDir/react-ndk/exported")
  165. }
  166. def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
  167. dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
  168. from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
  169. into("$buildDir/react-ndk/exported")
  170. }
  171. afterEvaluate {
  172. // If you wish to add a custom TurboModule or component locally,
  173. // you should uncomment this line.
  174. // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
  175. preDebugBuild.dependsOn(packageReactNdkDebugLibs)
  176. preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
  177. // Due to a bug inside AGP, we have to explicitly set a dependency
  178. // between configureCMakeDebug* tasks and the preBuild tasks.
  179. // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
  180. configureCMakeRelWithDebInfo.dependsOn(preReleaseBuild)
  181. configureCMakeDebug.dependsOn(preDebugBuild)
  182. reactNativeArchitectures().each { architecture ->
  183. tasks.findByName("configureCMakeDebug[${architecture}]")?.configure {
  184. dependsOn("preDebugBuild")
  185. }
  186. tasks.findByName("configureCMakeRelWithDebInfo[${architecture}]")?.configure {
  187. dependsOn("preReleaseBuild")
  188. }
  189. }
  190. }
  191. }
  192. splits {
  193. abi {
  194. reset()
  195. enable enableSeparateBuildPerCPUArchitecture
  196. universalApk false // If true, also generate a universal APK
  197. include (*reactNativeArchitectures())
  198. }
  199. }
  200. signingConfigs {
  201. debug {
  202. storeFile file('debug.keystore')
  203. storePassword 'android'
  204. keyAlias 'androiddebugkey'
  205. keyPassword 'android'
  206. }
  207. }
  208. buildTypes {
  209. debug {
  210. signingConfig signingConfigs.debug
  211. }
  212. release {
  213. // Caution! In production, you need to generate your own keystore file.
  214. // see https://reactnative.dev/docs/signed-apk-android.
  215. signingConfig signingConfigs.debug
  216. minifyEnabled enableProguardInReleaseBuilds
  217. proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
  218. }
  219. }
  220. // applicationVariants are e.g. debug, release
  221. applicationVariants.all { variant ->
  222. variant.outputs.each { output ->
  223. // For each separate APK per architecture, set a unique version code as described here:
  224. // https://developer.android.com/studio/build/configure-apk-splits.html
  225. // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
  226. def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
  227. def abi = output.getFilter(OutputFile.ABI)
  228. if (abi != null) { // null for the universal-debug, universal-release variants
  229. output.versionCodeOverride =
  230. defaultConfig.versionCode * 1000 + versionCodes.get(abi)
  231. }
  232. }
  233. }
  234. }
  235. dependencies {
  236. implementation fileTree(dir: "libs", include: ["*.jar"])
  237. //noinspection GradleDynamicVersion
  238. implementation "com.facebook.react:react-native:+" // From node_modules
  239. implementation 'com.facebook.fresco:animated-gif:2.5.0'
  240. implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
  241. debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
  242. exclude group:'com.facebook.fbjni'
  243. }
  244. debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
  245. exclude group:'com.facebook.flipper'
  246. exclude group:'com.squareup.okhttp3', module:'okhttp'
  247. }
  248. debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
  249. exclude group:'com.facebook.flipper'
  250. }
  251. if (enableHermes) {
  252. //noinspection GradleDynamicVersion
  253. implementation("com.facebook.react:hermes-engine:+") { // From node_modules
  254. exclude group:'com.facebook.fbjni'
  255. }
  256. } else {
  257. implementation jscFlavor
  258. }
  259. }
  260. if (isNewArchitectureEnabled()) {
  261. // If new architecture is enabled, we let you build RN from source
  262. // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
  263. // This will be applied to all the imported transtitive dependency.
  264. configurations.all {
  265. resolutionStrategy.dependencySubstitution {
  266. substitute(module("com.facebook.react:react-native"))
  267. .using(project(":ReactAndroid"))
  268. .because("On New Architecture we're building React Native from source")
  269. substitute(module("com.facebook.react:hermes-engine"))
  270. .using(project(":ReactAndroid:hermes-engine"))
  271. .because("On New Architecture we're building Hermes from source")
  272. }
  273. }
  274. }
  275. // Run this once to be able to run the application with BUCK
  276. // puts all compile dependencies into folder libs for BUCK to use
  277. task copyDownloadableDepsToLibs(type: Copy) {
  278. from configurations.implementation
  279. into 'libs'
  280. }
  281. apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
  282. def isNewArchitectureEnabled() {
  283. // To opt-in for the New Architecture, you can either:
  284. // - Set `newArchEnabled` to true inside the `gradle.properties` file
  285. // - Invoke gradle with `-newArchEnabled=true`
  286. // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  287. return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
  288. }