Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

Running google cloud speech api on Android 4.2

I have a problem running io.grpc:grpc-okhttp on Android 4.2 devices.

I have implemented SpeechClient with Credentials, for running Google cloud speech to text. On Android 7.1 device I don't have a problem. The problems happen on Android 4.2

So in my starting gradle I had

implementation 'io.grpc:grpc-okhttp:1.35.0'
implementation 'io.grpc:grpc-android:1.35.0'
implementation 'com.google.cloud:google-cloud-speech:1.24.0'

Problems.

  1. First problem was with TLS security for SSL (which is too old on Android 4.2). For this, I added dependency

    implementation 'org.conscrypt:conscrypt-android:2.5.1'
    

    and implemented in my DialogFragment (which is running the code)

     Security.insertProviderAt(Conscrypt.newProvider(), 1) 
    
  2. After I implemented this, I am getting

    Duplicate class org.conscrypt.AbstractConscryptEngine found in modules jetified-conscrypt-android-2.5.1-runtime (org.conscrypt:conscrypt-android:2.5.1) and jetified-conscrypt-openjdk-uber-2.2.1 (org.conscrypt:conscrypt-openjdk-uber:2.2.1)
    

    issues (alot of duplicated classes) So for fixing this, I added

    configurations {
       all {
          exclude group: "org.conscrypt", module: "conscrypt-openjdk-uber"
       }
     }
    
  3. Now I get this error, because com.google.api.client is using Standard.UTF-8 and my device is on Android 17.

com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Failed computing credential metadata
        at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:73)
        at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
        at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
        at com.google.api.gax.grpc.ExceptionResponseObserver.onErrorImpl(ExceptionResponseObserver.java:82)
        at com.google.api.gax.rpc.StateCheckingResponseObserver.onError(StateCheckingResponseObserver.java:86)
        at com.google.api.gax.grpc.GrpcDirectStreamController$ResponseObserverAdapter.onClose(GrpcDirectStreamController.java:149)
        at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426)
        at io.grpc.internal.ClientCallImpl.access$500(ClientCallImpl.java:66)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:689)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$900(ClientCallImpl.java:577)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:751)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:740)
        at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
        at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
     Caused by: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Failed computing credential metadata
        at io.grpc.Status.asRuntimeException(Status.java:533)
        at com.google.api.gax.grpc.GrpcDirectStreamController$ResponseObserverAdapter.onClose(GrpcDirectStreamController.java:149)?
        at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426)?
        at io.grpc.internal.ClientCallImpl.access$500(ClientCallImpl.java:66)?
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:689)?
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$900(ClientCallImpl.java:577)?
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:751)?
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:740)?
        at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)?
        at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)?
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)?
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)?
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)?
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)?
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)?
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)?
        at java.lang.Thread.run(Thread.java:856)?
     Caused by: java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets
        at com.google.api.client.util.StringUtils.getBytesUtf8(StringUtils.java:51)
        at com.google.api.client.json.webtoken.JsonWebSignature.signUsingRsaSha256(JsonWebSignature.java:571)
        at com.google.auth.oauth2.JwtCredentials.refresh(JwtCredentials.java:125)
        at com.google.auth.oauth2.JwtCredentials.getRequestMetadata(JwtCredentials.java:163)
        at com.google.auth.oauth2.ServiceAccountJwtAccessCredentials.getRequestMetadata(ServiceAccountJwtAccessCredentials.java:365)
        at com.google.auth.Credentials.blockingGetToCallback(Credentials.java:112)
        at com.google.auth.oauth2.ServiceAccountJwtAccessCredentials.getRequestMetadata(ServiceAccountJwtAccessCredentials.java:341)
        at io.grpc.auth.GoogleAuthLibraryCallCredentials.applyRequestMetadata(GoogleAuthLibraryCallCredentials.java:110)
        at io.grpc.CallCredentials2.applyRequestMetadata(CallCredentials2.java:58)
        at io.grpc.internal.CallCredentialsApplyingTransportFactory$CallCredentialsApplyingTransport.newStream(CallCredentialsApplyingTransportFactory.java:108)
        at io.grpc.internal.ForwardingConnectionClientTransport.newStream(ForwardingConnectionClientTransport.java:49)
        at io.grpc.internal.InternalSubchannel$CallTracingTransport.newStream(InternalSubchannel.java:671)
        at io.grpc.internal.DelayedClientTransport$PendingStream.createRealStream(DelayedClientTransport.java:353)
        at io.grpc.internal.DelayedClientTransport$PendingStream.access$300(DelayedClientTransport.java:341)

Question:

  1. How to resolve this issue and run Speech with "ApiStreamObserver? I tried reverting back "io.grpc:grpc-" libraries to version 1.0 or something, but then I have other problems...
question from:https://stackoverflow.com/questions/65939229/running-google-cloud-speech-api-on-android-4-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have resolved this issue and commited example project on Github

https://github.com/DjToMeK27/CloudGoogleSpeechToText/tree/main

What I did in this project

  1. Downloaded https://github.com/GoogleCloudPlatform/android-docs-samples/blob/master/README.md
  2. Changed classes in project SpeechRecognitionClient in android-docs-samples-masterspeech
    • Changed AudioRecord in AudoEmitter.kt to support API 16
  3. Imported org.conscrypt:conscrypt-android:2.5.1 library
    • Had to exclude group org.conscrypt from google-cloud-speech (in build.gradle)
  4. Added init { Security.insertProviderAt(Conscrypt.newProvider(), 1) } For Android 4.1 to run proper TLS for GRC to work
  5. I downloaded https://github.com/googleapis project
    • Changed all StandardCharsets found in google-http-client to Charset.forName())
    • And also changed some tests to work
    • https://imgur.com/a/iU7yjCZ (files changes)
  6. I have build this project and added .jar files, which I included here
  7. And now it works!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...