Okhttp.Builder 中的 ExceptionInInitializerError w/Multidex &奇巧 [英] ExceptionInInitializerError in Okhttp.Builder w/ Multidex & Kitkat

查看:166
本文介绍了Okhttp.Builder 中的 ExceptionInInitializerError w/Multidex &奇巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行 Kitkat 4.4.2 且启用 Multidex 的 VM 上遇到 ExceptionInInitializerError 异常.

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

我有以下库:

实现'jp.wasabeef:recyclerview-animators:3.0.0'实现 'com.ashokvarma.android:bottom-navigation-bar:2.1.0'实现 'com.squareup.retrofit2:converter-gson:2.5.0'实现 'com.ogaclejapan.smarttablayout:utils-v4:2.0.0@aar'实现 '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.12.0'实现 'com.karumi:dexter:5.0.0'实现 'com.nineoldandroids:library:2.4.0'实现 'org.jsoup:jsoup:1.11.3'实现 'saschpe.android:customtabs:2.0.0'

我正在使用此代码创建改造连接

Retrofit 改造 = new Retrofit.Builder().baseUrl(常量.API_LINK).addConverterFactory(GsonConverterFactory.create()).client(Utils.getHttpClientBuilder()).建造();

并使用此代码获取构建器的实例

public static OkHttpClient getHttpClientBuilder(){返回新的 OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS).connectTimeout(60, TimeUnit.SECONDS).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).建造();}

getHttpClientBuilder() 方法中调用build()"时会发生异常.

我还在 AndroidManifest.xml 中分配了一个默认应用程序,它扩展了 MultiDexApplication 并且在 onCreate(Bundle bundle) 中有 MultiDex.install(this);).

该问题仅发生在 Kitkat 中.任何想法为什么?我试过降级 Okhttp 库并将其升级到最新版本,但没有成功.

从 Retroft 构建器中删除客户端不会改变任何内容.错误仍然显示.

解决方案

OkHttp 3.13+ 需要 Android 5+

square/okhttp github页面提到最低要求是Android 5+

Github 链接

媒体链接

正如@Jaeger 提到的,@fancyjyl 对于低于 Android 5 的版本我们需要使用 OkHttp 3.12.x,但不建议使用它.

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.

解决方案

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.

这篇关于Okhttp.Builder 中的 ExceptionInInitializerError w/Multidex &amp;amp;奇巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆