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

Mac多Android项目Gradle依赖故障求助:无法确定编译任务依赖

问题:多Android项目Gradle依赖异常,报错"Could not determine the dependencies of task ':app:compileProductionDebugKotlin'"

在MacBook上维护多个Android项目,之前为修复某项目的Gradle运行问题修改了版本配置,该项目恢复正常,但其余所有项目均出现异常,部分依赖失效,核心报错为:

Could not determine the dependencies of task ':app:compileProductionDebugKotlin'

已尝试的解决方案(均无效)

  • 更新Gradle版本
  • 切换Java版本
  • 添加Maven/Google/JCenter仓库
  • 修改Gradle Wrapper及Properties版本配置
  • 多次重装Android Studio(含旧版本)
  • 重新从源码控制导入项目

附项目app模块build.gradle代码

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 33
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.sample"
        minSdkVersion 26
        targetSdkVersion 33
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true

    }
    packagingOptions {
        resources.excludes.add("META-INF/*")
    }
    productFlavors {
        flavorDimensions "default"
    }

    signingConfigs {
        release {
            …. 
            v2SigningEnabled true
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        debug {
            //            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    androidExtensions {
        experimental = true
    }

    lintOptions {
        abortOnError false
    }

    dataBinding {
        enabled = true
    }
    applicationVariants.all { variant ->
        variant.outputs.all {
            def flavor = ""
            if (variant.name == "stagingDebug") {
                flavor = "...."
            } else {
                flavor = "...."
            }

            def versionName = variant.versionName
            outputFileName = "${flavor}-${versionName}.apk"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31"

    // SUPPORT
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.core:core-ktx:1.8.0'
    // RecyclerView
    implementation "androidx.recyclerview:recyclerview:1.2.1"

    // CardView
    implementation 'androidx.cardview:cardview:1.0.0'

    // ConstraintLayout
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"

    //Material
    implementation 'com.google.android.material:material:1.5.0-alpha02'

    /*    // TESTING
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'*/
    implementation 'com.android.support:multidex:1.0.3'

    // Scalable dimensions
    implementation 'com.intuit.sdp:sdp-android:1.0.6'
    implementation 'com.intuit.ssp:ssp-android:1.0.6'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.viewpager:viewpager:1.0.0'
    implementation 'com.google.firebase:firebase-database-ktx:20.0.4'
    //implementation project(path: ':mediaslider')

    // Mockito
    testImplementation 'org.mockito:mockito-core:2.23.0'
    androidTestImplementation 'org.mockito:mockito-android:2.7.22'

    // LifeCycle Extensions : LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"

    // LifeCycle Extensions : ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

    // LifeCycle Extensions : Lifecycle Scope
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"

    // Activity Kotlin Extension
    implementation 'androidx.activity:activity-ktx:1.1.0'

    // Fragment Kotlin Extension
    implementation 'androidx.fragment:fragment-ktx:1.3.3'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'com.squareup.okhttp3:mockwebserver:4.5.0'

    // Dagger Hilt
    implementation 'com.google.dagger:dagger:2.39.1'
    annotationProcessor "com.google.dagger:dagger-compiler:2.39.1"
    kapt 'com.google.dagger:dagger-compiler:2.39.1'

    implementation 'com.google.dagger:hilt-android:2.37'
    kapt 'com.google.dagger:hilt-android-compiler:2.37'
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7"

    // Glide
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

    implementation 'org.jetbrains.anko:anko-sdk15:0.8.2'
    implementation 'com.beust:klaxon:0.30'

    // Timber Logger
    implementation 'com.jakewharton.timber:timber:4.7.1'

    // Broadcast manager
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

    //Country code picker
    implementation 'com.hbb20:ccp:2.4.0'

    // Firebase Auth
    implementation 'com.google.firebase:firebase-auth-ktx:19.4.0'
    implementation 'com.google.android.gms:play-services-auth:18.1.0'

    //Nested RecyclerView
    implementation 'com.github.rubensousa:gravitysnaphelper:2.1.0'

    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    implementation 'io.reactivex:rxandroid:1.2.0'
    implementation 'io.reactivex:rxjava:1.1.8'

    implementation 'com.squareup.okhttp3:okhttp:4.5.0'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-messaging:17.3.3'

    //crashLytics
    implementation platform('com.google.firebase:firebase-bom:26.1.0')
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation platform('com.google.firebase:firebase-bom:26.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation("com.google.firebase:firebase-config")

    implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'

    //glide image transformation
    implementation 'jp.wasabeef:glide-transformations:3.0.1'
    implementation 'com.github.dhaval2404:imagepicker:1.7.3'
    implementation 'com.android.support:design:26.1.0'

    implementation('com.journeyapps:zxing-android-embedded:4.2.0') { transitive = false }
    implementation 'com.google.zxing:core:3.3.0'

    compile fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.github.vipulasri.layouttoimage:library:1.0.0'
    implementation 'id.zelory:compressor:3.0.1'
    implementation("androidx.activity:activity-ktx:1.5.1")
    implementation 'pub.devrel:easypermissions:3.0.0'

    implementation 'id.zelory:cekrek:1.0.0'

    implementation("org.greenrobot:eventbus:3.3.1")

    implementation 'com.nex3z:notification-badge:1.0.4'

    //websocket
    implementation 'tech.gusavila92:java-android-websocket-client:1.2.2'

    //calender
    implementation 'io.github.architshah248.calendar:awesome-calendar:2.0.0'

    // Add the dependency for the Performance Monitoring library
    implementation("com.google.firebase:firebase-perf")
}

可行解决方案尝试

1. 检查全局Gradle配置

Mac上全局Gradle配置文件位于~/.gradle/gradle.properties和~/.gradle/init.gradle,之前修改的配置可能影响所有项目:

  • 打开这两个文件,删除或注释掉可能修改的版本强制配置(比如org.gradle.java.home、kotlin.version、android.useAndroidX等全局属性)
  • 恢复默认配置,确保没有全局覆盖项目本地配置

2. 彻底清理缓存

  • 执行终端命令清理单个项目缓存并强制刷新依赖:
    ./gradlew clean build --refresh-dependencies
    
  • 清理Android Studio全局缓存:
    • 打开Android Studio,点击File > Invalidate Caches...,选择Invalidate and Restart
    • 删除Mac上的Gradle缓存目录:rm -rf ~/.gradle/caches

3. 修复依赖冲突与版本不一致

从提供的build.gradle看,存在多处版本冲突和不合理配置,需逐一修正:

  • 重复依赖:删除重复声明的implementation fileTree(dir: 'libs', include: ['*.jar']),移除重复的com.google.android.material:material依赖,保留稳定版(建议1.9.0)
  • Firebase版本统一:删除单独指定版本的firebase-core、firebase-messaging,仅保留Firebase BOM管理所有Firebase依赖
  • Hilt版本匹配:将dagger和hilt-android版本统一为同一稳定版(比如2.44),确保插件与依赖版本一致
  • 移除Support库:删除com.android.support:design:26.1.0,替换为AndroidX的com.google.android.material:material
  • 更新旧依赖:将androidx.activity:activity-ktx、androidx.fragment:fragment-ktx等旧版本升级到与compileSdk 33兼容的稳定版

4. 修正根目录build.gradle配置

确保根目录的build.gradle中仓库和插件版本配置正确,示例如下:

buildscript {
    ext {
        kotlin_version = "1.5.31"
        hilt_version = "2.37"
        gradle_version = "7.4.2"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath "com.google.gms:google-services:4.3.15"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.5"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

5. 验证系统环境变量

  • 检查JAVA_HOME环境变量,确保指向与项目兼容的JDK版本(建议JDK 11,适配Gradle 7.x):
    echo $JAVA_HOME
    
  • 若使用多个JDK,可通过jenv或asdf管理版本,确保每个项目使用正确的JDK

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 09:22:04