NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/Dispatchers
NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/Dispatchers
我有一个使用 Kotlin Coroutines 并将 CoroutineScope 用作
的库
private val coroutineScope by lazy(mode = LazyThreadSafetyMode.NONE) {
CoroutineScope(Dispatchers.Main)
}
当我在其他项目中使用该库时出现错误:
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]]
我也在我的 consumer pro guard 文件中添加了 -keepnames class kotlinx.** { *; }
。
有没有人有类似的问题?我在 android.
中使用协程 1.3.3
能否请您使用此版本并重试:
kotlin_coroutinesVersion = "1.2.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutinesVersion"
ClassNotFoundException 可能由于错误生成 pox.xml 文件而出现,其中包含库的依赖关系图,这里是一个用于将库发布到本地 maven repo 并生成 pom.xml 文件的脚本:
https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017
要将其应用到库中,需要将 publishMavenLocal.gradle 添加到库项目并将此行放在库的底部 build.gradle: apply from: 'publishMavenLocal.gradle'.
同样为了从本地 Maven 仓库测试它,在测试应用 gradle 文件中应该有额外的标识符 - transitive = true,像这样:
implementation ('com.example.android:library:0.0.1-library@aar') { transitive = true }
在上面@orest-herdil 发布的 link 的帮助下,我发现问题是我的库模块的依赖项没有添加到捆绑 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)
}
}
}
}
}
pom.withXml { .... } 部分将遍历我的库模块中的所有依赖项,并将它们添加到生成 POM一一归档。截至目前,编译已被弃用,我仅通过 实现 依赖。
我有一个使用 Kotlin Coroutines 并将 CoroutineScope 用作
的库private val coroutineScope by lazy(mode = LazyThreadSafetyMode.NONE) {
CoroutineScope(Dispatchers.Main)
}
当我在其他项目中使用该库时出现错误:
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]]
我也在我的 consumer pro guard 文件中添加了 -keepnames class kotlinx.** { *; }
。
有没有人有类似的问题?我在 android.
中使用协程 1.3.3能否请您使用此版本并重试:
kotlin_coroutinesVersion = "1.2.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutinesVersion"
ClassNotFoundException 可能由于错误生成 pox.xml 文件而出现,其中包含库的依赖关系图,这里是一个用于将库发布到本地 maven repo 并生成 pom.xml 文件的脚本:
https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017
要将其应用到库中,需要将 publishMavenLocal.gradle 添加到库项目并将此行放在库的底部 build.gradle: apply from: 'publishMavenLocal.gradle'.
同样为了从本地 Maven 仓库测试它,在测试应用 gradle 文件中应该有额外的标识符 - transitive = true,像这样:
implementation ('com.example.android:library:0.0.1-library@aar') { transitive = true }
在上面@orest-herdil 发布的 link 的帮助下,我发现问题是我的库模块的依赖项没有添加到捆绑 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)
}
}
}
}
}
pom.withXml { .... } 部分将遍历我的库模块中的所有依赖项,并将它们添加到生成 POM一一归档。截至目前,编译已被弃用,我仅通过 实现 依赖。