使用gradlew assembleRelease打包React Native APK失败求助
无法生成Android Release APK:资源链接失败(react-native-fetch-blob相关)
问题描述
过去两周尝试生成Release APK一直失败,执行gradlew bundleRelease生成bundle完全正常,但运行gradlew assembleRelease时报错,错误指向:react-native-fetch-blob:verifyReleaseResources任务失败,出现大量Android资源找不到的错误。
相关配置
package.json
{ "name": "edesign", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest", "build": "npx 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/", "lint": "eslint ." }, "dependencies": { "@react-native-community/cameraroll": "^4.1.2", "@react-navigation/drawer": "^6.4.1", "@react-navigation/native": "^6.0.10", "@react-navigation/native-stack": "^6.6.1", "react": "17.0.2", "react-devtools": "^4.24.4", "react-native": "0.67.3", "react-native-admob-next": "^1.2.0", "react-native-device-info": "^9.0.2", "react-native-fetch-blob": "^0.10.8", "react-native-fs": "^2.20.0", "react-native-gesture-handler": "^2.4.1", "react-native-image-crop-picker": "^0.37.3", "react-native-push-notification": "^8.1.1", "react-native-reanimated": "^2.8.0", "react-native-safe-area-context": "3.4.1", "react-native-screens": "^3.13.1", "react-native-share": "^7.4.0", "react-native-vector-icons": "^9.1.0", "react-native-version-check": "^3.4.3", "react-native-view-shot": "^3.1.2" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/runtime": "^7.12.5", "@react-native-async-storage/async-storage": "^1.17.3", "@react-native-community/eslint-config": "^2.0.0", "@react-native-community/slider": "^4.2.2", "@react-native-firebase/app": "^14.9.1", "@react-native-firebase/firestore": "^14.9.1", "@react-native-firebase/messaging": "^14.9.1", "babel-jest": "^26.6.3", "eslint": "7.14.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "^0.66.2", "react-native-image-picker": "^4.8.1", "react-test-renderer": "17.0.2" }, "jest": { "preset": "react-native" } }
android/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "30.0.2" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 30 ndkVersion = "21.4.7075529" googlePlayServicesVersion = "+" // default: "+" firebaseVersion = "+" // default: "+" androidXCore = "1.6.0" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:4.2.2") classpath 'com.google.gms:google-services:4.3.12' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenCentral() mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") } maven { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } google() maven { url 'https://www.jitpack.io' } } }
错误信息
* What went wrong: Execution failed for task ':react-native-fetch-blob:verifyReleaseResources'. > A failure occurred while executing com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action > Android resource linking failed ERROR:C:\Users\lylde\.gradle\caches\transforms-3\aee9ee05966214edc1879619afacf3e1\transformed\appcompat-1.0.2\res\values-v26\values-v26.xml:5:5-8:13: AAPT: error: resource android:attr/colorError not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\aee9ee05966214edc1879619afacf3e1\transformed\appcompat-1.0.2\res\values-v26\values-v26.xml:9:5-12:13: AAPT: error: resource android:attr/colorError not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\aee9ee05966214edc1879619afacf3e1\transformed\appcompat-1.0.2\res\values-v26\values-v26.xml:13:5-16:13: AAPT: error: style attribute 'android:attr/keyboardNavigationCluster' not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\aee9ee05966214edc1879619afacf3e1\transformed\appcompat-1.0.2\res\values-v28\values-v28.xml:5:5-8:13: AAPT: error: resource android:attr/dialogCornerRadius not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\aee9ee05966214edc1879619afacf3e1\transformed\appcompat-1.0.2\res\values-v28\values-v28.xml:9:5-12:13: AAPT: error: resource android:attr/dialogCornerRadius not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontStyle not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/font not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontWeight not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontVariationSettings not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/ttcIndex not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:174:5-210:25: AAPT: error: resource android:attr/startX not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:174:5-210:25: AAPT: error: resource android:attr/startY not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:174:5-210:25: AAPT: error: resource android:attr/endX not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:174:5-210:25: AAPT: error: resource android:attr/endY not found. ERROR:C:\Users\lylde\.gradle\caches\transforms-3\6659f15da30f261a037980a726c1722d\transformed\core-1.1.0\res\values\values.xml:211:5-217:25: AAPT: error: resource android:attr/offset not found.
解决方案
方案1:替换为维护中的分支库
react-native-fetch-blob已停止维护,无法适配高版本Android SDK。建议替换为社区维护的rn-fetch-blob:
- 卸载原库:
npm uninstall react-native-fetch-blob # 或 yarn remove react-native-fetch-blob
- 安装替代库:
npm install rn-fetch-blob # 或 yarn add rn-fetch-blob
- 替换代码中的导入:将
import RNFetchBlob from 'react-native-fetch-blob'改为import RNFetchBlob from 'rn-fetch-blob'
方案2:强制统一AndroidX依赖版本
如果不想替换库,可在android/build.gradle中添加配置,强制react-native-fetch-blob使用与项目一致的AndroidX版本:
在allprojects块内添加:
subprojects { if (project.name.contains("react-native-fetch-blob")) { buildscript { ext { compileSdkVersion = rootProject.ext.compileSdkVersion buildToolsVersion = rootProject.ext.buildToolsVersion targetSdkVersion = rootProject.ext.targetSdkVersion } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core:1.9.0' } } }
方案3:清理缓存并重新构建
完成上述任一方案后,执行以下命令清理缓存:
cd android ./gradlew clean rm -rf ~/.gradle/caches cd .. npm install # 或 yarn install
然后重新尝试生成Release APK:
cd android ./gradlew assembleRelease
内容的提问来源于stack exchange,提问作者Sumanl46
相关产品推荐
相关产品推荐

