【问题标题】:java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.experimental.Deferred"java.lang.ClassNotFoundException:找不到类“kotlinx.coroutines.experimental.Deferred”
【发布时间】:2019-05-11 01:01:59
【问题描述】:

我在使用带有改造的协程时遇到了这个问题,你能告诉我为什么会出现这个错误

java.lang.ClassNotFoundException:在路径上找不到类“kotlinx.coroutines.experimental.Deferred”:DexPathList[[zip 文件“/data/app/com.coroutines.retrofit.kotlin-1/ base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

当我使用以下依赖项时,

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

实现 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1' 实现 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

而 kotlin 版本是: ext.kotlin_version = '1.3.10'

【问题讨论】:

  • 为什么要使用实验版?
  • 我尝试使用 ext.kotlin_version = '1.3.0' 但对于 Deferred,我仍然遇到相同的错误,api 服务是:@Headers("Content-Type: application/json") @GET( ApiClient.POSTS_URL) 有趣的 getPosts(): Deferred>>

标签: android kotlin retrofit coroutine


【解决方案1】:

在您的 build.gradle 中添加此依赖项:(删除协程的实验性依赖项)

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

并添加 CoroutineCallAdapterFactory() 进行改造

addCallAdapterFactory(CoroutineCallAdapterFactory())

【讨论】:

    【解决方案2】:

    我面临同样的问题,这是 Jake Warton 的图书馆问题。它在内部使用实验参考。

    使用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
    

    此库已弃用。这是 git 上图书馆页面的解决方案

    此库已弃用。请迁移到 Retrofit 2.6.0 或更新版本及其内置的挂起支持

    【讨论】:

    • 但是我们不再添加 addCallAdapterFactory 了吗?
    • 我相信没有必要,改造调用只会有暂停修饰符,你可以正常启动它们。如果开箱即用,不知道为什么人们仍然希望将另一个库和适配器工厂添加到您的项目中:)
    • 没有更多的升级到最新的改造和爆炸......问题解决......非常感谢老兄
    【解决方案3】:

    有效:(注意jar任务)

    plugins {
        application
        kotlin("jvm") version "1.6.10"
    }
    
    group = "org.example"
    version = "1.0-SNAPSHOT"
    
    application {
        mainClass.set("org.example.MainKt")
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation("io.ktor:ktor-client-core-jvm:1.6.7")
        implementation("io.ktor:ktor-client-cio-jvm:1.6.7")
    }
    
    tasks {
        jar {
            manifest {
                attributes["Main-Class"] = application.mainClass
            }
    
            duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    
            configurations.compileClasspath.get().forEach {
                from(if (it.isDirectory) it else zipTree(it))
            }
        }
    
        compileKotlin {
            kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.ExperimentalStdlibApi"
            kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 2021-08-30
      • 2018-04-04
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多