简体   繁体   中英

Fat jar problem with Ktor and Kotlin multiplatform

I have a kotlin multiplatform project targeting Android and iOS and recently integrated ktor so that the shared library can load web resources. It's working on iOS, but on Android I get this error:

java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/HttpClientJvmKt

I'm guessing my build pipeline is creating a jar that doesn't contain all the dependencies that ktor needs. So I'm trying to make a fat jar using the gradle shadow plugin ( com.github.jengelman.gradle.plugins:shadow:2.0.4 ). Here is my task:

task shadowJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
    from kotlin.targets.android.compilations.main.output
    def runtimeClasspath = kotlin.targets.android.compilations.main.runtimeDependencyFiles
    configurations = [runtimeClasspath]
    baseName = 'myproject-android-shadow'
    exclude 'META-INF/*'
    exclude '*.kotlin_metadata'
}

The jar jumped up from 300Kb to 8.5Mb.

The result is a new error: More than one file was found with OS independent path 'kotlinx/coroutines/CoroutineExceptionHandler.kotlin_metadata'

You'll notice I've tried excluding files from the shadowJar , but it's not working.

Although building a fat jar is possible, the easier solution was to include the missing dependencies ( ktor and kotlinx ) in the app directly. Then I had to add a bunch of exclude 'META-INF ... statements in packagingOptions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM