|
1 | 1 | // @ts-check |
2 | 2 | import * as nodefs from "node:fs"; |
3 | 3 | import * as path from "node:path"; |
4 | | -import { findFile } from "../scripts/helpers.js"; |
| 4 | +import { findFile, toVersionNumber } from "../scripts/helpers.js"; |
5 | 5 | import { generateAndroidManifest } from "./android-manifest.js"; |
6 | 6 | import { configureGradleWrapper } from "./gradle-wrapper.js"; |
7 | 7 |
|
8 | | -/** @import { ProjectConfig, ProjectParams } from "../scripts/types.js"; */ |
| 8 | +/** |
| 9 | + * @import { |
| 10 | + * Configuration, |
| 11 | + * ConfigureParams, |
| 12 | + * ProjectConfig, |
| 13 | + * ProjectParams, |
| 14 | + * } from "../scripts/types.js"; |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @returns {string} |
| 19 | + */ |
| 20 | +export function buildGradle() { |
| 21 | + return `buildscript { |
| 22 | + apply(from: { |
| 23 | + def searchDir = rootDir.toPath() |
| 24 | + do { |
| 25 | + def p = searchDir.resolve("node_modules/react-native-test-app/android/dependencies.gradle") |
| 26 | + if (p.toFile().exists()) { |
| 27 | + return p.toRealPath().toString() |
| 28 | + } |
| 29 | + } while (searchDir = searchDir.getParent()) |
| 30 | + throw new GradleException("Could not find \`react-native-test-app\`"); |
| 31 | + }()) |
| 32 | +
|
| 33 | + repositories { |
| 34 | + mavenCentral() |
| 35 | + google() |
| 36 | + } |
| 37 | +
|
| 38 | + dependencies { |
| 39 | + getReactNativeDependencies().each { dependency -> |
| 40 | + classpath(dependency) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +`; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {number} _targetVersion Target React Native version |
| 49 | + * @returns {string} |
| 50 | + */ |
| 51 | +export function gradleProperties(_targetVersion) { |
| 52 | + return `# Project-wide Gradle settings. |
| 53 | +
|
| 54 | +# IDE (e.g. Android Studio) users: |
| 55 | +# Gradle settings configured through the IDE *will override* |
| 56 | +# any settings specified in this file. |
| 57 | +
|
| 58 | +# For more details on how to configure your build environment visit |
| 59 | +# http://www.gradle.org/docs/current/userguide/build_environment.html |
| 60 | +
|
| 61 | +# Specifies the JVM arguments used for the Gradle Daemon. The setting is |
| 62 | +# particularly useful for configuring JVM memory settings for build performance. |
| 63 | +# This does not affect the JVM settings for the Gradle client VM. |
| 64 | +# The default is \`-Xmx512m -XX:MaxMetaspaceSize=256m\`. |
| 65 | +org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 |
| 66 | +
|
| 67 | +# When configured, Gradle will fork up to org.gradle.workers.max JVMs to execute |
| 68 | +# projects in parallel. To learn more about parallel task execution, see the |
| 69 | +# section on Gradle build performance: |
| 70 | +# https://docs.gradle.org/current/userguide/performance.html#parallel_execution. |
| 71 | +# Default is \`false\`. |
| 72 | +#org.gradle.parallel=true |
| 73 | +
|
| 74 | +# AndroidX package structure to make it clearer which packages are bundled with the |
| 75 | +# Android operating system, and which are packaged with your app's APK |
| 76 | +# https://developer.android.com/topic/libraries/support-library/androidx-rn |
| 77 | +android.useAndroidX=true |
| 78 | +# Automatically convert third-party libraries to use AndroidX |
| 79 | +#android.enableJetifier=true |
| 80 | +# Jetifier randomly fails on these libraries |
| 81 | +#android.jetifier.ignorelist=hermes-android,react-android |
| 82 | +
|
| 83 | +# Use this property to specify which architecture you want to build. |
| 84 | +# You can also override it from the CLI using |
| 85 | +# ./gradlew <task> -PreactNativeArchitectures=x86_64 |
| 86 | +reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64 |
| 87 | +
|
| 88 | +# Use this property to enable support to the new architecture. |
| 89 | +# This will allow you to use TurboModules and the Fabric render in |
| 90 | +# your application. You should enable this flag either if you want |
| 91 | +# to write custom TurboModules/Fabric components OR use libraries that |
| 92 | +# are providing them. |
| 93 | +# Note that this is incompatible with web debugging. |
| 94 | +newArchEnabled=true |
| 95 | +#bridgelessEnabled=true |
| 96 | +
|
| 97 | +# Uncomment the line below to build React Native from source. |
| 98 | +#react.buildFromSource=true |
| 99 | +
|
| 100 | +# Version of Android NDK to build against. |
| 101 | +#ANDROID_NDK_VERSION=26.1.10909125 |
| 102 | +
|
| 103 | +# Version of Kotlin to build against. |
| 104 | +#KOTLIN_VERSION=1.8.22 |
| 105 | +`; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * @param {string} name Root project name |
| 110 | + * @returns {string} |
| 111 | + */ |
| 112 | +export function settingsGradle(name) { |
| 113 | + return `pluginManagement { |
| 114 | + repositories { |
| 115 | + gradlePluginPortal() |
| 116 | + mavenCentral() |
| 117 | + google() |
| 118 | + } |
| 119 | +} |
| 120 | +
|
| 121 | +rootProject.name = "${name}" |
| 122 | +
|
| 123 | +apply(from: { |
| 124 | + def searchDir = rootDir.toPath() |
| 125 | + do { |
| 126 | + def p = searchDir.resolve("node_modules/react-native-test-app/test-app.gradle") |
| 127 | + if (p.toFile().exists()) { |
| 128 | + return p.toRealPath().toString() |
| 129 | + } |
| 130 | + } while (searchDir = searchDir.getParent()) |
| 131 | + throw new GradleException("Could not find \`react-native-test-app\`"); |
| 132 | +}()) |
| 133 | +applyTestAppSettings(settings) |
| 134 | +`; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @param {...string} paths |
| 139 | + * @returns {{ source: string; }} |
| 140 | + */ |
| 141 | +function copyFrom(...paths) { |
| 142 | + return { source: path.join(...paths) }; |
| 143 | +} |
9 | 144 |
|
10 | 145 | /** |
11 | 146 | * @returns {string | undefined} |
@@ -47,3 +182,43 @@ export function configure( |
47 | 182 | packageName: packageName || getAndroidPackageName(), |
48 | 183 | }; |
49 | 184 | } |
| 185 | + |
| 186 | +/** |
| 187 | + * @param {ConfigureParams} params |
| 188 | + * @returns {Configuration} |
| 189 | + */ |
| 190 | +export function getTemplate({ name, testAppPath, targetVersion }) { |
| 191 | + const targetVersionNum = toVersionNumber(targetVersion); |
| 192 | + return { |
| 193 | + files: { |
| 194 | + "build.gradle": buildGradle(), |
| 195 | + "gradle/wrapper/gradle-wrapper.jar": copyFrom( |
| 196 | + testAppPath, |
| 197 | + "example", |
| 198 | + "android", |
| 199 | + "gradle", |
| 200 | + "wrapper", |
| 201 | + "gradle-wrapper.jar" |
| 202 | + ), |
| 203 | + "gradle/wrapper/gradle-wrapper.properties": copyFrom( |
| 204 | + testAppPath, |
| 205 | + "example", |
| 206 | + "android", |
| 207 | + "gradle", |
| 208 | + "wrapper", |
| 209 | + "gradle-wrapper.properties" |
| 210 | + ), |
| 211 | + "gradle.properties": gradleProperties(targetVersionNum), |
| 212 | + gradlew: copyFrom(testAppPath, "example", "android", "gradlew"), |
| 213 | + "gradlew.bat": copyFrom(testAppPath, "example", "android", "gradlew.bat"), |
| 214 | + "settings.gradle": settingsGradle(name), |
| 215 | + }, |
| 216 | + oldFiles: [], |
| 217 | + scripts: { |
| 218 | + android: "react-native run-android", |
| 219 | + "build:android": |
| 220 | + "react-native bundle --entry-file index.js --platform android --dev true --bundle-output dist/main.android.jsbundle --assets-dest dist/res", |
| 221 | + }, |
| 222 | + dependencies: {}, |
| 223 | + }; |
| 224 | +} |
0 commit comments