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

Android集成Google Photo Library遇NoSuchMethodError问题求助

解决Google Photos Library Client初始化时的NoSuchMethodError问题

我在Android应用中尝试获取并展示Google照片,使用以下代码但无法正常运行:

try {
    val credentialsStream : InputStream = resources.openRawResource(R.raw.credential)

    val credentials: GoogleCredentials =
        GoogleCredentials.fromStream(credentialsStream)
            .createScoped(Collections.singletonList("https://www.googleapis.com/auth/photoslibrary.readonly"))


    val settings = PhotosLibrarySettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
        .build()

    val photosLibraryClient: PhotosLibraryClient = PhotosLibraryClient.initialize(settings)

    val albums: MutableIterable<Album>? = photosLibraryClient.listAlbums().iterateAll()
    if (albums != null) {
        for (album in albums) {
            println("Album title: " + album.title)
            println("Album product URL: " + album.productUrl)
       
        }
    }

} catch (e: ApiException) {
    JLog.w("Error", "handleSignInResult:error: ${e.message}")
    e.printStackTrace()
}

使用的依赖库如下:

implementation ('io.grpc:grpc-okhttp:1.31.1') {
    exclude group: "com.squareup.okhttp"
}
implementation 'com.google.api-client:google-api-client-android:1.31.1'
implementation 'com.google.photos.library:google-photos-library-client:1.7.2'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.7.0'

执行val photosLibraryClient: PhotosLibraryClient = PhotosLibraryClient.initialize(settings)时出现错误:

java.lang.NoSuchMethodError: No direct method (Ljava/lang/String;)V in class Lio/grpc/internal/AbstractManagedChannelImplBuilder; or its super classes (declaration of 'io.grpc.internal.AbstractManagedChannelImplBuilder' appears in /data/app/~~MOmfPzH0_A99g7RxMbRc7Q==/com.clicandprint-OwkMyEC4oLeBuyKNeMogtw==/base.apk!classes33.dex)


解决方案

这个错误源于grpc依赖版本不兼容:你手动指定的io.grpc:grpc-okhttp:1.31.1与Google Photos Library Client 1.7.2依赖的grpc版本存在方法签名差异,导致找不到对应方法。

方法一:移除手动指定的grpc依赖

让Photos Library Client自动引入兼容的grpc版本,修改后的依赖如下:

implementation 'com.google.api-client:google-api-client-android:1.31.1'
implementation 'com.google.photos.library:google-photos-library-client:1.7.2'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.7.0'

方法二:统一grpc版本

如果必须手动管理grpc版本,需确保所有grpc相关组件版本一致。可以查看Photos Library Client 1.7.2的maven依赖树,找到它依赖的grpc版本,然后统一指定该版本的grpc组件。

额外注意事项

  • 确认R.raw.credential对应的json文件是有效的Google Cloud服务账号凭据,且已在控制台启用Photos Library API
  • 服务账号需要获得目标Google Photos账号的访问权限:如果是个人账号,需将相册共享给服务账号邮箱;如果是G Suite域账号,需配置域范围委派。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:13:59