React Native错误:在Android发布APK中,一些屏幕上出现了重复的资源和资产,而其他屏幕上却没有出现。
React Native错误:在Android发布APK中,一些屏幕上出现了重复的资源和资产,而其他屏幕上却没有出现。
我正在使用Gradle版本3.2.0
和compileSdkVersion
28在安卓上构建我的React Native应用程序的发布APK。我运行以下命令:
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ cd android && ./gradlew clean && ./gradlew assembleRelease && ./gradlew installRelease
我的文件夹结构如下:
我的package.json
是:
{
"name": "App",
"version": "2.0.0",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"axios": "^0.18.0",
"native-base": "^2.8.0",
"prop-types": "^15.6.2",
"qs": "^6.5.2",
"react": "^16.5.2",
"react-native": "0.57.0",
"react-native-animatable": "^1.3.0",
"react-native-animated-icons": "^1.0.5",
"react-native-appsee": "^2.4.14",
"react-native-blur": "^3.2.2",
"react-native-cast-chrome": "1.0.0",
"react-native-cast-ui": "1.0.0",
"react-native-device-info": "^0.22.3",
"react-native-dialog": "^5.1.0",
"react-native-elements": "^0.19.1",
"react-native-email": "^1.0.1",
"react-native-fast-image": "^5.0.3",
"react-native-fbsdk": "^0.8.0",
"react-native-flurry-analytics": "^3.0.2",
"react-native-gridview": "^0.1.2",
"react-native-iap": "^2.2.1",
"react-native-iphone-x-helper": "^1.0.3",
"react-native-keyboard-aware-scroll-view": "^0.7.0",
"react-native-kochava-tracker": "^1.0.0",
"react-native-linear-gradient": "^2.4.0",
"react-native-material-dropdown": "^0.11.1",
"react-native-modal": "^6.5.0",
"react-native-orientation": "^3.1.3",
"react-native-photo-upload": "^1.3.0",
"react-native-scrollable-tab-view": "^0.8.0",
"react-native-snap-carousel": "^3.7.4",
"react-native-tab-view": "^1.2.0",
"react-native-underline-tabbar": "^1.3.6",
"react-native-vector-icons": "^5.0.0",
"react-native-video": "^3.2.1",
"react-native-video-controls": "^2.2.3",
"react-navigation": "^2.11.2",
"react-navigation-backhandler": "^1.1.1",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"@babel/plugin-external-helpers": "^7.0.0",
"ajv": "^6.5.2",
"babel-eslint": "^9.0.0",
"babel-jest": "23.4.2",
"babel-plugin-transform-remove-console": "^6.9.4",
"eslint": "^5.3.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.0",
"jest": "23.4.2",
"metro-react-native-babel-preset": "^0.45.4",
"react-test-renderer": "16.4.1",
"schedule": "^0.4.0"
},
"jest": {
"preset": "react-native"
}
}
在运行命令时,我遇到Task :app:mergeReleaseResources FAILED
的错误,所有我的资源都出现Error: Duplicate resources
。当我从可绘制文件夹中删除资源时,应用程序可以构建。
但是,当运行发布APK时,有些图片根本不显示,而其他图片显示正常。此外,同一张图片在某些地方显示,而在其他地方则不显示。在从JS服务器运行调试应用程序时没有出现这些问题。这个问题在iOS上没有发生。
我该如何解决这个问题?
React Native在Android发布APK中出现重复资源的错误,可能是因为在构建上传过程中出现了问题。这篇文章提供了解决这个问题的方法。
问题的原因可能是在构建上传过程中,某些屏幕中的资源被重复引入,而其他屏幕中的资源却没有被引入。
解决这个问题的方法是在项目的`android/app/build.gradle`文件中添加以下代码:
android { ... packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/INDEX.LIST' } }
这样做可以排除掉一些重复的资源文件,从而解决重复资源的问题。
更多关于解决重复资源问题的详细信息,请参考原文链接:https://medium.com//handling-duplicate-resource-issue-in-android-released-builds-in-react-native-f63c67176bce
React Native版本升级到0.57后,根据其更新日志,要求将Android工具更新至较新的配置要求(SDK 27、gradle 4.4和support library 27)。然而,使用Android插件3.2构建由于gradle脚本的问题无法正常工作,所以暂时请继续使用Android Studio 3.1。
为了解决这个问题,尝试按照React Native的更新日志进行设置,使用com.android.tools.build:gradle:3.1.4
应该可以解决。
如果仍然存在问题,并且想要使用bundle,可以尝试移除android build文件夹,并仅运行./gradlew assembleRelease
而不进行任何bundle操作。我也在使用RN 0.57版本,并且在没有进行bundle操作的情况下生成apk运行正常。
如果仍然有问题并且想要使用bundle,可以参考React Native Android Duplicate file error when generating apk,其中的答案应该可以解决问题。
关于0.57版本的Android更新的更多信息,请参考https://github.com/react-native-community/react-native-releases/blob/master/CHANGELOG.md。
如果以上方法都尝试过仍然无效,可以将您的package.json文件中的依赖项添加到问题描述中,至少包括RN版本和babel相关的配置。
以下是我的项目结构:
(这里省略了具体的项目结构内容)