NoClassDefFoundError:解析失败:Lkotlinx/coroutines/Dispatchers

ama*_*pid 2 android android-library kotlin kotlin-coroutines

我有一个使用 Kotlin Coroutines 并使用 CoroutineScope 作为的库

private val coroutineScope by lazy(mode = LazyThreadSafetyMode.NONE) {
        CoroutineScope(Dispatchers.Main)
    }
Run Code Online (Sandbox Code Playgroud)

当我在其他项目中使用该库时,出现错误:

com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.Dispatchers" on path: DexPathList[[zip file "/data/app/com.ssss.ssss-X44QPiLhKdl-D6eyVAYkOA==/base.apk"],nativeLibraryDirectories=[/data/app/com.ssss.testauthenticater-X44QPiLhKdl-D6eyVAYkOA==/lib/x86, /system/lib, /system/product/lib]]
Run Code Online (Sandbox Code Playgroud)

我也添加-keepnames class kotlinx.** { *; } 了我的消费者专业防护文件。

有没有人有类似的问题?我在 android 中使用协程 1.3.3。

0xA*_*iHn 7

你能不能使用这个版本然后再试一次:

kotlin_coroutinesVersion = "1.2.1"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutinesVersion"
Run Code Online (Sandbox Code Playgroud)


ama*_*pid 2

经过一段时间后,在 @orest-herdil 上面发布的链接的帮助下,我发现问题是我的库模块的依赖项没有添加到捆绑 AAR 文件时生成的 POM 文件中。所以我将我的 Maven 发布代码更新为:

publishing {
    publications {
        aar(MavenPublication) {
            groupId "com.mylibrary"
            version "1.1.1"
            artifactId "awesome"

            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.each { dependency ->
                    final dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dependency.group)
                    dependencyNode.appendNode('artifactId', dependency.name)
                    dependencyNode.appendNode('version', dependency.version)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

pom.withXml { .... }部分将遍历我的库模块中的所有依赖项,并将它们一一添加到生成POM文件中。截至目前,编译已被弃用,我仅处理实现依赖项。