GoogleCloudPlatform / android-docs-samples

Apache License 2.0
375 stars 596 forks source link

Proguard Enabled - Google Speech-to-Text API stopped working #120

Open MuhammadRashid opened 4 years ago

MuhammadRashid commented 4 years ago

Hi, Google Speech-to-Text API stopped working when progurard is enabled in Android app.

release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' }

I am using Speech-to-Text Client Libraries. App's Gradle file dependencies are below; // Dependencies for the speech client implementation 'io.grpc:grpc-okhttp:1.27.0' implementation 'com.google.cloud:google-cloud-speech:1.22.3'

dejandobnikar commented 3 years ago

Ran into the same problem.

For

implementation io.grpc:grpc-okhttp:1.32.1
implementation com.google.cloud:google-cloud-speech:1.24.0

The following configuration works:

-keep class com.google.api.** { *; }
-keep class io.grpc.** { *; }
-keep class com.google.rpc.** { *; }
-keep class com.google.auth.oauth2.** { *; }
-keep class com.google.protobuf.** { *; }
-keep class com.google.cloud.speech.** { *; }

It would be awesome if the samples would include the most optimal Proguard configuration.

Del-S commented 2 years ago

Another issue wi proguard is with Text-to-speech library. Issue was due to Java Gax which is used to get library version. Done in Speech and TextToSpeech as well. It uses package information but since proguard strips the package it will return null and gax will not be able to figure out library version.

Call site gets package (will be null) and it calls getImplementationVersion on it which results in NPE:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getImplementationVersion()' on a null object reference
    at com.google.api.gax.core.GaxProperties.getLibraryVersion(SourceFile:1)
    at m27.invoke(SourceFile:10)

Adding this line to proguard configuration solves the issue:

-keep public class com.google.cloud.texttospeech.v1.stub.TextToSpeechStubSettings {public *; protected *;}

Or you can just keep the whole text to speach package to be sure.

-keep class com.google.cloud.texttospeech.** { *; }

First option solves Gax issue and second keeps it not obfuscated as a whole which makes them both not optimal solutions since there might be more issues than just the one with Gax and it is also not optimal to keep the whole package un-obfuscated.

Bottom line is: as @dejandobnikar said it would be awesome if each library would contain optimal Proguard configuration.