question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExceptionInitializerError with new Android 11 version (DP1)

See original GitHub issue

OKHTTP 3.14.4 Retrofit 2.7.1 Device: Pixel 3 XL Android Build: crosshatch-rpp1.200123.016-factory-5808a1e6.zip

Note: Issue is not reproducible on Android 10 or earlier versions.

Found this issue when running my sample app using latest version of Retrofit (2.7.1) and targeting newly released Android 11 (targetSdkVersion = 30).

When attempting to run the app in Android 11, the app immediately crashes with the following exception:

2020-02-20 14:26:08.386 22962-22962/com.my.example.retrofit2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.my.example.retrofit2, PID: 22962 java.lang.ExceptionInInitializerError at okhttp3.internal.platform.Platform.get(Platform.java:85) at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263) at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229) at okhttp3.OkHttpClient.<init>(OkHttpClient.java:202) at retrofit2.Retrofit$Builder.build(Retrofit.java:614) at com.my.example.retrofit2.network.RetrofitInstance.getRetrofitInstance(RetrofitInstance.java:37) at com.my.example.retrofit2.main.IntractorImpl.getMarsPhotosArrayList(IntractorImpl.java:21) at com.my.example.retrofit2.main.MainPresenterImpl.requestDataFromServer(MainPresenterImpl.java:29) at com.my.example.retrofit2.main.MainActivity.onCreate(MainActivity.java:37) at android.app.Activity.performCreate(Activity.java:7970) at android.app.Activity.performCreate(Activity.java:7959) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3480) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2041) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7472) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956) Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 29 at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238) at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202) at okhttp3.internal.platform.Platform.<clinit>(Platform.java:79) at okhttp3.internal.platform.Platform.get(Platform.java:85)  at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263)  at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229)  at okhttp3.OkHttpClient.<init>(OkHttpClient.java:202)  at retrofit2.Retrofit$Builder.build(Retrofit.java:614)  at com.my.example.retrofit2.network.RetrofitInstance.getRetrofitInstance(RetrofitInstance.java:37)  at com.my.example.retrofit2.main.IntractorImpl.getMarsPhotosArrayList(IntractorImpl.java:21)  at com.my.example.retrofit2.main.MainPresenterImpl.requestDataFromServer(MainPresenterImpl.java:29)  at com.my.example.retrofit2.main.MainActivity.onCreate(MainActivity.java:37)  at android.app.Activity.performCreate(Activity.java:7970)  at android.app.Activity.performCreate(Activity.java:7959)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3480)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2041)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:216)  at android.app.ActivityThread.main(ActivityThread.java:7472)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)

My retrofit code is very simple and not attempting to do any custom initialization or setting a different OKHTTP client (see code for reference below):

`    public static Retrofit getRetrofitInstance(){

        if(retrofit == null){
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }`

After doing some digging into the OKHTTP code, noticed the following warning coming from adb logcat:

2020-02-20 14:26:08.382 22962-22962/com.my.example.retrofit2 D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2020-02-20 14:26:08.384 22962-22962/com.my.example.retrofit2 W/DebugTag: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setUseSessionTickets(Z)V (greylist-max-q,core-platform-api, reflection, denied)

Per official documentation found here, Android 11 no longer allows Reflection API calls to setUseSessionTickets, which appears to be used in a few places when initializing AndroidSocketAdapter:

https://github.com/square/okhttp/blob/ff0dee1c34fc06084357c93dc480f6ad89fef24a/okhttp/src/main/java/okhttp3/internal/platform/android/AndroidSocketAdapter.kt#L34

Looking at the other reflection calls done in the AndroidSocketAdapter, the following methods have also been blacklisted in Android 11 and will cause exceptions to be thrown:

1.   private val getAlpnSelectedProtocol = sslSocketClass.getMethod("getAlpnSelectedProtocol")
2.   private val setHostname = sslSocketClass.getMethod("setHostname", String::class.java)

Looks like OKHTTP will need to avoid these reflection calls and instead use available public APIs in Android 11 where applicable. Until then, applications may experience Fatal Runtime Exceptions when initializing OKHTTP. I noticed there is an Android10SocketAdapter, perhaps whats needed here would be an Android11SocketAdapter with specific implementation around these changes?

Let me know if more information is needed.

Best, Alex

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Feb 21, 2020

In addition to the two others mentioned setHostname and getAlpnSelectedProtocol, setAlpnProtocols is used which is also on the list.

Their replacements are:

Lcom/android/org/conscrypt/AbstractConscryptSocket;->getAlpnSelectedProtocol()[B   # Use javax.net.ssl.SSLSocket#getApplicationProtocol().
Lcom/android/org/conscrypt/ConscryptFileDescriptorSocket;->setHostname(Ljava/lang/String;)V   # Use javax.net.ssl.SSLParameters#setServerNames.
Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([Ljava/lang/String;)V   # Use javax.net.ssl.SSLParameters#setApplicationProtocols(java.lang.String[]).

The ALPN ones are a direct replacement, the setHostname one is a bit more complicated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

App crashing on android 11 : java.lang ... - Stack Overflow
thats a bug in OkHttp library, upgrade it to at least 4.9.0 version (preferably newest one).
Read more >
ExceptionInInitializerError - Android Developers
Constructs a new ExceptionInInitializerError class by saving a reference to the Throwable object thrown for later retrieval by the getException() method.
Read more >
Could you fix Pre-launch report false-positives ... - Issue Tracker
Another new Error; this one coming from my process, but only android classes in stack trace. is it caused by me app or...
Read more >
[Q&A] Issues with Substratum? Post here! | Page 267
I'm running android 9 on poco f1 with carbon rom and root. Anyone can help me please? I copy my logchar. » [com.topjohnwu.magisk]:...
Read more >
JetBrains IDEs do not work with pure Wayland (i.e. without ...
Also, much of the new improvements are done on Wayland these days, ... built on January 26, 2021 Runtime version: 11.0.9.1+11-b1145.77 amd64 VM:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found