You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native执行npm run android报错:checkDebugAarMetadata任务失败

解决React Native npm run android构建失败问题

问题详情

执行npm run android构建React Native项目时持续失败,模拟器可正常运行但安装失败,具体错误信息如下:

What went wrong:                                                                                                                                    
Execution failed for task ':app:checkDebugAarMetadata'.                                                                                               
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform react-android-0.72.6-debug.aar (com.facebook.react:react-android:0.72.6) to match attributes {artifactType=android-aar-metadata, com.android.build.api.attributes.BuildTypeAttr=debug, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for JetifyTransform: C:\Users\BenTo\.gradle\caches\modules-2\files-2.1\com.facebook.react\react-android\0.72.6\2e2db118c9084b05c1a5d6079148bc983668f372\react-android-0.72.6-debug.aar.

项目根目录build.gradle配置:

buildscript {
ext {
    buildToolsVersion = "33.0.0"
    minSdkVersion = 21
    compileSdkVersion = 33
    targetSdkVersion = 33

    // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
    ndkVersion = "23.1.7779620"
}
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath("com.android.tools.build:gradle")
    classpath('com.facebook.react:react-native-gradle-plugin')
}

执行react doctor检测结果:

Android
 ✓ Adb - Required to verify if the android device is attached correctly
 ✓ JDK - Required to compile Java code
 ✓ Android Studio - Required for building and installing your app on Android
 ✖ Android SDK - Required for building and installing your app on Android
   - Versions found: N/A
   - Version supported: 33.0.0
 ✓ ANDROID_HOME - Environment variable that points to your Android SDK installation

Errors:   1
Warnings: 0

Usage
 › Press f to try to fix issues.
 › Press e to try to fix errors.
 › Press w to try to fix warnings.
 › Press Enter to exit.

按e修复后提示问题已解决并需重启,但重启后错误依旧。

解决方案

1. 手动确认Android SDK安装情况

打开Android Studio,进入Settings > Appearance & Behavior > System Settings > Android SDK:

  • 在SDK Platforms标签下,勾选**Android 13 (Tiramisu)**对应的SDK Platform 33.0.0,点击Apply完成安装。
  • 切换到SDK Tools标签,勾选Android Build Tools 33.0.0,确保安装完成。

2. 清理Gradle缓存

删除本地Gradle缓存目录:C:\Users\BenTo\.gradle\caches,删除后重新执行npm run android。

3. 明确指定Gradle和AGP版本

React Native 0.72.6需要兼容的AGP版本为7.3.1,Gradle版本为7.5:

  • 修改项目根目录build.gradle中的依赖:
    dependencies {
        classpath("com.android.tools.build:gradle:7.3.1")
        classpath('com.facebook.react:react-native-gradle-plugin')
    }
    
  • 打开android/gradle/wrapper/gradle-wrapper.properties,修改distributionUrl:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
    

4. 同步Gradle并重新构建

打开Android Studio,点击File > Sync Project with Gradle Files,等待同步完成后,再执行npm run android。

5. 验证环境变量正确性

确认系统环境变量:

  • ANDROID_HOME指向Android SDK的正确路径(比如C:\Users\BenTo\AppData\Local\Android\Sdk)。
  • PATH中添加%ANDROID_HOME%\platform-tools和%ANDROID_HOME%\tools,确保命令行可识别adb等工具。

内容的提问来源于stack exchange,提问作者Ben Tongue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:49:55