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

运行React Native应用遇构建错误:无法解析gradle:7.2.1

React Native构建错误:无法解析com.android.tools.build:gradle:7.2.1

问题现象

构建React Native应用时触发错误:

配置根项目'testttt'时发生错误,无法确定null的依赖项,无法解析':classpath'配置的所有任务依赖,无法解析com.android.tools.build:gradle:7.2.1

对应的根build.gradle配置如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "31.0.0"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 31

        if (System.properties['os.arch'] == "aarch64") {
            // For M1 Users we need to use the NDK 24 which added support for aarch64
            ndkVersion = "24.0.8215888"
        } else {
            // Otherwise we default to the side-by-side NDK version from AGP.
            ndkVersion = "21.4.7075529"
        }
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.2.1")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("de.undercouch:gradle-download-task:5.0.1")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        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")
        }
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
        google()
        maven { url 'https://www.jitpack.io' }
    }
}

解决方法

  • 校验Gradle与AGP版本兼容性
    AGP 7.2.1要求Gradle版本至少为7.3.3。打开项目根目录下的gradle/wrapper/gradle-wrapper.properties,确保distributionUrl指向对应版本:

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
    
  • 优化仓库配置(适配网络受限环境)
    若无法正常访问官方仓库,在buildscript和allprojects的repositories中添加国内镜像仓库,放在官方仓库之前:

    buildscript {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/google' }
            maven { url 'https://maven.aliyun.com/repository/public' }
            google()
            mavenCentral()
        }
        // 其他原有配置
    }
    
    allprojects {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/google' }
            maven { url 'https://maven.aliyun.com/repository/public' }
            // 其他原有仓库配置
        }
    }
    
  • 清理缓存并刷新依赖
    执行命令清理Gradle缓存并强制重新拉取依赖:

    • Windows:gradlew clean build --refresh-dependencies
    • macOS/Linux:./gradlew clean build --refresh-dependencies
      也可手动删除项目根目录下的.gradle/caches文件夹,以及用户目录下的~/.gradle/caches文件夹。
  • 检查代理设置
    确认本地代理未拦截Gradle的仓库请求,若使用代理需在gradle.properties中正确配置代理信息,或暂时关闭代理尝试构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:01:08