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

AppCenter构建React Native项目遭遇Gradle错误求助

Expo React Native项目关联AppCenter后Gradle构建失败问题

我将Expo创建的React Native练习项目(仅包含App.js文件)从GitLab仓库关联至AppCenter,用于构建、测试及部署。起初AppCenter无法识别项目,执行expo eject生成Android文件夹后关联成功,但构建时出现Gradle错误,本地项目也无法运行,修改Android目录中的Gradle版本后问题仍未解决。

构建错误日志

Starting a Gradle Daemon (subsequent builds will be faster)
Gradle 8.0.1
Gradle 8.0.1

> Configure project :gradle-plugin
Resolved org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.22 in detachedConfiguration1 
Resolved org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.22 in :classpath 
Project react-native-gradle-plugin at : is either no Android app project or build version has not been set to override. Skipping...

> Configure project :
Project ReVele at : is either no Android app project or build version has not been set to override. Skipping...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'ReVele'.
> Could not determine the dependencies of null.
   > Could not resolve all task dependencies for configuration ':classpath'.
      > Could not find com.android.tools.build:gradle:x.y.z.
        Searched in the following locations:
          - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/x.y.z/gradle-x.y.z.pom
          - https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/x.y.z/gradle-x.y.z.pom
        Required by:
            project :
      > Could not resolve com.facebook.react:react-native-gradle-plugin.
        Required by:
            project :
         > No matching variant of project :gradle-plugin was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0.1' but:
             - Variant 'apiElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a library, packaged as a jar, and its dependencies declared externally:
                 - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
                 - Other compatible attribute:
                     - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
             - Variant 'mainSourceElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a component, and its dependencies declared externally:
                 - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
                 - Other compatible attributes:
                     - Doesn't say anything about its target Java version (required compatibility with Java 8)
                     - Doesn't say anything about its elements (required them packaged as a jar)
                     - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
                     - Doesn't say anything about its usage (required runtime)
             - Variant 'runtimeElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
                 - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
                 - Other compatible attribute:
                     - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
             - Variant 'testResultsElementsForTest' capability com.facebook.react:react-native-gradle-plugin:unspecified:
                 - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
                 - Other compatible attributes:
                     - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                     - Doesn't say anything about its target Java version (required compatibility with Java 8)
                     - Doesn't say anything about its elements (required them packaged as a jar)
                     - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
                     - Doesn't say anything about its usage (required runtime)

* 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 2m 28s
Error: /Users/runner/work/1/s/android/gradlew failed with return code: 1
    at ChildProcess.<anonymous> (/Users/runner/work/_tasks/Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4/1.128.0/node_modules/vsts-task-lib/toolrunner.js:569:30)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:920:16)
    at Socket.<anonymous> (internal/child_process.js:351:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:509:12)
##[error]Error: /Users/runner/work/1/s/android/gradlew failed with return code: 1

解决方案

1. 修复Android Gradle插件版本缺失问题

打开android/build.gradle文件,找到buildscript下的dependencies块,将占位符x.y.z替换为与你当前React Native版本兼容的Android Gradle插件版本(例如RN 0.71+对应7.4.2):

buildscript {
    dependencies {
        classpath("com.android.tools.build:gradle:7.4.2")
        // 保留其他原有依赖
    }
}

同时修改android/gradle/wrapper/gradle-wrapper.properties中的distributionUrl,匹配对应Gradle版本(7.4.2插件对应Gradle 7.5):

distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip

2. 统一Java版本为11

错误显示react-native-gradle-plugin需要Java 11,但当前环境使用Java 8,需修改项目配置:
打开android/build.gradle,添加或更新编译配置:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
}

同时在AppCenter的构建配置中,将Java版本设置为11。

3. 修正react-native-gradle-plugin依赖

将android/build.gradle中本地依赖的react-native-gradle-plugin替换为Maven仓库的官方版本,确保版本与React Native一致:

buildscript {
    repositories {
        mavenCentral()
        // 保留其他仓库配置
    }
    dependencies {
        classpath("com.facebook.react:react-native-gradle-plugin:0.71.14") // 替换为你的RN版本对应值
    }
}

4. 清理缓存后重新构建

本地执行以下命令清理缓存:

cd android
./gradlew clean
cd ..
npm install

AppCenter构建前可添加清理缓存的步骤,确保构建环境干净。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:15:01