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