javaandroidgrpcillegalstateexceptionclarifai

ClarifaiChannel.INSTANCE.getGrpcChannel() throwing IllegalStateException: Could not find TLS ALPN provider


I am trying to use Clarifai to predict images in an Android app. I am using the Clarifai gRPC Java client. However, I am getting an IllegalStateException saying 'Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt, or Jetty NPN/ALPN available'. I am using Java 11, Clarifai gRPC 8.0.0.

Below is my code:

public class ClarifaiTask extends AsyncTask<File, Integer, Boolean> {

    protected Boolean doInBackground(File... images) {
        V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel()).withCallCredentials(new ClarifaiCallCredentials(API_KEY));


        MultiOutputResponse response = stub.postModelOutputs(PostModelOutputsRequest.newBuilder().setModelId(MODEL_ID).addInputs(Input.newBuilder().setData(Data.newBuilder().setImage(Image.newBuilder().setUrl(IMG_URL)))).build());

        if (response.getStatus().getCode() != StatusCode.SUCCESS) {
            throw new RuntimeException("Request failed, status: " + response.getStatus());
        }

        for (Concept c : response.getOutputs(0).getData().getConceptsList()) {
            System.out.println(String.format("%12s: %,.2f", c.getName(), c.getValue()));
        }

        return true;
    }
    
    protected void onPostExecute(Boolean result) {

    }
}

The error:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.package.name, PID: 25874
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$4.done(AsyncTask.java:415)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:381)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:250)
        at java.util.concurrent.FutureTask.run(FutureTask.java:269)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at java.lang.Thread.run(Thread.java:1012)
     Caused by: java.lang.IllegalStateException: Could not find TLS ALPN provider; no working netty-tcnative, Conscrypt, or Jetty NPN/ALPN available
        at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.defaultSslProvider(GrpcSslContexts.java:246)
        at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.configure(GrpcSslContexts.java:146)
        at io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts.forClient(GrpcSslContexts.java:95)
        at com.clarifai.channel.ClarifaiChannel.getGrpcChannel(ClarifaiChannel.java:37)
        at com.clarifai.channel.ClarifaiChannel.getGrpcChannel(ClarifaiChannel.java:28)
        at com.dinuxsoft.somethingg.ClarifaiTask.doInBackground(ClarifaiTask.java:41)
        at com.dinuxsoft.somethingg.ClarifaiTask.doInBackground(ClarifaiTask.java:34)
        at android.os.AsyncTask$3.call(AsyncTask.java:394)
        at java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) 
        at java.lang.Thread.run(Thread.java:1012) 

Any help is appreciated.


Solution

  • you can try installing conscrypt to provide TLS support.

    Then, you can try updating your ClarifaiTask with:

    static {
        Security.insertProviderAt(Conscrypt.newProvider(), 1);
    }
    

    Before you're calling the stub.