fix: configure gradle java.home (#1795)

This commit is contained in:
エリス
2025-04-02 19:10:56 +09:00
committed by GitHub
parent f697ca7dec
commit 2ffe68ab17

View File

@@ -286,14 +286,49 @@ class ProjectBuilder {
prepEnv (opts) {
const self = this;
const config = this._getCordovaConfig();
const configPropertiesPath = path.join(self.root, 'tools', '.gradle', 'config.properties');
return check_reqs.check_gradle()
.then(function () {
events.emit('verbose', `Using Gradle: ${config.GRADLE_VERSION}`);
return self.installGradleWrapper(config.GRADLE_VERSION);
}).then(async function () {
try {
// Try to create "config.properties" file if missing
const fd = fs.openSync(configPropertiesPath, 'wx+', 0o600);
fs.writeFileSync(fd, '', 'utf8');
fs.closeSync(fd);
} catch {
// File already existed, nothing to do.
}
// Loads "config.properties"for editing
const configProperties = createEditor(configPropertiesPath);
/*
* File is replaced on each build. "before_build" hook scripts can inject
* custom settings before this step runs. This step will still overwrite
* the "java.home" setting. Ensure environment variables are properly
* configured if want to use custom "java.home".
*
* Sets "java.home" using the "CORDOVA_JAVA_HOME" environment variable.
* If unavailable, fallback to "JAVA_HOME".
* If neither is set, "java.home" is removed (if previously set),
* allowing Android Studio to display a warning and auto-configure
* to use its internal (bundled) Java.
*/
const javaHome = process.env.CORDOVA_JAVA_HOME || process.env.JAVA_HOME || false;
if (javaHome) {
configProperties.set('java.home', javaHome);
} else {
configProperties.unset('java.home');
}
// Saves changes
configProperties.save();
}).then(async function () {
await fsp.cp(path.join(self.root, 'tools', 'gradle'), path.join(self.root, 'gradle'), { recursive: true, force: true });
await fsp.cp(path.join(self.root, 'tools', 'gradlew'), path.join(self.root, 'gradlew'), { recursive: true, force: true });
await fsp.cp(path.join(self.root, 'tools', 'gradlew.bat'), path.join(self.root, 'gradlew.bat'), { recursive: true, force: true });
await fsp.cp(path.join(self.root, 'tools', '.gradle'), path.join(self.root, '.gradle'), { recursive: true, force: true });
}).then(function () {
return self.prepBuildFiles();
}).then(() => {