androidkotlingoogle-cloud-pubsub

Unexpected TLS exception with Google Pub/Sub client in kotlin / java


I am writing an app on android that utilizes Google pubsub and have a corresponding client already written in go.

Please forgive me in advance as this is my first android app and first time using kotlin. I've programmed in Java and go, so if something looks glaringly wrong, apologies in advance.

I've gone through documentation, but am stuck.

The problem I am running into stems from initializing a pubsub client. I believe my credentials json is picked up along with my project, topic, etc. The issue I am running into is that I don't believe I should be adding more dependencies to the project since the google pubsub example does not include those dependencies. I don't know how far that rabbit hole goes, but I would think that the Google example should work out of the box:

I am referencing: https://cloud.google.com/pubsub/docs/publish-receive-messages-client-library#pubsub-client-libraries-java

I am not using the gcloud command, but have created a credentials.json and it works in my golang app just fine.

Relative bits: build.gradle.kts

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

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

app/src/main/res/raw/credentials.json

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"}

Problem occurs here:

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
}

I first added the grpc library and then added boringssl library, but that didn't seem right. The most recent error I get (even after adding that boringssl library) is:

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

I must point out that the configuration and credentials objects are properly populated at this point.

I tried following the online documentation for Google pubsub Java client and was expecting a message to be published. I also referred to an example project, but those libraries appear out of date:

https://github.com/androidthings/weatherstation

EDIT #1:

I updated the dependencies:

grpc-netty -> grpc-okhttp

and modified the packaging:

resources.excludes.add("META-INF/*") -> resources.excludes.add("META-INF/INDEX.LIST") and resources.excludes.add("META-INF/DEPENDENCIES")

The error I am facing now is as if the application is not connected to the Internet. It complains that pubsub.googleapis.com cannot be resolved yet on that same machine, a ping resolves to an IP address.

EDIT #2: Upon further debugging, I see I need the Internet permission.

EDIT #3: Pub Sub connectivity is working.


Solution

  • The solution to my problem was indeed dependencies. I stumbled upon stale code and the dependencies weren't right.

    The solution was:

    1. use grpc-okhttp, not grpc-netty
    2. instead of excluding everything under META-INF, just exclude INDEX.LIST and DEPENDENCIES