6
votes

I am facing this issue when using coroutine with retrofit, can you please let me know why i am getting this error

java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.experimental.Deferred" on path: DexPathList[[zip file "/data/app/com.coroutines.retrofit.kotlin-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

while i am using the below dependencies,

implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

and the kotlin version is : ext.kotlin_version = '1.3.10'

2
Why you are using experimental version? - Shweta Chauhan
I tried with ext.kotlin_version = '1.3.0' but still i got same error for Deferred, The api service is : @Headers("Content-Type: application/json") @GET(ApiClient.POSTS_URL) fun getPosts(): Deferred<Response<List<Posts>>> - Hemendra Gangwar

2 Answers

14
votes

Add this dependency in your build.gradle : (remove experimental dependency for coroutine)

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
    implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
}

And add CoroutineCallAdapterFactory() for retrofit

addCallAdapterFactory(CoroutineCallAdapterFactory())
1
votes

I am facing the same problem, it is the Jake Warton's library problem. It is using experimental references internally.

Using implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'

java.lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/experimental/Deferred;
    at com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory.get

This library is deprecated . Here is the solution from the library's page on git

This library is deprecated. Please migrate to Retrofit 2.6.0 or newer and its built-in suspend support