@@ -286,14 +286,49 @@ class ProjectBuilder {
286
286
prepEnv ( opts ) {
287
287
const self = this ;
288
288
const config = this . _getCordovaConfig ( ) ;
289
+ const configPropertiesPath = path . join ( self . root , 'tools' , '.gradle' , 'config.properties' ) ;
290
+
289
291
return check_reqs . check_gradle ( )
290
292
. then ( function ( ) {
291
293
events . emit ( 'verbose' , `Using Gradle: ${ config . GRADLE_VERSION } ` ) ;
292
294
return self . installGradleWrapper ( config . GRADLE_VERSION ) ;
295
+ } ) . then ( async function ( ) {
296
+ try {
297
+ // Try to create "config.properties" file if missing
298
+ const fd = fs . openSync ( configPropertiesPath , 'wx+' , 0o600 ) ;
299
+ fs . writeFileSync ( fd , '' , 'utf8' ) ;
300
+ fs . closeSync ( fd ) ;
301
+ } catch {
302
+ // File already existed, nothing to do.
303
+ }
304
+
305
+ // Loads "config.properties"for editing
306
+ const configProperties = createEditor ( configPropertiesPath ) ;
307
+ /*
308
+ * File is replaced on each build. "before_build" hook scripts can inject
309
+ * custom settings before this step runs. This step will still overwrite
310
+ * the "java.home" setting. Ensure environment variables are properly
311
+ * configured if want to use custom "java.home".
312
+ *
313
+ * Sets "java.home" using the "CORDOVA_JAVA_HOME" environment variable.
314
+ * If unavailable, fallback to "JAVA_HOME".
315
+ * If neither is set, "java.home" is removed (if previously set),
316
+ * allowing Android Studio to display a warning and auto-configure
317
+ * to use its internal (bundled) Java.
318
+ */
319
+ const javaHome = process . env . CORDOVA_JAVA_HOME || process . env . JAVA_HOME || false ;
320
+ if ( javaHome ) {
321
+ configProperties . set ( 'java.home' , javaHome ) ;
322
+ } else {
323
+ configProperties . unset ( 'java.home' ) ;
324
+ }
325
+ // Saves changes
326
+ configProperties . save ( ) ;
293
327
} ) . then ( async function ( ) {
294
328
await fsp . cp ( path . join ( self . root , 'tools' , 'gradle' ) , path . join ( self . root , 'gradle' ) , { recursive : true , force : true } ) ;
295
329
await fsp . cp ( path . join ( self . root , 'tools' , 'gradlew' ) , path . join ( self . root , 'gradlew' ) , { recursive : true , force : true } ) ;
296
330
await fsp . cp ( path . join ( self . root , 'tools' , 'gradlew.bat' ) , path . join ( self . root , 'gradlew.bat' ) , { recursive : true , force : true } ) ;
331
+ await fsp . cp ( path . join ( self . root , 'tools' , '.gradle' ) , path . join ( self . root , '.gradle' ) , { recursive : true , force : true } ) ;
297
332
} ) . then ( function ( ) {
298
333
return self . prepBuildFiles ( ) ;
299
334
} ) . then ( ( ) => {
0 commit comments