我在React-Native-App中需要package.json来读出其中的某些字段:
const pjson = require('../../package.json');
因此,在使用gradle捆绑Android中的RELEASE APK时出现错误:
任务:app:mergeReleaseResources失败/home/Projekte/APP/android/app/build/generated/res/react/release/raw/package.json:错误:程序包不是有效的资源名称(保留的Java关键字)
是否有办法从合并了APK资源的流程中排除此package.json?
解决此问题的最简单方法是将其更新为react-native 0.61.x
。可以找到整个解决方案here。
是什么原因导致的,取自上面的链接:
cli将带有json扩展名的文件复制到资源目录,在Android的案例,android / app / build / res。
这里有问题。如果复制了项目根目录下的package.json,它不会在node_modules中重命名为其他package.json(例如raw / 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"
OR
将以下文件添加到项目的根目录,
// 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');
}
并将以下代码段添加到您的package.json
"scripts": {
"postinstall": "babel-node ./fix_metro_android_release_bug.js",
...
}