在Cordova(11.0.0)项目中,当为Android("cordova-android":"^10.1.2")
平台构建时,我从gradle(7.4.2)得到这个错误:无法使字段私有最终java. lang.Stringjava.io。文件路径可访问:模块java.base不会"打开java.io"到未命名的模块
。
根据这个答案和这个答案,它与Java18有关,这个问题可以通过向gradle.properties添加一些属性来解决。
需要编辑的文件是平台/android/gradle.properties
,建议使用before_build
钩子脚本修改该文件。
我已经这样做了,并确认我的脚本正确地复制了我的gradle.properties版本。然而,随后有东西用文件的默认版本覆盖了它。
如何防止我的文件版本被默认版本覆盖?
要修复jvmargs,有必要修改:node_modules\cordova-android\lib\config\GradleProperties tiesParser. js:
_configureProperties (properties) {
// Iterate though the properties and set only if missing.
Object.keys(properties).forEach(key => {
const value = this.gradleFile.get(key);
if (!value) {
// Handles the case of adding missing defaults or new properties that are missing.
events.emit('verbose', `[Gradle Properties] Appending configuration item: ${key}=${properties[key]}`);
this.gradleFile.set(key, properties[key]);
} else if (value !== properties[key]) {
if (this._defaults[key] && this._defaults[key] !== properties[key]) {
let shouldEmit = true;
if (key === 'org.gradle.jvmargs') {
shouldEmit = this._isJVMMemoryLessThanRecommended(properties[key], this._defaults[key]);
}
if (shouldEmit) {
// Since the value does not match default, we will notify the discrepancy with Cordova's recommended value.
events.emit('info', `[Gradle Properties] Detected Gradle property "${key}" with the value of "${properties[key]}", Cordova's recommended value is "${this._defaults[key]}"`);
}
} else {
// When the current value exists but does not match the new value or does matches the default key value, the new value it set.
events.emit('verbose', `[Gradle Properties] Updating Gradle property "${key}" with the value of "${properties[key]}"`);
}
// We will set the new value in either case.
this.gradleFile.set(key, properties[key]);
}
});
//add this line here (Ben) :
this.gradleFile.set('org.gradle.jvmargs', this.gradleFile.get('org.gradle.jvmargs') + " --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED");
}
gradle 7.5.1版本的cordova编译问题