Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC fails when service method called from Android SyncAdapter

Tags:

android

grpc

It took me some time to figure out that below error is caused only when I try to use gRPC client from SyncAdapter:

No functional channel service provider found. Try adding a dependency on the grpc-okhttp or grpc-netty artifact

The setup is exactly like in official tutorial Transferring Data Using Sync Adapters.

I've generated gRPC code using same setup as an example from grpc-java. The compile 'com.squareup.okhttp:okhttp:2.2.0' dependency in example is not necessary as compile 'io.grpc:grpc-okhttp:0.9.0' was modified to be self contained.

When the gRPC service method is called from the main activity it works fine.

I suspect that the grpc-okhttp must be initialized in some way but have no clue how.

like image 953
Paweł Szczur Avatar asked Sep 25 '22 21:09

Paweł Szczur


1 Answers

I've decided to read the code of gRPC. The code I've used to create channel was:

mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
        .usePlaintext(true).build();

The way the generic ManagedChannelBuilder class works is to dynamically load all classes extending ManageChannelBoulder and choose the one with highest priority. Then the chosen class is remembered in static variable.

It seems that during a call in SyncAdapter the appropriate builder for okhttp was not available. I've fixed it by hardcoding the choosen builder:

mChannel = OkHttpChannelBuilder.forAddress(mHost, mPort)
        .usePlaintext(true).build();

I hope to save someone's evening.

like image 110
Paweł Szczur Avatar answered Oct 12 '22 11:10

Paweł Szczur