简体   繁体   English

如何防止gradle.properties在Cordova Android中被覆盖?

[英]How to prevent gradle.properties from being overwritten in Cordova Android?

In a Cordova (11.0.0) project, when building for the Android ("cordova-android": "^10.1.2") platform, I'm getting this error from gradle(7.4.2): Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module .在 Cordova (11.0.0) 项目中,在为 Android ("cordova-android": "^10.1.2")平台构建时,我从 gradle(7.4.2) 收到此错误: Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module

According to this answer and this answer it's related to Java 18 and the issue can be fixed by adding some properties to gradle.properties.根据这个答案这个答案,它与 Java 18 相关,可以通过向 gradle.properties 添加一些属性来解决这个问题。

The file requiring editing is platforms/android/gradle.properties and it is recommended that this file is modified using the before_build hook script.需要编辑的文件为platforms/android/gradle.properties建议使用before_build hook脚本修改该文件。

I have done that and confirmed that my script correctly copies my version of gradle.properties.我已经这样做并确认我的脚本正确复制了我的 gradle.properties 版本。 However something is subsequently overwriting that with the default version of the file.然而,随后有一些东西用文件的默认版本覆盖了它。

How do I prevent my version of the file being overwritten by the default version?如何防止我的文件版本被默认版本覆盖?

to fix jvmargs it is necessary to modify: node_modules\cordova-android\lib\config\GradlePropertiesParser.js with:要修复 jvmargs,需要修改:node_modules\cordova-android\lib\config\GradlePropertiesParser.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");
}

Cordova compile problem with gradle version 7.5.1 Cordova 编译问题 gradle 版本 7.5.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM