https://kotlinlang.org logo
#ktor
Title
# ktor
k

KamilH

02/09/2021, 4:36 PM
I’ve got a Kotlin library that uses Ktor as a http client. When I’m trying to use it in the Java (Android) project I’m getting a
java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/features/json/JsonFeature;
exception, even though I can see that library’s pom file contains all of the required dependencies and my consuming app is downloading those dependencies. Library is working well in the Kotlin project. Do I need to add some kind of a configuration to be able to use it from Java app?
a

andylamax

02/09/2021, 4:36 PM
how did you include it in your android lib?
k

KamilH

02/09/2021, 4:42 PM
Maybe I wasn’t clear enough, so I’ve got a multiplatform library project that contains 2 targets: android and iOS. I’m distributing it separately (not as a multiplatform library), as an Android library via JCenter and as an iOS framework via Cocoapods. When I’m trying to integrate it in Kotlin’s Android app it works well. When I’m trying to integrate it in Java only Android app it compile but it’s failing with the exception I put above
a

andylamax

02/09/2021, 4:48 PM
You were clear enough. its just that only including
io.ktor:ktor-client-core
is not enought. You need to specify a specific client for android. either
io.ktor:ktor-client-android
or
io.ktor:ktor-client-okhttp
e.t.c. Now, which one did you include in your gradle build script?
k

KamilH

02/09/2021, 4:51 PM
Ah, I’m sorry I didn’t understand the question. So library itself uses:
Copy code
io.ktor:ktor-client-core
io.ktor:ktor-client-json
io.ktor:ktor-client-logging
io.ktor:ktor-client-serialization
io.ktor:ktor-client-tests
io.ktor:ktor-client-android
All of those dependencies are getting included in the pom file as well
a

andylamax

02/09/2021, 5:03 PM
At this point, I would be of more help if I could see your
build.gradle(.kts)
configurations
j

Joost Klitsie

02/09/2021, 5:13 PM
Maybe you can try upgrading ktor to latest, I think I had a similar issue
a

andylamax

02/09/2021, 5:14 PM
Well, the script looks okay. Maybe just try setting android to target java 1.8? Also try updating ktor to 1.5.1
k

KamilH

02/09/2021, 5:15 PM
I tried ktor 1.5.1 and beside that I had to add
Copy code
packagingOptions {
        exclude("META-INF/ktor-http.kotlin_module")
        exclude("META-INF/ktor-utils.kotlin_module")
        exclude("META-INF/ktor-io.kotlin_module")
        exclude("META-INF/ktor-http-cio.kotlin_module")
        exclude("META-INF/ktor-client-core.kotlin_module")
    }
in a consumer app, it didn’t solve a problem unfortunately
a

andylamax

02/09/2021, 5:17 PM
then try android to target java 1.8 Also update your AGP at least go with 4.2.0?
I would start with AGP. Coz I remember reading somewhere that starting with AGP 4, android will default to target java 1.8
k

KamilH

02/09/2021, 5:19 PM
Sure, thanks I will try and get back if it helped
b

Big Chungus

02/09/2021, 5:25 PM
Your Library deps need to be declared as api instead as implementation.
This way they'll be downloaded on consumer project too
k

KamilH

02/09/2021, 6:29 PM
Thank you for suggestions. I applied suggested changes in this commit: https://github.com/audioburst-labs/AudioburstMobileLibrary/commit/21d093c39e15605cb2ad9f18c7d8958859c32ca6 unfortunately it didn’t solve a problem
j

Joost Klitsie

02/09/2021, 7:06 PM
Could it be because you use jvm and amdroid targets at the same time?
👍 1
k

KamilH

02/09/2021, 7:24 PM
Thanks, I checked it as well and unfortunately it didn’t solve a problem
To be specific, I removed a jvm target completely, because I used it only for debugging purpose, but it didn’t help
I have found those two similar issues: https://github.com/ktorio/ktor/issues/1608 https://github.com/ktorio/ktor/issues/1039 and tried workarounds included there (I added ktor dependencies to the consuming app), but unfortunately again without any luck
320 Views