【问题标题】:Gradle sync is failing with "Could not resolve dependency" errorsGradle 同步失败并出现“无法解析依赖项”错误
【发布时间】:2022-12-14 22:48:38
【问题描述】:

我有以下设置:

  • Android Studio 2021.3.1
  • 等级:7.4

根级别 build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
        google()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath 'com.github.spotbugs:spotbugs-gradle-plugin:4.7.1'
    }
}

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

模块级 build.gradle:

import com.github.spotbugs.snom.SpotBugsTask

buildscript {
    repositories {
        mavenCentral()
        google()
        maven { url 'https://jitpack.io' }
    }
}

plugins {
    id 'com.android.library'
    id 'checkstyle'
    id 'com.github.spotbugs'
}

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

apply from: "publisher.gradle"

<...>

dependencies {
    implementation platform('com.google.firebase:firebase-bom:31.0.0')
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.mikepenz:materialdrawer:6.1.2'
    implementation 'com.github.AppIntro:AppIntro:6.2.0'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-database'
    implementation 'com.github.sematext:sematext-logsene-android:3.2.0'
    implementation 'com.github.mik3y:usb-serial-for-android:3.4.6'
}

在做的同时梯度同步在 Android Studio 中,我遇到了以下错误:

文本中的第一个错误:

:projectName:main: Could not resolve androidx.appcompat:appcompat:1.5.1.
Required by:
    project :projectName

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

正如您在上面看到的,我已经声明了包含这些依赖项的存储库,并且我确信这些版本存在。

我已经尝试过:清除 gradle 缓存、清除 Android Studio 缓存并重新启动、切换到另一个网络、关闭我的防火墙—​​—没有任何帮助。我看到 gradle 已经成功地解决了完整列表中的一些依赖项(例如,materialdrawer 和 firebase)。

前段时间一切正常 - 我已经 2-3 个月没有打开这个项目了,它之前一直在工作。现在不是了,我不明白为什么。有人可以帮我吗?

啊,顺便说一下,“gradle clean”,然后是“gradle build”,它使用相同的依赖关系,成功完成,真正组装构建,在同一台笔记本电脑上的相同 Android Studio 实例上,只有“gradle sync”是失败。这让我真的不清楚发生了什么。

【问题讨论】:

    标签: android android-studio gradle


    【解决方案1】:

    在您的根级别 gradle 中,也许尝试添加

    maven {
      url = uri("https://plugins.gradle.org/m2/")
    }
    

    所以概述

    buildscript {
        val kotlinVersion = "1.7.20"
        repositories {
            maven {
                url = uri("https://plugins.gradle.org/m2/")
            }
            google()
            mavenCentral()
        }
        dependencies {
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    

    注意:我的 Gradle 采用 kts 格式,因此您的格式可能有所不同。

    【讨论】:

    • 谢谢,但这没有帮助。
    • @Roher 你更新到最新的 Android Studio 和 gradle 版本了吗?
    • @Noiling 是的,我记得我必须在互联网的深处搜索这个解决方案。
    • 我打开了一个问题,因为这似乎与 Android Studio Dolphin 有关。我现在在清单中得到“未解决的类”,尽管我可以在模拟器中启动应用程序。这是链接:issuetracker.google.com/u/1/issues/254891042
    • 我删除了整个SDK文件夹并更新为AS Electric Eel,终于解决了所有问题。所以是的,我认为这与 AS Dolphin 有关。
    【解决方案2】:

    我最近遇到了由“androidx.legacy:legacy-support-v4:1.0.0”引起的“facebook-login”依赖性的类似问题。

    禁用 Jetifiergradle.properties解决它。

    android.enableJetifier=false
    

    【讨论】: