Skip to content

Commit 1687f97

Browse files
committed
fix(android): automatically configure Gradle wrapper
To disable this behavior, set `RNTA_CONFIGURE_GRADLE_WRAPPER=0`.
1 parent 211891f commit 1687f97

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

android/app/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ if (autodetectReactNativeVersion || enableNewArchitecture) {
1818
react {
1919
reactNativeDir = reactNativePath
2020
codegenDir = file(
21-
findNodeModulesPath("@react-native/codegen", reactNativePath) // >= 0.72
22-
?: findNodeModulesPath("react-native-codegen", reactNativePath)) // < 0.72
21+
reactNativeVersion >= v(0, 72, 0)
22+
? findNodeModulesPath("@react-native/codegen", reactNativePath)
23+
: findNodeModulesPath("react-native-codegen", reactNativePath)
24+
)
2325
}
2426

2527
// We don't want the React plugin to bundle.

scripts/configure-projects.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
const nodefs = require("node:fs");
99
const path = require("node:path");
10+
const tty = require("node:tty");
1011
const {
1112
findNearest,
1213
getPackageVersion,
@@ -42,6 +43,72 @@ const cliPlatformIOSVersion = (() => {
4243
};
4344
})();
4445

46+
/**
47+
* Configures Gradle wrapper as necessary before the Android app is built.
48+
* @param {string} sourceDir
49+
*/
50+
function configureGradleWrapper(sourceDir, fs = nodefs) {
51+
const androidCommands = ["build-android", "run-android"];
52+
if (
53+
process.env["RNTA_CONFIGURE_GRADLE_WRAPPER"] === "0" ||
54+
!process.argv.some((arg) => androidCommands.includes(arg))
55+
) {
56+
return;
57+
}
58+
59+
try {
60+
const version = toVersionNumber(
61+
getPackageVersion("react-native", sourceDir, fs)
62+
);
63+
64+
const gradleWrapperProperties = path.join(
65+
sourceDir,
66+
"gradle",
67+
"wrapper",
68+
"gradle-wrapper.properties"
69+
);
70+
const props = readTextFile(gradleWrapperProperties);
71+
const re = /gradle-([.0-9]*?)-.*?\.zip/;
72+
const m = props.match(re);
73+
if (!m) {
74+
return;
75+
}
76+
77+
const gradleVersion = (() => {
78+
const gradleVersion = toVersionNumber(m[1]);
79+
if (version === 0 || version >= v(0, 74, 0)) {
80+
if (gradleVersion < v(8, 6, 0)) {
81+
return "8.6";
82+
}
83+
} else if (version >= v(0, 73, 0)) {
84+
if (gradleVersion < v(8, 3, 0)) {
85+
return "8.3";
86+
}
87+
} else if (version >= v(0, 72, 0)) {
88+
if (gradleVersion < v(8, 1, 1)) {
89+
return "8.1.1";
90+
}
91+
} else if (gradleVersion < v(7, 5, 1) || gradleVersion >= v(8, 0, 0)) {
92+
return "7.6.4";
93+
}
94+
return undefined;
95+
})();
96+
97+
if (gradleVersion) {
98+
const tag = tty.WriteStream.prototype.hasColors()
99+
? "\u001B[33m\u001B[1mwarn\u001B[22m\u001B[39m"
100+
: "warn";
101+
console.warn(tag, `Setting Gradle version ${gradleVersion}`);
102+
fs.writeFileSync(
103+
gradleWrapperProperties,
104+
props.replace(re, `gradle-${gradleVersion}-bin.zip`)
105+
);
106+
}
107+
} catch (_) {
108+
// ignore
109+
}
110+
}
111+
45112
/**
46113
* @param {string} sourceDir
47114
* @returns {string}
@@ -109,6 +176,7 @@ function configureProjects({ android, ios, windows }, fs = nodefs) {
109176
path.resolve(projectRoot, android.sourceDir)
110177
),
111178
};
179+
configureGradleWrapper(android.sourceDir, fs);
112180
}
113181

114182
if (ios) {

scripts/set-react-version.mjs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -382,22 +382,6 @@ async function getProfile(v, coreOnly) {
382382
}
383383
}
384384

385-
/**
386-
* Sets Gradle Wrapper to specified version.
387-
* @param {string} version
388-
*/
389-
function setGradleVersion(version) {
390-
const gradleWrapperProperties =
391-
"example/android/gradle/wrapper/gradle-wrapper.properties";
392-
fs.writeFileSync(
393-
gradleWrapperProperties,
394-
readTextFile(gradleWrapperProperties).replace(
395-
/gradle-[.0-9]*-bin\.zip/,
396-
`gradle-${version}-bin.zip`
397-
)
398-
);
399-
}
400-
401385
/**
402386
* Sets specified React Native version.
403387
* @param {string} version
@@ -466,8 +450,6 @@ if (isMain(import.meta.url)) {
466450
: toVersionNumber(version);
467451
if (numVersion >= v(0, 74, 0)) {
468452
disableJetifier();
469-
} else if (numVersion < v(0, 73, 0)) {
470-
setGradleVersion("7.6.3");
471453
}
472454

473455
// `@react-native-webapis/web-storage` is not compatible with codegen 0.71

0 commit comments

Comments
 (0)