diff --git a/android/app/build.gradle b/android/app/build.gradle index 8d497bfdb..26da5a211 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -18,8 +18,10 @@ if (autodetectReactNativeVersion || enableNewArchitecture) { react { reactNativeDir = reactNativePath codegenDir = file( - findNodeModulesPath("@react-native/codegen", reactNativePath) // >= 0.72 - ?: findNodeModulesPath("react-native-codegen", reactNativePath)) // < 0.72 + reactNativeVersion >= v(0, 72, 0) + ? findNodeModulesPath("@react-native/codegen", reactNativePath) + : findNodeModulesPath("react-native-codegen", reactNativePath) + ) } // We don't want the React plugin to bundle. diff --git a/scripts/configure-projects.js b/scripts/configure-projects.js index e7ef9cc96..359098350 100644 --- a/scripts/configure-projects.js +++ b/scripts/configure-projects.js @@ -7,6 +7,7 @@ */ const nodefs = require("node:fs"); const path = require("node:path"); +const tty = require("node:tty"); const { findNearest, getPackageVersion, @@ -42,6 +43,76 @@ const cliPlatformIOSVersion = (() => { }; })(); +/** + * Configures Gradle wrapper as necessary before the Android app is built. + * @param {string} sourceDir + */ +function configureGradleWrapper(sourceDir, fs = nodefs) { + const androidCommands = ["build-android", "run-android"]; + if ( + process.env["RNTA_CONFIGURE_GRADLE_WRAPPER"] === "0" || + !process.argv.some((arg) => androidCommands.includes(arg)) + ) { + return; + } + + const gradleWrapperProperties = path.join( + sourceDir, + "gradle", + "wrapper", + "gradle-wrapper.properties" + ); + if (!fs.existsSync(gradleWrapperProperties)) { + return; + } + + const tag = tty.WriteStream.prototype.hasColors() + ? "\u001B[33m\u001B[1mwarn\u001B[22m\u001B[39m" + : "warn"; + + try { + const props = readTextFile(gradleWrapperProperties); + const re = /gradle-([.0-9]*?)-.*?\.zip/; + const m = props.match(re); + if (!m) { + return; + } + + const gradleVersion = (() => { + const gradleVersion = toVersionNumber(m[1]); + const version = toVersionNumber( + getPackageVersion("react-native", sourceDir, fs) + ); + if (version === 0 || version >= v(0, 74, 0)) { + if (gradleVersion < v(8, 6, 0)) { + return "8.6"; + } + } else if (version >= v(0, 73, 0)) { + if (gradleVersion < v(8, 3, 0)) { + return "8.3"; + } + } else if (version >= v(0, 72, 0)) { + if (gradleVersion < v(8, 1, 1)) { + return "8.1.1"; + } + } else if (gradleVersion < v(7, 5, 1) || gradleVersion >= v(8, 0, 0)) { + return "7.6.4"; + } + return undefined; + })(); + + if (gradleVersion) { + console.warn(tag, `Setting Gradle version ${gradleVersion}`); + fs.writeFileSync( + gradleWrapperProperties, + props.replace(re, `gradle-${gradleVersion}-bin.zip`) + ); + } + } catch (_) { + console.warn(tag, "Failed to determine Gradle version"); + } +} + /** * @param {string} sourceDir * @returns {string} @@ -50,7 +121,7 @@ function androidManifestPath(sourceDir) { return path.relative( sourceDir, path.join( - path.dirname(require.resolve("../package.json")), + path.dirname(__dirname), "android", "app", "src", @@ -109,6 +180,7 @@ function configureProjects({ android, ios, windows }, fs = nodefs) { path.resolve(projectRoot, android.sourceDir) ), }; + configureGradleWrapper(android.sourceDir, fs); } if (ios) { diff --git a/scripts/set-react-version.mjs b/scripts/set-react-version.mjs index f188b4118..d1133f4fe 100755 --- a/scripts/set-react-version.mjs +++ b/scripts/set-react-version.mjs @@ -382,22 +382,6 @@ async function getProfile(v, coreOnly) { } } -/** - * Sets Gradle Wrapper to specified version. - * @param {string} version - */ -function setGradleVersion(version) { - const gradleWrapperProperties = - "example/android/gradle/wrapper/gradle-wrapper.properties"; - fs.writeFileSync( - gradleWrapperProperties, - readTextFile(gradleWrapperProperties).replace( - /gradle-[.0-9]*-bin\.zip/, - `gradle-${version}-bin.zip` - ) - ); -} - /** * Sets specified React Native version. * @param {string} version @@ -466,8 +450,6 @@ if (isMain(import.meta.url)) { : toVersionNumber(version); if (numVersion >= v(0, 74, 0)) { disableJetifier(); - } else if (numVersion < v(0, 73, 0)) { - setGradleVersion("7.6.3"); } // `@react-native-webapis/web-storage` is not compatible with codegen 0.71