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

Kotlin/Android中Google Pub/Sub客户端初始化TLS异常排查

解决Android上Google Pub/Sub客户端初始化的TLS ALPN错误

问题背景

首次开发Android应用(使用Kotlin),集成Google Pub/Sub服务,已有Go语言客户端。确认credentials.json、项目ID及主题名称等配置均正确加载,但添加grpc、boringssl依赖后仍遇到TLS相关初始化错误。

错误信息

Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt, or Jetty NPN/ALPN available

相关配置

build.gradle.kts

android {
    packagingOptions {
        resources.excludes.add("META-INF/*")
    }
}

dependencies {
    implementation(libs.googlePubSub)
    implementation(libs.grpc)
    implementation(libs.tls)
}

gradle/libs.versions.toml

[versions]
googlePubSub = "1.134.2"
grpc = "1.68.1"
tls = "2.0.69.Final"

[libraries]
googlePubSub = { group = "com.google.cloud", name = "google-cloud-pubsub",    version.ref="googlePubSub"}
grpc = { group = "io.grpc", name = "grpc-netty", version.ref="grpc"}
tls = { group = "io.netty", name = "netty-tcnative-boringssl-static",   version.ref="tls"}

问题代码片段

fun getPublisher(): Publisher {
    return Publisher.newBuilder(getTopic()).setCredentialsProvider { credentials}.build()
}

fun getTopic(): TopicName {
    val topic = TopicName.of(configuration.project, configuration.requestTopic)
    val topicAdminSettings = TopicAdminSettings.newBuilder().setCredentialsProvider{ credentials }.build()
    TopicAdminClient.create(topicAdminSettings).use { topicAdminClient -> topicAdminClient.createTopic(topic)}
    return topic
}

解决过程

  • EDIT #1:将grpc-netty依赖替换为grpc-okhttp,修改打包配置为仅排除META-INF/INDEX.LIST和META-INF/DEPENDENCIES,此时出现pubsub.googleapis.com无法解析的错误。
  • EDIT #2:排查后发现AndroidManifest.xml中未添加Internet权限,补充权限声明。
  • EDIT #3:完成上述调整后,Pub/Sub客户端成功初始化,服务连通正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 05:13:12