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
相关产品推荐
相关产品推荐

