解决 Gradle 错误:Could not find androidx.appcompat:appcompat:1.7.1

Build Execution Failed

Task: :app:checkDebugAarMetadata

Issue: Could not resolve all files for configuration :app:debugRuntimeClasspath

Details:

Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
Could not find androidx.appcompat:appcompat:1.7.1.
Searched in the following locations:
        
  • file:/C:/Users/OS/.m2/repository/androidx/appcompat/appcompat/1.7.1/appcompat-1.7.1.pom
  • https://maven.aliyun.com/repository/public/androidx/appcompat/appcompat/1.7.1/appcompat-1.7.1.pom
  • https://repo.maven.apache.org/maven2/androidx/appcompat/appcompat/1.7.1/appcompat-1.7.1.pom

Required by: project :app

中央仓库也没有?但是 GOOGLE 是有这个版本。

问题分析

从错误日志可以看到,Gradle 只在以下位置搜索:

但没有搜索 Google 仓库! 这就是问题所在。

解决方案

  1. 确保 Google 仓库配置正确
  2. 在项目根目录的 build.gradle 中:

    allprojects {
        repositories {
            google() // 必须在前面!
            mavenCentral()
            maven { url 'https://maven.aliyun.com/repository/public' }
        }
    }

    或者如果使用 settings.gradle (较新的 Gradle 版本):

    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google() // 必须包含!
            mavenCentral()
            maven { url 'https://maven.aliyun.com/repository/public' }
        }
    }
  3. 如果网络问题,使用阿里云的 Google 镜像
  4. allprojects {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/google' } // Google 镜像
            maven { url 'https://maven.aliyun.com/repository/central' } // Central 镜像
            google() // 备用
            mavenCentral() // 备用
        }
    }
  5. 检查是否有仓库过滤或排除设置
  6. # 删除这类可能干扰仓库访问的设置
    # android.enableR8.fullMode=true
  7. 强制刷新依赖
  8. ./gradlew clean --refresh-dependencies
    ./gradlew build
  9. 验证版本存在性
  10. 手动验证该版本确实存在:Google 仓库

  11. 临时使用其他版本
  12. implementation 'androidx.appcompat:appcompat:1.7.0'
    // 或
    implementation 'androidx.appcompat:appcompat:1.6.1'