koin: KClass for Objective-C classes is not supported yet

Describe the bug The bug appears when you want to get a Objective-C class

Koin version 3.0.1-alpha-3

Edit: 3.1.0 remains the same Edit: 3.1.2 remains the same

a simple example to demonstrate

// works
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<Settings> { AppleSettings(userDefaults) }
    }
)

//fails
fun initIOS(userDefaults: NSUserDefaults) = initClient(
    module {
       single<NSUserDefaults> { userDefaults }
       single<Settings> { AppleSettings(get()) }
    }
)

It compiles fine but getting run time exception on IOS, the log below

Debug: (Kermit) starting koin
Function doesn't have or inherit @Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.IllegalStateException: KClass for Objective-C classes is not supported yet
    at 0   CCC                                 0x000000010b61082f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95
    at 1   CCC                                 0x000000010b608f8d kfun:kotlin.Exception#<init>(kotlin.String?){} + 93
    at 2   CCC                                 0x000000010b6091fd kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93
    at 3   CCC                                 0x000000010b6098ad kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} + 93
    at 4   CCC                                 0x000000010b6513cf kfun:kotlin.native.internal.KClassUnsupportedImpl#hashCode(){}kotlin.Int + 335
    at 5   CCC                                 0x000000010b8de31f kfun:org.koin.core.definition.BeanDefinition#hashCode(){}kotlin.Int + 383
    at 6   CCC                                 0x000000010b626072 kfun:kotlin.collections.HashMap.hash#internal + 226
    at 7   CCC                                 0x000000010b627736 kfun:kotlin.collections.HashMap#addKey(1:0){}kotlin.Int + 486
    at 8   CCC                                 0x000000010b6310ff kfun:kotlin.collections.HashSet#add(1:0){}kotlin.Boolean + 207
    at 9   CCC                                 0x000000010b8e77a7 kfun:org.koin.core.module#addDefinition@kotlin.collections.HashSet<org.koin.core.definition.BeanDefinition<*>>(org.koin.core.definition.BeanDefinition<*>){} + 439
    at 10  CCC                                 0x000000010b472354 kfun:com.github.mustafaozhan.ccc.client.di.initIOS$lambda-1#internal + 1252
    at 11  CCC                                 0x000000010b472541 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.invoke#internal + 97
    at 12  CCC                                 0x000000010b4725c1 kfun:com.github.mustafaozhan.ccc.client.di.$initIOS$lambda-1$FUNCTION_REFERENCE$52.$<bridge-UNNN>invoke(-1:0){}#internal + 97
    at 13  CCC                                 0x000000010b8fefed kfun:org.koin.dsl#module(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>){}org.koin.core.module.Module + 381
    at 14  CCC                                 0x000000010b8ff184 kfun:org.koin.dsl#module$default(kotlin.Boolean;kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>;kotlin.Int){}org.koin.core.module.Module + 276
    at 15  CCC                                 0x000000010b471a82 kfun:com.github.mustafaozhan.ccc.client.di#initIOS(platform.Foundation.NSUserDefaults){}org.koin.core.KoinApplication + 354
    at 16  CCC                                 0x000000010b5aaec8 objc2kotlin.3553 + 216
    at 17  CCC                                 0x000000010b2a43d2 $s3CCC6CCCAppVACycfC + 18
    at 18  CCC                                 0x000000010b2a4bc9 $s3CCC6CCCAppV7SwiftUI3AppAadEPxycfCTW + 9
    at 19  SwiftUI                             0x000000010eebc65f $s7SwiftUI3AppPAAE4mainyyFZ + 47
    at 20  CCC                                 0x000000010b2a4b61 $s3CCC6CCCAppV5$mainyyFZ + 33
    at 21  CCC                                 0x000000010b2a4be4 main + 20
    at 22  libdyld.dylib                       0x0000000112b6895d start + 1
    at 23  ???                                 0x0000000000000001 0x0 + 1
CoreSimulator 725.10 - Device: iPhone 11 Pro Max (1C7C1A0E-2FE5-47FF-AAB1-57530F0DAD34) - Runtime: iOS 14.0 (18A5319g) - DeviceType: iPhone 11 Pro Max

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Is this related to what I’ve encountered?

I can’t get from ios if the dependency is defined with interface:

single<TokenSource> {
        TokenSourceImpl()
} //this doesn't work


single {
        TokenSourceImpl()
} //this works

//kotlin function to get
fun Koin.get(objCClass: ObjCClass, parameter: Any): Any {
    val kClazz = getOriginalKotlinClass(objCClass)!!
    return get(kClazz) { parametersOf(parameter) }
}
//swift usage
lazy var tokenSource = koin.get(objCClass: TokenSourceImpl.self) as! TokenSourceImpl

Error: Uncaught Kotlin exception: org.koin.core.error.NoBeanDefFoundException: No definition found for class:‘com.foo.main.ktor.TokenSourceImpl’. Check your definitions!

@arnaudgiuliani sure thanks a lot! Finger crossed for 3.3.0 🤞 🙂