ktorio/ktor

Failed resolution of: Lio/ktor/client/HttpClientJvmKt;

VincentChen1212 opened this issue · 6 comments

Ktor Version and Engine Used (client or server and name)
latest.release

Describe the bug
java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/HttpClientJvmKt;

To Reproduce
In my K/N Project
I build a jar
Then this jar built to a aar with another jar/method/so...

When I use aar
I got error

I reference "#1039"
add

        exclude 'META-INF/kotlinx-io.kotlin_module'
        exclude 'META-INF/atomicfu.kotlin_module'
        exclude 'META-INF/kotlinx-coroutines-io.kotlin_module'
        exclude 'META-INF/kotlinx-coroutines-core.kotlin_module'
    }```
But still Error

**Expected behavior**


**Screenshots**

Could you please provide more build.gradle and fix the snippet?

Hi,
Thanks for your helping

In my IntelliJ K/N Project

kotlin {
    targets {
        ...

        jvm("android")

        ...
    }

    sourceSets {
        ...

        sourceSets["androidMain"].dependencies {
            implementation(kotlin("stdlib-jdk8"))
            implementation("io.ktor:ktor-client-android:latest.release")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:latest.release")
        }

        ...
    }
}

This project will gen a jar file

In my AndroidStudio AAR Library Project

android {
    compileSdkVersion(28)

    defaultConfig {
        minSdkVersion(21)
        ...
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            isDebuggable = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

    packagingOptions {
        exclude("META-INF/kotlinx-io.kotlin_module")
        exclude("META-INF/atomicfu.kotlin_module")
        exclude("META-INF/kotlinx-coroutines-io.kotlin_module")
        exclude("META-INF/kotlinx-coroutines-core.kotlin_module")
    }
}

dependencies {
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    ...
    implementation(files("libs/JarAndroid.jar"))
    implementation("io.ktor:ktor-client-android:latest.release")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3")
    ....
}

This project will gen a aar file

When I use this aar
it get " java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/HttpClientJvmKt;"

Thank you very much

I needed a POM file to get all the dependencies in order. To make it easier I used the maven-publish plugin and used the mavenLocal() to get my library.

as standard it uses a generated one named libraryname-unspecified.pom which probably doesn't include the dependencies needed for the libraries.

Maybe someone else can tell you more about it... it's just what i had find out having the problem

Having the exact same issue. Everything runs fine when using the demo app from within the multiplatform project. Once using the published .AAR (via maven-publish plugin) I get the same error in my app.

Edit: Turned out I forgot to add
implementation("io.ktor:ktor-client-android:1.3.2")
But when adding this dependency it just goes on:
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.ktor.client.features.json.JsonFeature"

Check the generated POM file if it includes all the dependencies, I had a lot of problems with the plugins not generating the dependencies right, a easy bypass is to add the dependencies to the main android app instead of the multiplatform library. (aar files don't include the dependencies, these are inside the .pom files)

I actually had an issue with the publishing task from maven. I defined the artifact myself pointing to the build/output folder. Instead it is necessary to use from(components["kotlin"]).

So here is the proper build.gradle publishing script from the MPP library:

plugins {
    id("com.android.library")
    kotlin("multiplatform")
    id("kotlinx-serialization")
    id("maven-publish")
}

kotlin {
  publishing {
        publications {
            val debugLibrary = "multiplatform-library-debug"
            register(debugLibrary, MavenPublication::class) {
                from(components["kotlin"])
                groupId = "com.example"
                artifactId = debugLibrary
                version = "0.0.1"
            }
        }
    }
}

Also do not forget to add

implementation("io.ktor:ktor-client-android:1.3.2")

to the consuming apps build.gradle file. Hope that helps someone