build.gradle 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. //gms
  83. //apply plugin: 'com.google.gms.google-services'
  84. /**
  85. * Set this to true to create two separate APKs instead of one:
  86. * - An APK that only works on ARM devices
  87. * - An APK that only works on x86 devices
  88. * The advantage is the size of the APK is reduced by about 4MB.
  89. * Upload all the APKs to the Play Store and people will download
  90. * the correct one based on the CPU architecture of their device.
  91. */
  92. def enableSeparateBuildPerCPUArchitecture = false
  93. /**
  94. * Run Proguard to shrink the Java bytecode in release builds.
  95. */
  96. def enableProguardInReleaseBuilds = false
  97. /**
  98. * The preferred build flavor of JavaScriptCore.
  99. *
  100. * For example, to use the international variant, you can use:
  101. * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
  102. *
  103. * The international variant includes ICU i18n library and necessary data
  104. * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
  105. * give correct results when using with locales other than en-US. Note that
  106. * this variant is about 6MiB larger per architecture than default.
  107. */
  108. //def jscFlavor = 'org.webkit:android-jsc:+'
  109. def jscFlavor = 'org.webkit:android-jsc-intl:+'
  110. /**
  111. * Whether to enable the Hermes VM.
  112. *
  113. * This should be set on project.ext.react and that value will be read here. If it is not set
  114. * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
  115. * and the benefits of using Hermes will therefore be sharply reduced.
  116. */
  117. def enableHermes = project.ext.react.get("enableHermes", false);
  118. /**
  119. * Architectures to build native code for.
  120. */
  121. def reactNativeArchitectures() {
  122. def value = project.getProperties().get("reactNativeArchitectures")
  123. return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
  124. }
  125. android {
  126. ndkVersion rootProject.ext.ndkVersion
  127. compileSdkVersion rootProject.ext.compileSdkVersion
  128. aaptOptions.cruncherEnabled = false
  129. aaptOptions.useNewCruncher = false
  130. defaultConfig {
  131. applicationId app_id
  132. minSdkVersion rootProject.ext.minSdkVersion
  133. targetSdkVersion rootProject.ext.targetSdkVersion
  134. versionCode 1
  135. versionName "1.0.1"
  136. buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  137. manifestPlaceholders = [
  138. JPUSH_APPKEY: "7cf918ada725a9e9aecc8a17", //在此替换你的APPKey
  139. JPUSH_CHANNEL: "online" //在此替换你的channelÏ
  140. ]
  141. if (isNewArchitectureEnabled()) {
  142. // We configure the CMake build only if you decide to opt-in for the New Architecture.
  143. externalNativeBuild {
  144. cmake {
  145. arguments "-DPROJECT_BUILD_DIR=$buildDir",
  146. "-DREACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
  147. "-DREACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
  148. "-DNODE_MODULES_DIR=$rootDir/../node_modules",
  149. "-DANDROID_STL=c++_shared"
  150. }
  151. }
  152. if (!enableSeparateBuildPerCPUArchitecture) {
  153. ndk {
  154. abiFilters (*reactNativeArchitectures())
  155. }
  156. }
  157. }
  158. }
  159. if (isNewArchitectureEnabled()) {
  160. // We configure the NDK build only if you decide to opt-in for the New Architecture.
  161. externalNativeBuild {
  162. cmake {
  163. path "$projectDir/src/main/jni/CMakeLists.txt"
  164. }
  165. }
  166. def reactAndroidProjectDir = project(':ReactAndroid').projectDir
  167. def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
  168. dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
  169. from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
  170. into("$buildDir/react-ndk/exported")
  171. }
  172. def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
  173. dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
  174. from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
  175. into("$buildDir/react-ndk/exported")
  176. }
  177. afterEvaluate {
  178. // If you wish to add a custom TurboModule or component locally,
  179. // you should uncomment this line.
  180. // preBuild.dependsOn("generateCodegenArtifactsFromSchema")
  181. preDebugBuild.dependsOn(packageReactNdkDebugLibs)
  182. preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
  183. // Due to a bug inside AGP, we have to explicitly set a dependency
  184. // between configureCMakeDebug* tasks and the preBuild tasks.
  185. // This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
  186. configureCMakeRelWithDebInfo.dependsOn(preReleaseBuild)
  187. configureCMakeDebug.dependsOn(preDebugBuild)
  188. reactNativeArchitectures().each { architecture ->
  189. tasks.findByName("configureCMakeDebug[${architecture}]")?.configure {
  190. dependsOn("preDebugBuild")
  191. }
  192. tasks.findByName("configureCMakeRelWithDebInfo[${architecture}]")?.configure {
  193. dependsOn("preReleaseBuild")
  194. }
  195. }
  196. }
  197. }
  198. splits {
  199. abi {
  200. reset()
  201. enable enableSeparateBuildPerCPUArchitecture
  202. universalApk false // If true, also generate a universal APK
  203. include (*reactNativeArchitectures())
  204. }
  205. }
  206. signingConfigs {
  207. debug {
  208. storeFile file('debug.keystore')
  209. storePassword 'android'
  210. keyAlias 'androiddebugkey'
  211. keyPassword 'android'
  212. }
  213. }
  214. buildTypes {
  215. debug {
  216. signingConfig signingConfigs.debug
  217. }
  218. release {
  219. // Caution! In production, you need to generate your own keystore file.
  220. // see https://reactnative.dev/docs/signed-apk-android.
  221. signingConfig signingConfigs.debug
  222. minifyEnabled enableProguardInReleaseBuilds
  223. proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
  224. }
  225. }
  226. // applicationVariants are e.g. debug, release
  227. applicationVariants.all { variant ->
  228. variant.outputs.each { output ->
  229. // For each separate APK per architecture, set a unique version code as described here:
  230. // https://developer.android.com/studio/build/configure-apk-splits.html
  231. // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
  232. def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
  233. def abi = output.getFilter(OutputFile.ABI)
  234. if (abi != null) { // null for the universal-debug, universal-release variants
  235. output.versionCodeOverride =
  236. defaultConfig.versionCode * 1000 + versionCodes.get(abi)
  237. }
  238. }
  239. }
  240. }
  241. dependencies {
  242. implementation fileTree(dir: "libs", include: ["*.jar"])
  243. implementation project(':jpush-react-native') // 添加 jpush 依赖
  244. implementation project(':jcore-react-native') // 添加 jcore 依赖
  245. //noinspection GradleDynamicVersion
  246. implementation "com.facebook.react:react-native:+" // From node_modules
  247. implementation 'com.facebook.fresco:animated-gif:2.5.0'
  248. implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
  249. debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
  250. exclude group:'com.facebook.fbjni'
  251. }
  252. debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
  253. exclude group:'com.facebook.flipper'
  254. exclude group:'com.squareup.okhttp3', module:'okhttp'
  255. }
  256. debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
  257. exclude group:'com.facebook.flipper'
  258. }
  259. if (enableHermes) {
  260. //noinspection GradleDynamicVersion
  261. implementation("com.facebook.react:hermes-engine:+") { // From node_modules
  262. exclude group:'com.facebook.fbjni'
  263. }
  264. } else {
  265. implementation jscFlavor
  266. }
  267. //gms
  268. // implementation 'com.google.firebase:firebase-messaging:23.0.5'
  269. }
  270. if (isNewArchitectureEnabled()) {
  271. // If new architecture is enabled, we let you build RN from source
  272. // Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
  273. // This will be applied to all the imported transtitive dependency.
  274. configurations.all {
  275. resolutionStrategy.dependencySubstitution {
  276. substitute(module("com.facebook.react:react-native"))
  277. .using(project(":ReactAndroid"))
  278. .because("On New Architecture we're building React Native from source")
  279. substitute(module("com.facebook.react:hermes-engine"))
  280. .using(project(":ReactAndroid:hermes-engine"))
  281. .because("On New Architecture we're building Hermes from source")
  282. }
  283. }
  284. }
  285. // Run this once to be able to run the application with BUCK
  286. // puts all compile dependencies into folder libs for BUCK to use
  287. task copyDownloadableDepsToLibs(type: Copy) {
  288. from configurations.implementation
  289. into 'libs'
  290. }
  291. apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
  292. def isNewArchitectureEnabled() {
  293. // To opt-in for the New Architecture, you can either:
  294. // - Set `newArchEnabled` to true inside the `gradle.properties` file
  295. // - Invoke gradle with `-newArchEnabled=true`
  296. // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
  297. return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
  298. }