77 */
88const nodefs = require ( "node:fs" ) ;
99const path = require ( "node:path" ) ;
10+ const tty = require ( "node:tty" ) ;
1011const {
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 = / g r a d l e - ( [ . 0 - 9 ] * ?) - .* ?\. z i p / ;
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 }
@@ -50,7 +117,7 @@ function androidManifestPath(sourceDir) {
50117 return path . relative (
51118 sourceDir ,
52119 path . join (
53- path . dirname ( require . resolve ( "../package.json" ) ) ,
120+ path . dirname ( __dirname ) ,
54121 "android" ,
55122 "app" ,
56123 "src" ,
@@ -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 ) {
0 commit comments