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

Android开发:无法解析com.google.android.gms:play-services-calendar依赖求助

解决Google Calendar依赖解析失败及CalendarScope权限请求问题

依赖解析失败的解决步骤

1. 检查Google Maven仓库配置

首先确认项目的仓库配置里包含Google Maven仓库,Google官方依赖都托管在这里,缺失的话会导致版本解析失败:

// Gradle 7+ 请在settings.gradle中配置
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

// 旧版Gradle 请在根目录build.gradle中配置
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

2. 强制统一依赖版本

报错里提示找不到20.0.0版本,但你明明声明的是20.4.0,这是因为项目中其他依赖可能间接引入了旧版本的calendar库,导致Gradle尝试拉取不兼容的版本。在模块的build.gradle中添加版本强制规则,强制使用你指定的20.4.0:

configurations.all {
    resolutionStrategy {
        force 'com.google.android.gms:play-services-calendar:20.4.0'
    }
}

3. 清理缓存重新同步

做完上面的配置后,执行以下操作:

  • 点击Android Studio右上角的「Sync Project with Gradle Files」按钮同步项目
  • 执行File > Invalidate Caches... > Invalidate and Restart清理IDE缓存并重启

CalendarScope权限的正确写法

你代码里直接写CalendarScope会有问题,正确的做法是使用Google提供的标准作用域常量:

import com.google.android.gms.common.Scopes
import com.google.android.gms.common.api.Scope

// 修改后的GoogleSignInOptions构建代码
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScope(Scope(Scopes.CALENDAR)) // 读写权限,只读用Scopes.CALENDAR_READONLY
        .requestIdToken(app.getString(R.string.web_client_id))
        .requestEmail()
        .build()

另外要注意,必须在Google Cloud Console中为你的OAuth 2.0客户端ID启用Calendar API权限,否则即使代码正确,请求权限时也会失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 03:30:43