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

Android Studio可构建但命令行gradlew任务执行失败求助

问题描述

使用最新版Android Studio Hedgehog | 2023.1.1 Patch 2创建空Activity项目,分别尝试Kotlin DSL和Groovy DSL。项目能通过Android Studio内置构建命令正常构建,但命令行执行任何gradlew任务均失败,仅./gradlew --version可正常运行,输出如下:

------------------------------------------------------------ Gradle 8.2
Build time: 2023-06-30 18:02:30 UTC Revision:
5f4a070a62a31a17438ac998c2b849f4f6892877
Kotlin: 1.8.20 Groovy: 3.0.17 Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023 JVM: 1.8.0_382 (Amazon.com Inc. 25.382-b05) OS: Mac OS X 14.3.1 aarch64

gradle-wrapper.properties配置:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip

Android Studio设置中「Build / Build tools / Gradle」的Gradle JDK已设为GRADLE_LOCAL_JAVA_HOME (jbr 17.0.7)。

所有gradlew调用失败,错误信息如下:

FAILURE: Build failed with an exception.

* What went wrong:
>A problem occurred configuring root project 'GradleTest2'.

> Could not resolve all files for configuration ':classpath'.

> Could not resolve com.android.tools.build:gradle:8.2.2.

> Required by: project :
> com.android.application:com.android.application.gradle.plugin:8.2.2

> No matching variant of com.android.tools.build:gradle:8.2.2 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.2' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:8.2.2 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.2')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:8.2.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation 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.2')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:8.2.2 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.2')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:8.2.2 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation 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.2')

* 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 391ms
mjollneer@IBMPC GradleTest % ./gradlew       

FAILURE: Build failed with an exception.

更新1

系统JAVA_HOME当前设置为:

/Users/mjollneer/Library/Java/JavaVirtualMachines/corretto-1.8.0_382/Contents/Home

若将其设置为jbr-17:

/Applications/Android\ Studio.app/Contents/jbr/Contents/Home

执行gradlew会报错:

ERROR: JAVA_HOME is set to an invalid directory

更新2

项目初始配置:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

将上述3个常量改为11或17后,问题依旧(Android Studio可正常构建,命令行仍失败)。

解决方案

1. 修复JAVA_HOME路径问题

设置jbr-17时出现路径无效错误,是因为路径里的转义符\多余,正确路径应为:

/Applications/Android Studio.app/Contents/jbr/Contents/Home

终端临时设置(仅当前会话有效):

export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"

永久生效需根据Shell类型修改配置文件:

  • zsh:编辑~/.zshrc,添加上述export命令后执行source ~/.zshrc
  • bash:编辑~/.bash_profile或~/.bashrc,添加命令后执行source ~/.bash_profile

2. 统一Gradle构建的JDK版本

Android Gradle Plugin 8.2.2要求构建时使用Java 11及以上版本,命令行用Java 8会导致不兼容。修改项目根目录下的gradle.properties,添加配置强制Gradle使用指定JDK:

org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home

此配置会覆盖系统JAVA_HOME,确保Gradle用JBR 17构建。

3. 验证配置

执行以下命令确认Gradle使用的JDK版本:

./gradlew --version

输出中JVM版本应显示为17.x.x,之后执行./gradlew assembleDebug等任务即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:55:16