React Native升级依赖后Android编译SDK版本不兼容问题求助
React Native项目Android构建错误:minCompileSdk(31)大于compileSdkVersion(android-30)
项目环境
- React Native版本:
0.63.4 - React版本:
17.0.1 - 已升级除
react和react-native外的所有依赖,完整依赖清单:
"dependencies": { "@monterosa/react-native-parallax-scroll": "^1.8.0", "@notifee/react-native": "^5.7.0", "@react-native-async-storage/async-storage": "^1.17.10", "@react-native-community/progress-bar-android": "^1.0.4", "@react-native-community/progress-view": "^1.3.2", "@react-native-firebase/app": "^15.4.0", "@react-native-firebase/messaging": "^15.4.0", "frisbee": "^3.1.4", "immutability-helper": "^3.1.1", "lodash": "^4.17.21", "native-base": "^3.4.15", "react": "17.0.1", "react-icons": "^4.4.0", "react-native": "0.63.4", "react-native-a": "^1.0.1", "react-native-app-intro-slider": "^4.0.4", "react-native-facebook-login": "^1.6.2", "react-native-fast-image": "^8.6.1", "react-native-firebase-push-notifications": "^2.0.2", "react-native-fs": "^2.20.0", "react-native-gesture-handler": "^2.6.0", "react-native-image-crop-picker": "^0.38.0", "react-native-in-app-utils": "^6.1.0", "react-native-loading-spinner-overlay": "^3.0.1", "react-native-localization": "^2.3.1", "react-native-material-dialog": "^0.7.7", "react-native-modal": "^13.0.1", "react-native-music-control": "^1.4.1", "react-native-music-player-service": "^0.1.4-beta", "react-native-popup-menu": "^0.16.1", "react-native-push-notification": "^8.1.1", "react-native-restart": "^0.0.24", "react-native-screens": "^3.17.0", "react-native-snap-carousel": "^3.9.1", "react-native-sound": "^0.11.2", "react-native-splash-screen": "^3.3.0", "react-native-super-grid": "^4.5.1", "react-native-swift": "^1.2.3", "react-native-swiper": "^1.6.0", "react-native-track-player": "^3.1.0", "react-native-typing-text": "^0.0.3", "react-native-vector-icons": "^9.2.0", "react-native-webview": "^11.23.1", "react-navigation": "^4.4.4", "react-redux": "^8.0.2", "react-timeago": "^7.1.0", "realm": "^10.21.1", "redux": "^4.2.0", "redux-saga": "^1.2.1", "rn-sliding-up-panel": "^2.4.6", "toggle-switch-react-native": "^3.3.0" }, "devDependencies": { "@babel/core": "^7.4.5", "@babel/runtime": "^7.4.5", "babel-jest": "^25.1.0", "jest": "^25.1.0", "metro-react-native-babel-preset": "^0.59.0", "schedule": "^0.5.0" }
问题描述
运行Android开发构建命令时,抛出以下错误:
The minCompileSdk (31) specified in a dependency's AAR metadata is greater than this module's compileSdkVersion (android-30)
已尝试的解决方案
- 强制Gradle将
minSdk、targetSdk、compileSdk设置为31 - 在项目级
build.gradle中添加依赖强制配置:
configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' } }
相关配置代码
App级build.gradle
apply plugin: "com.android.application" apply plugin: 'com.google.gms.google-services' import com.android.build.OutputFile project.ext.react = [ entryFile: "index.js", enableHermes: false, ] apply from: "../../node_modules/react-native/react.gradle" def enableSeparateBuildPerCPUArchitecture = false def enableProguardInReleaseBuilds = false def jscFlavor = 'org.webkit:android-jsc:+' def enableHermes = project.ext.react.get("enableHermes", false); android { compileSdkVersion rootProject.ext.compileSdkVersion ndkVersion "22.0.7026061" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } lintOptions { checkReleaseBuilds false abortOnError false } defaultConfig { applicationId "com.loveworld.worship" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion missingDimensionStrategy "RNN.reactNativeVersion", "reactNative56" versionCode 31 versionName "3.9" vectorDrawables.useSupportLibrary = true multiDexEnabled true ndk { abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64' } } signingConfigs { debug { storeFile file('debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } release { if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) { storeFile file(MYAPP_RELEASE_STORE_FILE) storePassword MYAPP_RELEASE_STORE_PASSWORD keyAlias MYAPP_RELEASE_KEY_ALIAS keyPassword MYAPP_RELEASE_KEY_PASSWORD } } } splits { abi { reset() enable enableSeparateBuildPerCPUArchitecture universalApk false // If true, also generate a universal APK include "armeabi-v7a", "x86" } } buildTypes { debug { signingConfig signingConfigs.debug } release { minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" signingConfig signingConfigs.release } mySpecialRelease { matchingFallbacks = ['release', 'debug'] } } applicationVariants.all { variant -> variant.outputs.each { output -> def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4] def abi = output.getFilter(OutputFile.ABI) if (abi != null) { output.versionCodeOverride = versionCodes.get(abi) * 1048576 + defaultConfig.versionCode } } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation project(':@react-native-async-storage_async-storage') implementation project(':react-native-facebook-login') implementation project(':react-native-firebase-push-notifications') implementation'com.facebook.soloader:soloader:0.9.0+' implementation project(':react-native-screens') implementation project(':react-native-restart') implementation project(':react-native-track-player') implementation project(':realm') implementation project(':react-native-fs') implementation project(':react-native-music-control') implementation project(':react-native-gesture-handler') implementation project(':react-native-localization') implementation project(':react-native-splash-screen') implementation project(':react-native-image-crop-picker') implementation project(':react-native-sound') implementation project(':react-native-fast-image') implementation 'com.android.support:multidex:1.0.1' implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" implementation "com.facebook.react:react-native:+" if (enableHermes) { def hermesPath = "../../node_modules/hermes-engine/android/"; debugImplementation files(hermesPath + "hermes-debug.aar") releaseImplementation files(hermesPath + "hermes-release.aar") } else { implementation jscFlavor } } task copyDownloadableDepsToLibs(type: Copy) { from configurations.compile into 'libs' }
可行解决方案
1. 统一项目级SDK版本配置
修改项目根目录android/build.gradle中的ext块,确保所有SDK版本指向31:
buildscript { ext { buildToolsVersion = "31.0.0" minSdkVersion = 21 // 根据业务需求调整,不低于依赖要求的最低值即可 compileSdkVersion = 31 targetSdkVersion = 31 supportLibVersion = "31.0.0" } // ...其他buildscript配置 }
2. 定位高要求SDK的依赖
执行以下命令找出哪个依赖要求minCompileSdk=31:
./gradlew app:dependencies --configuration debugCompileClasspath | grep -A 5 -B 5 "minCompileSdk"
常见嫌疑依赖:@notifee/react-native、@react-native-firebase/messaging、react-native-push-notification的最新版本可能强制要求SDK31。如果是这些依赖,可选择:
- 降级到适配RN0.63.4的旧版本
- 确保Gradle环境完全兼容SDK31(后续步骤)
3. 替换Android Support库为AndroidX
RN0.60+已默认使用AndroidX,需替换app级build.gradle中的旧支持库:
// 替换 implementation 'com.android.support:multidex:1.0.1' implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" // 为 implementation 'androidx.multidex:multidex:2.0.1' implementation "androidx.appcompat:appcompat:1.4.1"
4. 更新Gradle插件和Gradle版本
RN0.63.4适配的Gradle版本为6.7.1,插件版本为4.2.2:
- 修改项目级
build.gradle:
buildscript { dependencies { classpath("com.android.tools.build:gradle:4.2.2") // ...其他classpath依赖 } }
- 修改
android/gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
5. 清理缓存后重新构建
./gradlew clean rm -rf node_modules rm package-lock.json npm install ./gradlew assembleDebug
内容的提问来源于stack exchange,提问作者Siddhrajsinh Gohil
相关产品推荐
相关产品推荐

