InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.08k stars 719 forks source link

Koin in library project with Proguard #1724

Closed nono3551 closed 4 months ago

nono3551 commented 11 months ago

Koin in library project with Proguard I created kotlin library project and included:

val koin_ksp_version = "1.3.0"
api("io.insert-koin:koin-core:3.5.0")
api("io.insert-koin:koin-annotations:$koin_ksp_version")
ksp("io.insert-koin:koin-ksp-compiler:$koin_ksp_version")

I am using proguard with this library project. Problem is that when I include obfuscated library in other project I have problems with koin.

Problem 1: When I NO NOT add koin dependencies into my final project I get error:

Caused by: java.lang.NoClassDefFoundError: org/koin/core/context/DefaultContextExtKt
    at sk.backbone.license_manager.b.b.<clinit>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService.<init>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService.<init>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService$Builder.build(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.maindata.adcentral.api.ApiApplication.getLicenseService(ApiApplication.kt:39) ~[main/:na]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$0.CGLIB$getLicenseService$0(<generated>) ~[main/:na]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$2.invoke(<generated>) ~[main/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[spring-core-6.0.10.jar:6.0.10]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-6.0.10.jar:6.0.10]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$0.getLicenseService(<generated>) ~[main/:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 48 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.koin.core.context.DefaultContextExtKt
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 61 common frames omitted

Problem 2: I Add dependencies to library dependent project:

implementation("io.insert-koin:koin-core:3.5.0")
implementation("io.insert-koin:koin-annotations:$koin_ksp_version")

When I add koin dependencies into my final project I get error:

Caused by: java.lang.AbstractMethodError: Receiver class sk.backbone.license_manager.b.b$a does not define or inherit an implementation of the resolved method 'abstract java.lang.Object invoke(java.lang.Object)' of interface kotlin.jvm.functions.Function1.
    at org.koin.core.context.GlobalContext.startKoin(GlobalContext.kt:64) ~[koin-core-jvm-3.5.0.jar:na]
    at org.koin.core.context.DefaultContextExtKt.startKoin(DefaultContextExt.kt:40) ~[koin-core-jvm-3.5.0.jar:na]
    at sk.backbone.license_manager.b.b.<clinit>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService.<init>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService.<init>(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.backbone.license_manager.LicenseService$Builder.build(Unknown Source) ~[proguard-obfuscated.jar:na]
    at sk.maindata.adcentral.api.ApiApplication.getLicenseService(ApiApplication.kt:39) ~[main/:na]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$0.CGLIB$getLicenseService$0(<generated>) ~[main/:na]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$2.invoke(<generated>) ~[main/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[spring-core-6.0.10.jar:6.0.10]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-6.0.10.jar:6.0.10]
    at sk.maindata.adcentral.api.ApiApplication$$SpringCGLIB$$0.getLicenseService(<generated>) ~[main/:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 48 common frames omitted

Proguard File:

-verbose

-keepattributes Exceptions,InnerClasses,MethodParameters,*Annotation*,EnclosingMethod,Signature

-keepclassmembers class * { public <init>(...); }

-keep class kotlin.Metadata { *; }
-keep class kotlin.reflect.** { *; }

-keep class org.koin.** { *; }
-keep class org.koin.core.** { *; }
-keep class org.koin.dsl.** { *; }

-keep class com.fasterxml.jackson.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keep class org.codehaus.** { *; }
-keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {
    public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; }

-keep class sk.backbone.license_manager.exceptions.* { *; }
-keep class sk.backbone.license_manager.entities.license.* { *; }
-keepnames class sk.backbone.license_manager.entities.license.* { *; }
-keepclassmembers class sk.backbone.license_manager.entities.license.* { *; }
-keep class sk.backbone.license_manager.LicenseService { *; }
-keep interface sk.backbone.license_manager.IModulePropertyValidator { *; }
-keepnames interface sk.backbone.license_manager.IModulePropertyValidator { *; }
-keep class sk.backbone.license_manager.LicenseService$Builder { *; }
-keepnames class sk.backbone.license_manager.LicenseService$Builder { *; }

-dontwarn sk.backbone.**
-dontwarn org.koin.**
-dontwarn com.fasterxml.jackson.databind.**

-keep class com.fasterxml.** { *; }
-keepclassmembers class ** extends com.fasterxml.jackson.databind.ser.std.** {
   public <init>(...);
}

-keepclassmembers class ** extends com.fasterxml.jackson.databind.deser.std.** {
   public <init>(...);
}
 -keepnames class com.fasterxml.jackson.** { *; }
 -dontwarn com.fasterxml.jackson.databind.**
 -keep class org.codehaus.** { *; }
 -keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {
     public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; }
-keep public class your.class.** {
    *;
}
-dontwarn com.fasterxml.jackson.databind.*
arnaudgiuliani commented 9 months ago

I imagine you removed Koin and renabled it. Can you also remove the KSP part, just to check?

stale[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.