如何保留compileSdkVersion33解决React Native依赖编译错误
React Native编译API33下androidx.core依赖冲突解决
问题背景
使用npm run android构建React Native应用时,出现构建失败,同时收到Gradle废弃特性提示。核心错误为:依赖androidx.core:core-ktx:1.12.0和androidx.core:core:1.12.0要求项目编译API等级至少34,但当前项目compileSdkVersion为33。已安装react-native-pdf和react-native-blob-util包,希望保持compileSdkVersion为33,无需升级即可解决该问题。
错误日志
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/8.0.1/userguide/command_line_interface.html#sec:command_line_warnings 30 actionable tasks: 25 executed, 5 up-to-date info 💡 Tip: Make sure that you have set up your development environment correctly, by running react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction > 2 issues were found when checking AAR metadata: 1. Dependency 'androidx.core:core-ktx:1.12.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 7.4.2 is 33. Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on). 2. Dependency 'androidx.core:core:1.12.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on). * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 14s
解决方案(无需升级compileSdkVersion)
1. 强制降级androidx.core依赖版本
在项目的android/app/build.gradle文件的android代码块内添加依赖强制配置,指定使用兼容API33的androidx.core版本(1.11.0及以下版本均支持compileSdk33):
android { // 已有配置... configurations.all { resolutionStrategy { force 'androidx.core:core-ktx:1.11.0' force 'androidx.core:core:1.11.0' } } }
添加后重新执行npm run android即可。
2. 排查第三方库依赖来源
运行以下命令查看项目完整依赖树,确认是react-native-pdf还是react-native-blob-util引入了高版本的androidx.core:
cd android && ./gradlew app:dependencies
如果对应第三方库存在兼容compileSdk33的更新版本,可以尝试升级该库来解决依赖冲突。
3. 禁用AAR元数据检查(不推荐)
此方法会跳过依赖元数据校验,可能引入潜在兼容性问题,仅作为临时应急方案:
在android/app/build.gradle的android代码块中添加:
android { // 已有配置... lintOptions { abortOnError false checkReleaseBuilds false } }
内容的提问来源于stack exchange,提问作者Chanaka Naveen
相关产品推荐
相关产品推荐

