https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

aliftc12

10/18/2023, 2:06 AM
Hello everyone, I am working on private multiplatform library that depends on ktor. When the lib is consumed by the android app client, it always getting this error
Copy code
java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/engine/okhttp/OkHttp;
this is my pom.xml
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0>" xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <https://maven.apache.org/xsd/maven-4.0.0.xsd>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>kmm</artifactId>
  <version>dev</version>
  <packaging>aar</packaging>
  <dependencies>
    <dependency>
      <groupId>io.insert-koin</groupId>
      <artifactId>koin-core</artifactId>
      <version>3.2.2</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-core</artifactId>
      <version>2.3.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-encoding</artifactId>
      <version>2.3.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-content-negotiation</artifactId>
      <version>2.3.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-serialization-kotlinx-json</artifactId>
      <version>2.3.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-logging</artifactId>
      <version>2.3.1</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlinx</groupId>
      <artifactId>kotlinx-datetime</artifactId>
      <version>0.4.0</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.vidio.kmm.annotations</groupId>
      <artifactId>annotations</artifactId>
      <version>dev</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.rickclephas.kmp</groupId>
      <artifactId>kmp-nativecoroutines-core</artifactId>
      <version>0.13.3</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.rickclephas.kmp</groupId>
      <artifactId>kmp-nativecoroutines-annotations</artifactId>
      <version>0.13.3</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>
Do you have any idea to fix the issue?
p

Pablichjenkov

10/18/2023, 2:24 AM
Are you using maven to build the Android project? - interesting 🤔 It seems you are not adding the okhttp client engine dependency in your project. You can use
ktor-client-android
instead of okhttp I believe.
a

aliftc12

10/18/2023, 2:35 AM
Hi @Pablichjenkov, Im using gradle with maven local as repository. somehow removing the scope is solving the issue, but the ktor api is exposed to my android client (not intended). I do not want the consumer of my library knows that I am using ktor on KMM.
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0>" xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <https://maven.apache.org/xsd/maven-4.0.0.xsd>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>kmm</artifactId>
  <version>dev</version>
  <packaging>aar</packaging>
  <dependencies>
    <dependency>
      <groupId>io.insert-koin</groupId>
      <artifactId>koin-core</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-core</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-encoding</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-content-negotiation</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-serialization-kotlinx-json</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>io.ktor</groupId>
      <artifactId>ktor-client-logging</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlinx</groupId>
      <artifactId>kotlinx-datetime</artifactId>
      <version>0.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.vidio.kmm.annotations</groupId>
      <artifactId>annotations</artifactId>
      <version>dev</version>
    </dependency>
    <dependency>
      <groupId>com.rickclephas.kmp</groupId>
      <artifactId>kmp-nativecoroutines-core</artifactId>
      <version>0.13.3</version>
    </dependency>
    <dependency>
      <groupId>com.rickclephas.kmp</groupId>
      <artifactId>kmp-nativecoroutines-annotations</artifactId>
      <version>0.13.3</version>
    </dependency>
  </dependencies>
</project>
e

ephemient

10/18/2023, 2:39 AM
runtime-only dependencies don't work on native
why are you creating pom files anyway though? kmp should publish gradle modules with gradle module metadata
a

aliftc12

10/18/2023, 2:47 AM
Hi @ephemient, I just take example from another KMP lib like ktorfit. https://repo1.maven.org/maven2/de/jensklingenberg/ktorfit/ktorfit-lib-jvm/1.8.1/ktorfit-lib-jvm-1.8.1.pom I though it is the way to define the transitive deps of my library.
e

ephemient

10/18/2023, 2:50 AM
define
implementation
or
api
in your gradle build script and let the kotlin and gradle handle generating the pom and metadata
until https://youtrack.jetbrains.com/issue/KT-27219 is resolved, your downstream native or multiplatform consumers are going to see your dependencies, there's no way around that
👍 2
android/jvm-only consumers should follow the scopes though
p

Pablichjenkov

10/18/2023, 3:00 AM
I see, interesting. I actually don't recall the maven scope rules off the top of my head. I would do gradle which has better support and greater kmp community
a

aliftc12

10/18/2023, 5:03 AM
Thanks for the helps. TIL about gradle modules with gradle module metadata. 🙏
👍 1