Brak opisu

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