Retrofit: Retrofit Crash after Android Studio Updates

Created on 4 Mar 2019  路  7Comments  路  Source: square/retrofit

Hi. I am having a problem with retrofit crashing my app after some recent Android Studio updates. My trouble is the error is very generic and I'm looking for some help. I have tried a number of solutions from here and stack overflow with no luck so I am posting this in hope you might offer some suggestions.

Here is my code that is crashing and the logcat output below. As I mentioned, this was all previously working before I updated Android. BASE_URL in my class is like this: BASE_URL = "http://myurl.com/".

public static Retrofit getClient() {
if (mRetrofit ==null) {
Gson gson = new GsonBuilder()
.setLenient()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return mRetrofit;
}

2019-03-04 07:22:41.112 28169-28169/com.instreamwater.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.instreamwater.app, PID: 28169
java.lang.BootstrapMethodError: Exception from call site #0 bootstrap method
at okhttp3.internal.Util.(Unknown Source:111)
at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Unknown Source:0)
at okhttp3.HttpUrl$Builder.parse(Unknown Source:11)
at okhttp3.HttpUrl.get(Unknown Source:6)
at retrofit2.Retrofit$Builder.baseUrl(Unknown Source:5)
at com.instreamwater.app.network.ApiClient.getClient(Unknown Source:30)
at com.instreamwater.app.b.t.ai(Unknown Source:83)
at com.instreamwater.app.b.t.a(Unknown Source:0)
at com.instreamwater.app.b.t$5.onClick(Unknown Source:16)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null

Most helpful comment

You have updated OkHttp to version 3.13 or newer and per its release notes, OkHttp now requires that you enable Java 8 in your builds to function. You can learn more about how to enable this at https://developer.android.com/studio/write/java8-support.

You need to add something like

android {
    compileOptions {
        targetCompatibility = "8"
        sourceCompatibility = "8"
    }
}

to your application build.gradle.

All 7 comments

You have updated OkHttp to version 3.13 or newer and per its release notes, OkHttp now requires that you enable Java 8 in your builds to function. You can learn more about how to enable this at https://developer.android.com/studio/write/java8-support.

You need to add something like

android {
    compileOptions {
        targetCompatibility = "8"
        sourceCompatibility = "8"
    }
}

to your application build.gradle.

Jake - thank you very much. That was my issue and this fixed it. I really appreciate it.

I have
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
in my build.gradle but this does not resolve the issue for me. Am I doing something wrong?

The only way it worked for me was to use it like this:

compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}

That is not what you listed so I would suggest trying that and see if that fixes it.

@JakeWharton @toddcplayer well, I have the same problem and I did allow Java8 in my build.file but still crashing. I'm pretty sure it's because of my minSdkVersion = 19 and OkHttp version 3.13 support 21+.
I don't how to force Retrofit to use an older version of OkHttp like 3.12.1 ?

I had the same issue even after I added the solution from JakeWharton, but I still got same exception error. It works now after I downgraded retofit from "2.7.0" to "2.6.0".

Retrofit Gson error

* *SOLVED**

I was having the same issue

2020-02-26 22:42:11.958 6985-6985/com.example.retrofitgliderecycler E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.retrofitgliderecycler, PID: 6985
java.lang.BootstrapMethodError: Exception from call site #6 bootstrap method
at okhttp3.internal.Util.(Util.java:87)
at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
at okhttp3.HttpUrl.get(HttpUrl.java:917)
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)
at com.example.retrofitgliderecycler.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Activity.performCreate(Activity.java:7131)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at okhttp3.internal.Util.(Util.java:87)聽
at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)聽
at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)聽
at okhttp3.HttpUrl.get(HttpUrl.java:917)聽
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)聽
at com.example.retrofitgliderecycler.MainActivity.onCreate(MainActivity.java:46)聽
at android.app.Activity.performCreate(Activity.java:7140)聽
at android.app.Activity.performCreate(Activity.java:7131)聽
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)聽
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)聽
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)聽
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)聽
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)聽
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)聽
at android.os.Handler.dispatchMessage(Handler.java:106)聽
at android.os.Looper.loop(Looper.java:193)聽
at android.app.ActivityThread.main(ActivityThread.java:6692)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)聽

RESOLUTION :------------------------------------------------------------

Change
Retrofit ### retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();

ALSO TAKE NOTE OF implementation 'com.squareup.retrofit2:retrofit:2.7.1'
as when we use 2.7.2 or 2.7.1 it showing the error

TO---------------
_I am using implementation 'com.squareup.retrofit2:retrofit:2.5.0'_

/* use ScalarsConverterFactory instead of GsonFactoryConverter */

`
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.build();

`

Was this page helpful?
0 / 5 - 0 ratings