在macOS开发平台,本章节安装了新的依赖后,重新运行react-native run-android后报错

来源:5-12 本章总结

movingwest

2025-12-10

控制台输出的完整报错信息如下:
$ react-native run-android
info A dev server is already running for this project on port 8081.
info Launching emulator…
info Successfully launched emulator.
info Installing the app…
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

Task :app:checkDebugAarMetadata FAILED

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.

For more on this, please refer to https://docs.gradle.org/8.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
22 actionable tasks: 3 executed, 19 up-to-date

info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx 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
4 issues were found when checking AAR metadata:

   1.  Dependency 'androidx.core:core-ktx:1.16.0' requires libraries and applications that
       depend on it to compile against version 35 or later of the
       Android APIs.
 
       :app is currently compiled against android-34.
 
       Also, the maximum recommended compile SDK version for Android Gradle
       plugin 8.1.1 is 34.
 
       Recommended action: Update this project's version of the Android Gradle
       plugin to one that supports 35, then update this project to use
       compileSdk of at least 35.
 
       Note that updating a library or application's compileSdk (which
       allows newer APIs to be used) can be done separately from updating
       targetSdk (which opts the app in to new runtime behavior) and
       minSdk (which determines which devices the app can be installed
       on).
 
   2.  Dependency 'androidx.core:core-ktx:1.16.0' requires Android Gradle plugin 8.6.0 or higher.
 
       This build currently uses Android Gradle plugin 8.1.1.
 
   3.  Dependency 'androidx.core:core:1.16.0' requires libraries and applications that
       depend on it to compile against version 35 or later of the
       Android APIs.
 
       :app is currently compiled against android-34.
 
       Also, the maximum recommended compile SDK version for Android Gradle
       plugin 8.1.1 is 34.
 
       Recommended action: Update this project's version of the Android Gradle
       plugin to one that supports 35, then update this project to use
       compileSdk of at least 35.
 
       Note that updating a library or application's compileSdk (which
       allows newer APIs to be used) can be done separately from updating
       targetSdk (which opts the app in to new runtime behavior) and
       minSdk (which determines which devices the app can be installed
       on).
 
   4.  Dependency 'androidx.core:core:1.16.0' requires Android Gradle plugin 8.6.0 or higher.
 
       This build currently uses Android Gradle plugin 8.1.1.
  • 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 49s
error Failed to install the app. Command failed with exit code 1: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 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 > 4 issues were found when checking AAR metadata: 1. Dependency ‘androidx.core:core-ktx:1.16.0’ requires libraries and applications that depend on it to compile against version 35 or later of the Android APIs. :app is currently compiled against android-34. Also, the maximum recommended compile SDK version for Android Gradle plugin 8.1.1 is 34. Recommended action: Update this project’s version of the Android Gradle plugin to one that supports 35, then update this project to use compileSdk of at least 35. Note that updating a library or application’s compileSdk (which allows newer APIs to be used) can be done separately from updating targetSdk (which opts the app in to new runtime behavior) and minSdk (which determines which devices the app can be installed on). 2. Dependency ‘androidx.core:core-ktx:1.16.0’ requires Android Gradle plugin 8.6.0 or higher. This build currently uses Android Gradle plugin 8.1.1. 3. Dependency ‘androidx.core:core:1.16.0’ requires libraries and applications that depend on it to compile against version 35 or later of the Android APIs. :app is currently compiled against android-34. Also, the maximum recommended compile SDK version for Android Gradle plugin 8.1.1 is 34. Recommended action: Update this project’s version of the Android Gradle plugin to one that supports 35, then update this project to use compileSdk of at least 35. Note that updating a library or application’s compileSdk (which allows newer APIs to be used) can be done separately from updating targetSdk (which opts the app in to new runtime behavior) and minSdk (which determines which devices the app can be installed on). 4. Dependency ‘androidx.core:core:1.16.0’ requires Android Gradle plugin 8.6.0 or higher. This build currently uses Android Gradle plugin 8.1.1. * 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 49s.
info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

写回答

1回答

CrazyCodeBoy

2025-12-26


这个新版本强制要求:
* Android SDK (compileSdk) 必须是 35。
* Android Gradle Plugin (AGP) 必须是 8.6.0 或更高。
而你目前的配置是 compileSdk = 34 且 AGP = 8.1.1,所以导致了冲突。
以下是两种解决方案,建议优先尝试方案一:
方案一:升级项目配置(推荐)
这是最根本的解决办法,让你的项目支持最新的 Android 规范。
* 修改 android/build.gradle:
   找到 buildscript 部分,将 Android Gradle Plugin 版本升级。
   buildscript {
    dependencies {
        // 将 8.1.1 更改为 8.6.0 或更高
        classpath("com.android.tools.build:gradle:8.6.0")
    }
}

* 修改 android/app/build.gradle 或 android/build.gradle 中的 SDK 版本:
   找到 compileSdkVersion (或 compileSdk),将其改为 35。
   android {
    compileSdk = 35  // 或者 compileSdkVersion 35
    // ... 其他配置
}

* 重新运行:
   在终端执行 cd android && ./gradlew clean,然后重新运行 npx react-native run-android。
方案二:强制降级依赖版本(快速修复)
如果你不想升级整个项目的 SDK(因为升级 SDK 可能导致其他老旧库报错),你可以强制让 Gradle 使用低版本的 androidx.core。
在 android/build.gradle 的末尾添加以下代码:
subprojects {
    configurations.all {
        resolutionStrategy {
            // 强制将 androidx.core 锁定在 1.13.0 或 1.15.0(这些版本通常支持 SDK 34)
            force 'androidx.core:core:1.15.0'
            force 'androidx.core:core-ktx:1.15.0'
        }
    }
}

添加后,同样执行 cd android && ./gradlew clean 并重新编译。
常见问题排查
* Java 版本问题: 升级 AGP 到 8.6.0 可能需要 JDK 17。请确保你的环境变量中 JAVA_HOME 指向的是 JDK 17。
* 网络问题: 升级 Gradle 插件时需要下载新包,如果下载缓慢,请确保开启了代理或者使用了国内镜像源。
建议: 既然报错已经明确提示你需要 compileSdk 35,采用方案一是保持项目生命力的最佳做法。
如果你不确定如何修改文件,可以告诉我你目前的 React Native 版本吗?我可以提供更精确的代码位置。
1
0

RN入门到进阶,打造高质量上线App

解锁React Native开发应用新姿势,React Native新版本热门技术

3197 学习 · 3260 问题

查看课程