grpc-java: NoSuchMethodException due to OkHttpChannelProvider default constructor missing after R8 full mode optimization
What version of gRPC-Java are you using?
1.49.2
What is your environment?
Android
What did you see?
When run after R8 full mode optimization, grpc throws an exception during execution:
Failed to construct OkHttpChannelProvider
java.lang.NoSuchMethodException: io.grpc.okhttp.OkHttpChannelProvider.<init> []
at java.lang.Class.getConstructor0(Class.java:2363)
at java.lang.Class.getConstructor(Class.java:1759)
at io.grpc.android.AndroidChannelBuilder.<clinit>(SourceFile:14)
at com.example.GrpcServiceModule$channel$1.invokeSuspend(SourceFile:51)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(SourceFile:6)
at kotlinx.coroutines.DispatchedTask.run(SourceFile:108)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(SourceFile:77)
This is because R8 full mode does not implicitly keep default constructors: https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md#r8-full-mode
This Proguard/R8 configuration rule avoids the issue and should be added to https://github.com/grpc/grpc-java/blob/master/android/proguard-rules.txt:
-keepclassmembers, allowoptimization class io.grpc.okhttp.OkHttpChannelProvider {
<init>();
}
There may be additional configuration rules needed to take care of further default constructors accessed via reflection.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (8 by maintainers)
Well, I slept better for 3 weeks.
Yes, I also understood that that android-interop-testing would use
OkHttpChannelBuilder
. But my initial test ended up not using R8 as the debug build I was testing with did not have it enabled. It failed after addingminifyEnabled
, but not with the error from this issue. I also determined that version2.2.64
of R8 is being used, which is quite old. I’ll work on updating the project and reproducing the problem withOkHttpChannelBuilder
.