Search code examples
androidclassnotfoundexceptionokhttpandroid-9.0-pie

ClassNotFoundException: Didn't find class SchemeRegistry on Android 9


I am using the following OkHttp API to set SSL Factory:

OkHttpClient.Builder.sslSocketFactory(sslContext.getSocketFactory(), x509TrustManager);

To get sslContext I need to create SchemeRegistry:

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", new SslSocketFactory(sslContext), 443));

This produces the following crash, though:

ClassNotFoundException: Didn't find class "org.apache.http.conn.scheme.SchemeRegistry"

What can I do to avoid the crash?


Solution

  • The obsolete SchemeRegistry class is no longer available on Android P.

    If you still want/need to use legacy HTTP API, in addition to

    useLibrary 'org.apache.http.legacy'
    

    in android section of build.gradle, you also need to add

        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />
    

    to <application> tag in your Manifest.