【问题标题】:mergeReleaseResources Failed, caused of `not valid resource name` (Version from package.json)mergeReleaseResources 失败,原因是“资源名称无效”(来自 package.json 的版本)
【发布时间】:2020-01-26 13:29:19
【问题描述】:

我在我的 React-Native-App 中要求 package.json 从那里读取版本号以在我的应用程序中显示它:

const pjson = require('../../package.json');

因此,我在 Android 中使用 gradle 捆绑 RELEASE APK 时出错:

任务:app:mergeReleaseResources 失败 /home/Projekte/APP/android/app/build/generated/res/react/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)

有没有办法从合并APK资源的进程中排除这个package.json?

【问题讨论】:

    标签: android react-native gradle package.json


    【解决方案1】:

    解决此问题的最简单方法是更新到react-native 0.61.x。 整个解决方案可以在here找到。

    问题出在哪里,摘自以上链接:

    cli 会将带有 json 扩展名的文件复制到资源目录,在 Android 的 案例,android/app/build/res。

    问题来了。如果项目根目录的 package.json 被复制, 它不会被重命名为 node_modules 中的其他 package.json (例如 原始/node_modules_reactnativecodepush_package.json)。相反,它将 以 package.json 的形式存在于 res 中,这会导致错误:

    android/app/src/main/res/raw/package.json:错误:包不是 有效的资源名称(保留的 Java 关键字)

    如果您无法更新,可以将补丁直接应用到您的 node_modules/metro 模块,从捆绑发布过程中排除 package.json

    补丁: 在文件 node_modules/metro/src/DeltaBundler/Serializers/getAssets.js,第 66 行,替换

    getJsOutput(module).type === "js/module/asset"

    getJsOutput(module).type === "js/module/asset" && path.relative(options.projectRoot, module.path) !== "package.json"

    将以下文件添加到项目的根目录,

    // fix_metro_android_release_bug.js
    // Temporary fix for this issue: https://github.com/facebook/metro/pull/420
    const fs = require('fs');
    
    const fileLocation = './node_modules/metro/src/DeltaBundler/Serializers/getAssets.js';
    const targetText = 'getJsOutput(module).type === "js/module/asset"';
    const replacementText = 'getJsOutput(module).type === "js/module/asset" && path.relative(options.projectRoot, module.path) !== "package.json"';
    
    const fileContent = fs.readFileSync(fileLocation, 'utf8');
    if (fileContent.includes(targetText) && !fileContent.includes(replacementText)) {
        const patchedFileContent = fileContent.replace(targetText, replacementText);
        fs.writeFileSync(fileLocation, patchedFileContent, 'utf8');
    }
    

    并将以下 sn-p 添加到您的 package.json

    "scripts": {
        "postinstall": "babel-node ./fix_metro_android_release_bug.js",
        ...
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多