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,76 @@ 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+ const gradleWrapperProperties = path . join (
60+ sourceDir ,
61+ "gradle" ,
62+ "wrapper" ,
63+ "gradle-wrapper.properties"
64+ ) ;
65+ if ( ! fs . existsSync ( gradleWrapperProperties ) ) {
66+ return ;
67+ }
68+
69+ const tag = tty . WriteStream . prototype . hasColors ( )
70+ ? "\u001B[33m\u001B[1mwarn\u001B[22m\u001B[39m"
71+ : "warn" ;
72+
73+ try {
74+ const props = readTextFile ( gradleWrapperProperties ) ;
75+ const re = / g r a d l e - ( [ . 0 - 9 ] * ?) - .* ?\. z i p / ;
76+ const m = props . match ( re ) ;
77+ if ( ! m ) {
78+ return ;
79+ }
80+
81+ const gradleVersion = ( ( ) => {
82+ const gradleVersion = toVersionNumber ( m [ 1 ] ) ;
83+ const version = toVersionNumber (
84+ getPackageVersion ( "react-native" , sourceDir , fs )
85+ ) ;
86+ if ( version === 0 || version >= v ( 0 , 74 , 0 ) ) {
87+ if ( gradleVersion < v ( 8 , 6 , 0 ) ) {
88+ return "8.6" ;
89+ }
90+ } else if ( version >= v ( 0 , 73 , 0 ) ) {
91+ if ( gradleVersion < v ( 8 , 3 , 0 ) ) {
92+ return "8.3" ;
93+ }
94+ } else if ( version >= v ( 0 , 72 , 0 ) ) {
95+ if ( gradleVersion < v ( 8 , 1 , 1 ) ) {
96+ return "8.1.1" ;
97+ }
98+ } else if ( gradleVersion < v ( 7 , 5 , 1 ) || gradleVersion >= v ( 8 , 0 , 0 ) ) {
99+ return "7.6.4" ;
100+ }
101+ return undefined ;
102+ } ) ( ) ;
103+
104+ if ( gradleVersion ) {
105+ console . warn ( tag , `Setting Gradle version ${ gradleVersion } ` ) ;
106+ fs . writeFileSync (
107+ gradleWrapperProperties ,
108+ props . replace ( re , `gradle-${ gradleVersion } -bin.zip` )
109+ ) ;
110+ }
111+ } catch ( _ ) {
112+ console . warn ( tag , "Failed to determine Gradle version" ) ;
113+ }
114+ }
115+
45116/**
46117 * @param {string } sourceDir
47118 * @returns {string }
@@ -50,7 +121,7 @@ function androidManifestPath(sourceDir) {
50121 return path . relative (
51122 sourceDir ,
52123 path . join (
53- path . dirname ( require . resolve ( "../package.json" ) ) ,
124+ path . dirname ( __dirname ) ,
54125 "android" ,
55126 "app" ,
56127 "src" ,
@@ -109,6 +180,7 @@ function configureProjects({ android, ios, windows }, fs = nodefs) {
109180 path . resolve ( projectRoot , android . sourceDir )
110181 ) ,
111182 } ;
183+ configureGradleWrapper ( android . sourceDir , fs ) ;
112184 }
113185
114186 if ( ios ) {
0 commit comments