Fat jar problem with Ktor and Kotlin multiplatform

986 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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.