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

使用React Native CLI创建新项目时gradle:8.1.1依赖解析失败

解决React Native CLI创建项目时的Gradle依赖异常

问题详情

创建React Native新项目时出现以下异常:

Could not determine the dependencies of null.
Could not resolve all task dependencies for configuration ':classpath'.
Could not resolve com.android.tools.build:gradle:8.1.1.

相关配置文件如下:

根目录build.gradle

buildscript {
    ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "23.1.7779620"
    }
    repositories {
        google()
        mavenCentral()
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_X
        targetCompatibility JavaVersion.VERSION_X
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.1.1")
        classpath("com.facebook.react:react-native-gradle-plugin")
    }
}

app目录build.gradle

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"

react {
    
}

def enableProguardInReleaseBuilds = false


def jscFlavor = 'org.webkit:android-jsc:+'

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    namespace "com.firstrnapp"
    defaultConfig {
        applicationId "com.firstrnapp"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

buildscript {
 buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "23.1.7779620"
        kotlinVersion = "1.8.0"
        androidXBrowser = "1.4.0"

}
repositories {
    google()
    jcenter()
}

dependencies {
    // The version of react-native is set by the React Native Gradle Plugin
    implementation("com.facebook.react:react-android")
    classpath('com.android.tools.build:gradle:8.1.1')
    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

解决方案

1. 修正根目录build.gradle的Java版本配置

根目录build.gradle中compileOptions里的JavaVersion.VERSION_X是未定义占位符,Gradle 8.1.1要求使用JDK 17,需替换为具体版本:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

2. 清理app目录build.gradle的冗余配置

app目录的build.gradle存在多处错误配置,需调整:

  • 删除冗余的buildscript块(根目录已定义相关参数)
  • 移除dependencies中的classpath('com.android.tools.build:gradle:8.1.1')(classpath仅需在根目录buildscript中声明)
  • 将repositories中的jcenter()替换为mavenCentral()(jcenter已停止维护)
  • 添加FLIPPER_VERSION定义,避免变量未找到错误

修改后的app目录build.gradle核心部分:

// 新增FLIPPER_VERSION定义
def FLIPPER_VERSION = "0.182.0"

// ... 保留原android块内容 ...

// 删除原buildscript块
repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation("com.facebook.react:react-android")
    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }
    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}

3. 确认本地JDK版本匹配

Gradle 8.1.1强制要求JDK 17,执行以下命令检查当前JDK版本:

java -version

确保输出显示版本为17.x.x,若版本不符,需安装JDK 17并设置JAVA_HOME环境变量指向JDK 17的安装路径。

4. 清理缓存并重新构建

执行以下命令清理Gradle缓存并重新安装依赖:

./gradlew clean
npm install # 或 yarn install
npx react-native run-android

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 13:22:23