【问题标题】:How to prevent gradle.properties from being overwritten in Cordova Android?如何防止 gradle.properties 在 Cordova Android 中被覆盖?
【发布时间】:2022-10-03 00:20:55
【问题描述】:

在 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

根据this answerthis answer,它与Java 18 有关,可以通过向gradle.properties 添加一些属性来解决该问题。

需要编辑的文件是platforms/android/gradle.properties,使用before_build 挂钩脚本修改此文件是recommended

我已经这样做了,并确认我的脚本正确地复制了我的 gradle.properties 版本。然而,随后有些东西会用文件的默认版本覆盖它。

如何防止我的文件版本被默认版本覆盖?

  • 对于因 Java 错误而发现此问题的任何人。解决这个问题只会揭示更多与 Java 和 Gradle 版本相关的问题,而我可以解决这个问题的唯一方法是恢复到以前的 Java 版本。我不得不回到 Java 15。

标签: cordova cordova-android


【解决方案1】:

要修复 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

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2021-02-08
    • 2020-07-23
    • 2021-04-20
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多