解决此问题的最简单方法是更新到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",
...
}