简体   繁体   中英

Duplicated class Koin org.koin and io.insert-koin

I'm using two library that have dependencies of two different versions of Koin.

One of the library has org.koin:koin-android:2.0.1 and the other has io.insert-koin:koin-core-jvm:3.0.1 dependencies.

At compile time I got tons of Duplicate class error as below,

Duplicate class org.koin.android.BuildConfig found in modules jetified-koin-android-2.0.1-runtime (org.koin:koin-android:2.0.1) and jetified-koin-android-3.0.1-runtime (io.insert-koin:koin-android:3.0.1)
Duplicate class org.koin.android.ext.koin.KoinExtKt$androidContext$1 found in modules jetified-koin-android-2.0.1-runtime (org.koin:koin-android:2.0.1) and jetified-koin-android-3.0.1-runtime (io.insert-koin:koin-android:3.0.1)
Duplicate class org.koin.core.scope.Scope$injectOrNull$1 found in modules jetified-koin-core-2.0.1 (org.koin:koin-core:2.0.1) and jetified-koin-core-jvm-3.0.1 (io.insert-koin:koin-core-jvm:3.0.1)
Duplicate class org.koin.java.KoinJavaComponent$inject$1 found in modules jetified-koin-core-jvm-3.0.1 (io.insert-koin:koin-core-jvm:3.0.1) and jetified-koin-java-2.0.1 (org.koin:koin-java:2.0.1)
...

When I try to exclude one of them as below,

configurations {
    all { 
        exclude group: "io.insert-koin", module: "koin-android"
        exclude group: "io.insert-koin", module: "koin-core-jvm"
    }
}

I got NoClassDefFoundError at runtime on the relevant one.

When exclude org.koin:koin-android:2.0.1

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.koin.core.KoinComponent" on path: DexPathList[[zip file "/data/app/...

When exclude io.insert-koin:koin-core-jvm:3.0.1

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.koin.core.component.KoinComponent" on path: DexPathList[[zip file "/data/app/...

How can I get to use both libraries?

Koin 3.xx has no backwards compatibility. If there is no update on the sdk which uses Koin 2.xx version, maybe you should better find an older version for the opposite library, which uses Koin 2.xx But please be aware Koin version 2.2.3 also has a different path than others (Because of jcenter limitations)

How can I get to use both libraries?

You can't use both versions of Koin at the same time, but you can tell Gradle to replace one version by the other at compile time. Note that it's not the same as excluding one of the versions altogether, but really replacing one by the other.

Here's an example:

eachDependency { details ->
                // change org.koin:koin-androidx-viewmodel into io.insert-koin:koin-android
                if (details.requested.group == 'org.koin' && details.requested.name == 'koin-androidx-viewmodel') {
                    details.useTarget group: 'io.insert-koin', name: 'koin-android', version: details.requested.version
                }
            }

You can read more in Gradle's dochttps://docs.gradle.org/current/userguide/resolution_rules.html

It is only a partial solution though (and a hacky one too), as we're still limited by the API incompatibilities between Koin 2.x and 3.x. But if what you're using from Koin 2.x is still there in Koin 3.x, that should work.

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