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

Build.gradle可编译但出现Gradle依赖解析错误,请求技术支持

编译错误解决方案:Gradle无法解析com.android.tools.build:gradle:8.1.1

错误信息

无法使用Gradle安装路径'/Users/davideresigotti/.sdkman/candidates/gradle/current'运行阶段构建操作。org.gradle.api.ProjectConfigurationException: 配置根项目'android'时出现问题。无法解析配置':classpath'的所有文件。无法解析com.android.tools.build:gradle:8.1.1。所需条件:项目: 未找到匹配的com.android.tools.build:gradle:8.1.1变体。消费者被配置为寻找运行时使用、兼容Java 8、打包为jar、依赖外部声明的库,以及属性'org.gradle.plugin.api-version'值为'8.3',但:

  • 变体'apiElements'的能力com.android.tools.build:gradle:8.1.1声明了一个打包为jar、依赖外部声明的库:
    • 不兼容,因为该组件声明为编译时使用、兼容Java 11,而消费者需要运行时使用、兼容Java 8的组件
    • 其他兼容属性:
      • 未提及org.gradle.plugin.api-version(要求为'8.3')
  • 变体'javadocElements'的能力com.android.tools.build:gradle:8.1.1声明了一个运行时使用、依赖外部声明的组件:
    • 不兼容,因为该组件声明为文档,而消费者需要库
    • 其他兼容属性:
      • 未提及目标Java版本(要求兼容Java 8)
      • 未提及元素打包方式(要求打包为jar)
      • 未提及org.gradle.plugin.api-version(要求为'8.3')
  • 变体'runtimeElements'的能力com.android.tools.build:gradle:8.1.1声明了一个运行时使用、打包为jar、依赖外部声明的库:
    • 不兼容,因为该组件声明兼容Java 11,而消费者需要兼容Java 8的组件
    • 其他兼容属性:
      • 未提及org.gradle.plugin.api-version(要求为'8.3')
  • 变体'sourcesElements'的能力com.android.tools.build:gradle:8.1.1声明了一个运行时使用、依赖外部声明的组件:
    • 不兼容,因为该组件声明为文档,而消费者需要库
    • 其他兼容属性:
      • 未提及目标Java版本(要求兼容Java 8)
      • 未提及元素打包方式(要求打包为jar)
      • 未提及org.gradle.plugin.api-version(要求为'8.3')

当前app模块Build.gradle代码

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    namespace "com.example.soliveri_app"
    compileSdk 34


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.soliveri_app"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

解决方案

1. 对齐Gradle与AGP版本

AGP 8.1.1 要求Gradle版本在8.0-8.2之间,当前使用的Gradle 8.3版本不兼容,需降级:

  • 修改gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
    

2. 统一根项目Java版本配置

根项目的Build.gradle(非app模块)需配置Java 11+(AGP 8.x要求最低Java 11),确保classpath解析时使用正确的Java版本:

buildscript {
    ext.kotlin_version = '1.8.10' // 与AGP 8.1.1兼容的Kotlin版本
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.0' // 适配AGP的Google Services版本
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

// 配置所有编译任务使用Java 11
tasks.withType(JavaCompile) {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

subprojects {
    tasks.withType(JavaCompile) {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
}

3. 修正app模块依赖

将Kotlin依赖从jdk7升级为jdk8,匹配当前Java版本:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

4. 清理缓存并同步

  • 执行命令清理缓存并重新构建:
    ./gradlew clean build --refresh-dependencies
    
  • 或在Android Studio中依次执行:File -> Sync Project with Gradle Files,再Build -> Clean Project

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 23:27:05