Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionInInitializerError in Okhttp.Builder w/ Multidex & Kitkat

I'm experiencing an ExceptionInInitializerError exception on VM running Kitkat 4.4.2 with Multidex enabled.

java.lang.ExceptionInInitializerError
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229)
    at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015)
    at myapp.utils.Utils.getHttpClientBuilder(Utils.java:131)
    at myapp.fragments.FragmentHome.getHome(FragmentHome.java:326)
    at 
myapp.fragments.FragmentHome.onViewCreated(FragmentHome.java:135)

I have the following libraries:

implementation 'jp.wasabeef:recyclerview-animators:3.0.0'
implementation 'com.ashokvarma.android:bottom-navigation-bar:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.ogaclejapan.smarttablayout:utils-v4:2.0.0@aar'
implementation 'com.duolingo.open:rtl-viewpager:1.0.3'
implementation 'com.squareup.retrofit2:retrofit-converters:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.1'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.karumi:dexter:5.0.0'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'org.jsoup:jsoup:1.11.3'
implementation 'saschpe.android:customtabs:2.0.0'

I'm creating a retrofit connection using this code

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_LINK)
            .addConverterFactory(GsonConverterFactory.create())
            .client(Utils.getHttpClientBuilder())
            .build();

And getting an instance of the builder using this code

public static OkHttpClient getHttpClientBuilder(){
    return new OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build();
}

The exception happens when calling "build()" in the getHttpClientBuilder() method.

I also have a default application assigned in the AndroidManifest.xml, it extends MultiDexApplication and has MultiDex.install(this); in onCreate(Bundle bundle).

The issue only happens in Kitkat. Any ideas why? I've tried downgrading Okhttp library and upgrading it to the latest version, it didn't work out.

EDIT:

Removing the client from the Retroft builder doesn't change anything. The error still shows.

like image 906
Jaeger Avatar asked May 05 '19 14:05

Jaeger


2 Answers

OkHttp 3.13+ Requires Android 5+

The square/okhttp github page mentions that the minimum requirement is Android 5+

Github link

Medium link

As mentioned by @Jaeger, @fancyjyl for using with versions less than Android 5 we need to use OkHttp 3.12.x but it is not recommended to use it.

like image 56
itabdullah Avatar answered Nov 03 '22 00:11

itabdullah


show app dependencies tree

+--- com.ogaclejapan.smarttablayout:utils-v4:2.0.0
+--- com.duolingo.open:rtl-viewpager:1.0.3
+--- com.squareup.retrofit2:retrofit-converters:2.5.0
+--- com.squareup.okhttp3:logging-interceptor:3.14.1
|    \--- com.squareup.okhttp3:okhttp:3.14.1 (*)
+--- com.squareup.okhttp3:okhttp:3.12.0 -> 3.14.1 (*)

you should use 3.12.0 logging-interceptor

implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
like image 25
fancyyou Avatar answered Nov 03 '22 01:11

fancyyou