Skip to content

Commit 1c7cc3a

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

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,69 @@ const cliPlatformIOSVersion = (() => {
4242
};
4343
})();
4444

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

114178
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)