Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ExceptionInInitializerError On Android 7 #3336

Closed
yuyanghe opened this issue May 9, 2017 · 9 comments
Closed

java.lang.ExceptionInInitializerError On Android 7 #3336

yuyanghe opened this issue May 9, 2017 · 9 comments

Comments

@yuyanghe
Copy link

yuyanghe commented May 9, 2017

Hi We use OkHttp3.3.1 in our project. than I found some crash on Some Android 7.
it crash at ConnectionSpec$Builder.tlsVersions.
ths crash log is:
java.lang.ExceptionInInitializerError
at okhttp3.OkHttpClient.(Unknown Source)
at okhttp3.OkHttpClient.y(Unknown Source)
at okhttp3.OkHttpClient$Builder.(Unknown Source)
at com.gf.mobile.common.h.(Unknown Source)
at com.gf.mobile.common.h.b(Unknown Source)
at com.gf.mobile.common.h.a(Unknown Source)
at com.gf.mobile.control.a.l$1.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'okhttp3.ConnectionSpec$Builder okhttp3.ConnectionSpec$Builder.tlsVersions(okhttp3.TlsVersion[])' on a null object reference
at okhttp3.ConnectionSpec.(Unknown Source)
... 10 more

@swankjesse
Copy link
Collaborator

Can you provide more of the exception?

@yzbzz
Copy link

yzbzz commented May 12, 2017

I have also encountered this problem:
java.lang.ExceptionInInitializerError at okhttp3.internal.Util.a(Util.java:187) at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:121) at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:443) at com.ddu.base.network.okhttp.OKHttpManager.d(OKHttpManager.java:102) at com.ddu.base.network.okhttp.OKHttpManager.<init>(OKHttpManager.java:40) at com.ddu.base.network.okhttp.OKHttpManager.<init>(OKHttpManager.java:35) at com.ddu.base.network.okhttp.OKHttpManager$SingletonHolder.<clinit>(OKHttpManager.java:63) at com.ddu.base.network.okhttp.OKHttpManager$SingletonHolder.a(OKHttpManager.java:62) at com.ddu.base.network.okhttp.OKHttpManager.a(OKHttpManager.java:44) at com.ddu.base.network.okhttp.request.OKHttpRequest.<init>(OKHttpRequest.java:44) at com.ddu.base.network.okhttp.request.OKHttpGetRequest.<init>(OKHttpGetRequest.java:23) at com.ddu.base.network.okhttp.request.OKHttpGetRequest$Builder.a(OKHttpGetRequest.java:30) at com.ddu.base.api.OKHttpUtils.a(OKHttpUtils.java:49) at com.ddu.base.api.OKHttpUtils.a(OKHttpUtils.java:104) at com.ddu.base.api.dduHttpUtils.c(dduHttpUtils.java:163) at com.ddu.base.api.dduHttpUtils.b(dduHttpUtils.java:104) at com.ddu.base.api.dduHttpUtils.d(dduHttpUtils.java:72) at com.dduOwner.yc.activity.LogoActivity.d(LogoActivity.java:217) at com.dduOwner.yc.activity.LogoActivity.c(LogoActivity.java:208) at com.dduOwner.yc.activity.LogoActivity.onCreate(LogoActivity.java:108) at android.app.Activity.performCreate(Activity.java:6912) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at cn.jiguang.a.a.c.a.a.d.callActivityOnCreate(Unknown Source) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Caused by: java.lang.NullPointerException: source == null at okhttp3.ResponseBody.a(ResponseBody.java:215) at okhttp3.ResponseBody.a(ResponseBody.java:209) at okhttp3.internal.Util.<clinit>(Util.java:48) ... 33 more ,ver:4.7.0,sdk24,phone:hero2qltechn

There should be a problem with this line:
this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
OkHttpClient(Builder builder) { this.dispatcher = builder.dispatcher; this.proxy = builder.proxy; this.protocols = builder.protocols; this.connectionSpecs = builder.connectionSpecs; this.interceptors = Util.immutableList(builder.interceptors); this.networkInterceptors = Util.immutableList(builder.networkInterceptors); this.proxySelector = builder.proxySelector; this.cookieJar = builder.cookieJar; this.cache = builder.cache; this.internalCache = builder.internalCache; this.socketFactory = builder.socketFactory;

device name: Samsung Galaxy S7 edge
android Version: Android 7.0
model: SM-G9350

@yuyanghe
Copy link
Author

I have solve this problem. it caused by mutiDex. If you use Goolge MultiDex. please don't use MultiDex.install().if android sdk > 21

@yzbzz
Copy link

yzbzz commented May 12, 2017

my configuration:
defaultConfig {
versionName "4.7.0"
versionCode 56
minSdkVersion 17
targetSdkVersion 25
multiDexEnabled = true
}

@yzbzz
Copy link

yzbzz commented May 15, 2017

@yuyanghe Thank you for your answer. But my minSdkVersion is 17, What should we do?

@swankjesse
Copy link
Collaborator

Not sure what action for us to take on this. If there’s a specific bug, please provide an executable test case.

@PGMacDesign
Copy link

PGMacDesign commented Jul 14, 2019

I know this is an older issue, but I just inherited some legacy code that had this verbatim same issue and I resolved it on my end.

The core of the problem is that a NullPointerException is being thrown here:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at okhttp3.internal.tls.BasicTrustRootIndex.<init>(BasicTrustRootIndex.java:32)
        at okhttp3.internal.platform.Platform.buildTrustRootIndex(Platform.java:288)
        at okhttp3.internal.platform.AndroidPlatform.buildTrustRootIndex(AndroidPlatform.java:280)
        at okhttp3.internal.platform.Platform.buildCertificateChainCleaner(Platform.java:172)
        at okhttp3.internal.platform.AndroidPlatform.buildCertificateChainCleaner(AndroidPlatform.java:230)
        at okhttp3.internal.tls.CertificateChainCleaner.get(CertificateChainCleaner.java:41)
        at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.java:694)

This is being caused by the need (in this particular call) to allow all Certificates, in which I was using this code to generate the X509TrustManager in the builder:

                X509TrustManager trustManager = new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] xcs, String string)
                            throws CertificateException {}
                    public void checkServerTrusted(X509Certificate[] xcs, String string)
                            throws CertificateException {}
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                };

This is what a large number of StackOverflow posts recommend doing for this situation, but returning null in the getAcceptedIssuers() is why this is occurring. To fix this, simply change the code above to:

                X509TrustManager trustManager = new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] xcs, String string)
                            throws CertificateException {}
                    public void checkServerTrusted(X509Certificate[] xcs, String string)
                            throws CertificateException {}
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                };

I wrote up a StackOverflow Q/A here in case anyone needs to read it:
https://stackoverflow.com/q/57025434/2480714

@yschimke
Copy link
Collaborator

Please link to the Stackoverflow question

@PGMacDesign
Copy link

Sure, edited and included, thank you for reminding me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants