InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
162 stars 44 forks source link

Compile safety throws errors for Android Project #96

Closed sarn0ld closed 9 months ago

sarn0ld commented 1 year ago

Describe the bug I tried the new ksp flag "KOIN_CONFIG_CHECK" in an Android project.

Now I get compilation errors for Android specific classes:

e: [ksp] --> Missing Definition type 'android.content.Context' for '***'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'androidx.lifecycle.SavedStateHandle' for '***'. Fix your configuration to define type 'SavedStateHandle'.

To Reproduce Steps to reproduce the behavior:

  1. Add the ksp argument KOIN_CONFIG_CHECK to an Android project that uses Context and SavedStateHandle in it's dependency tree
  2. Click on compile
  3. See Compilation errors in the build output

Expected behavior No compile errors

Koin module and version:

    implementation "io.insert-koin:koin-androidx-compose:3.5.0"
    implementation "io.insert-koin:koin-annotations:1.3.0"
    testImplementation "io.insert-koin:koin-test:3.5.0"
    testImplementation "io.insert-koin:koin-test-junit5:3.5.0"
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1'
    ksp 'io.insert-koin:koin-ksp-compiler:1.3.0'
alexandrucaraus commented 1 year ago

I have the same problem when using annotations and have the KOIN_CONFIG_CHECK enabled.

ghasemdev commented 11 months ago

I create an PR https://github.com/InsertKoinIO/koin-annotations/pull/101

I-sty commented 11 months ago
e: [ksp] --> Missing Definition type 'android.app.Application' for '***'. Fix your configuration to define type 'Application'.
e: [ksp] --> Missing Definition type 'kotlinx.coroutines.CoroutineDispatcher' for '***'. Fix your configuration to define type 'CoroutineDispatcher'.
e: [ksp] --> Missing Definition type 'android.content.Context' for '***'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'kotlin.String' for '***'. Fix your configuration to define type 'String'.
e: [ksp] --> Missing Definition type 'android.content.SharedPreferences' for '***'. Fix your configuration to define type 'SharedPreferences'.
e: [ksp] --> Missing Definition type 'androidx.work.WorkManager' for '***'. Fix your configuration to define type 'WorkManager'.
e: [ksp] --> Missing Definition type 'androidx.work.OneTimeWorkRequest' for '***'. Fix your configuration to define type 'OneTimeWorkRequest'.

I have a few more issues with this ticket.

cc: @ghasemdev

ghasemdev commented 11 months ago
e: [ksp] --> Missing Definition type 'android.app.Application' for '***'. Fix your configuration to define type 'Application'.
e: [ksp] --> Missing Definition type 'kotlinx.coroutines.CoroutineDispatcher' for '***'. Fix your configuration to define type 'CoroutineDispatcher'.
e: [ksp] --> Missing Definition type 'android.content.Context' for '***'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'kotlin.String' for '***'. Fix your configuration to define type 'String'.
e: [ksp] --> Missing Definition type 'android.content.SharedPreferences' for '***'. Fix your configuration to define type 'SharedPreferences'.
e: [ksp] --> Missing Definition type 'androidx.work.WorkManager' for '***'. Fix your configuration to define type 'WorkManager'.
e: [ksp] --> Missing Definition type 'androidx.work.OneTimeWorkRequest' for '***'. Fix your configuration to define type 'OneTimeWorkRequest'.

I have a few more issues with this ticket.

cc: @ghasemdev

I am also experiencing this issue in the sample project I’m working on when using the regular provide module { }.

e: [ksp] --> Missing Definition type 'com.parsuomash.sdk.di.Token' for 'com.parsuomash.sdk.domain.usecase.Koo'. Fix your configuration to define type 'Token'.
e: [ksp] --> Missing Definition type 'kotlinx.coroutines.CoroutineDispatcher' for 'com.parsuomash.sdk.domain.usecase.UseCase'. Fix your configuration to define type 'CoroutineDispatcher'.
e: [ksp] --> Missing Definition type 'android.content.SharedPreferences' for 'com.parsuomash.sdk.domain.usecase.UseCase'. Fix your configuration to define type 'SharedPreferences'.
e: [ksp] --> Missing Definition type 'android.app.Application' for 'com.parsuomash.sdk.di.Session'. Fix your configuration to define type 'Application'.
e: [ksp] --> Missing Definition type 'android.content.Context' for 'com.parsuomash.sdk.di.Session'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'androidx.lifecycle.SavedStateHandle' for 'com.parsuomash.sdk.SDKViewModel'. Fix your configuration to define type 'SavedStateHandle'.
e: [ksp] --> Missing Definition type 'com.parsuomash.sdk.SDK' for 'com.parsuomash.sdk.SDKViewModel'. Fix your configuration to define type 'SDK'.
internal val tokenModule = module {
  factory {
    Token("dkjgfiuebvldvyl")
  }
}

@JvmInline
value class Token(val value: String)

internal val coroutinesModule = module {
  single {
    Dispatchers.IO
  } withOptions {
    named("IO")
    createdAtStart()
  }

  single {
    Dispatchers.Default
  } withOptions {
    named("Default")
    createdAtStart()
  }

  single {
    Dispatchers.Main
  } withOptions {
    named("Main")
    createdAtStart()
  }
}

internal val sharedPrefModule = module {
  single {
    provideSharedPref(androidContext())
  } withOptions {
    named("SDKSharedPref")
    createdAtStart()
  }
}

private fun provideSharedPref(
  context: Context,
  name: String = context.packageName
): SharedPreferences = context.getSharedPreferences(name, MODE_PRIVATE)

private val sdkModule = module {
    single {
      provideSDK(androidContext()) {
        token = sharedPreferences.getString("token", "null") ?: "null"
      }
    } withOptions {
      createdAtStart()
    }
  }
ghasemdev commented 11 months ago

Can you send the codes for providing the WorkManager & OneTimeWorkRequest and where you used it?

@I-sty

ghasemdev commented 11 months ago
e: [ksp] --> Missing Definition type 'android.app.Application' for '***'. Fix your configuration to define type 'Application'.
e: [ksp] --> Missing Definition type 'kotlinx.coroutines.CoroutineDispatcher' for '***'. Fix your configuration to define type 'CoroutineDispatcher'.
e: [ksp] --> Missing Definition type 'android.content.Context' for '***'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'kotlin.String' for '***'. Fix your configuration to define type 'String'.
e: [ksp] --> Missing Definition type 'android.content.SharedPreferences' for '***'. Fix your configuration to define type 'SharedPreferences'.
e: [ksp] --> Missing Definition type 'androidx.work.WorkManager' for '***'. Fix your configuration to define type 'WorkManager'.
e: [ksp] --> Missing Definition type 'androidx.work.OneTimeWorkRequest' for '***'. Fix your configuration to define type 'OneTimeWorkRequest'.

I have a few more issues with this ticket. cc: @ghasemdev

I am also experiencing this issue in the sample project I’m working on when using the regular provide module { }.

e: [ksp] --> Missing Definition type 'com.parsuomash.sdk.di.Token' for 'com.parsuomash.sdk.domain.usecase.Koo'. Fix your configuration to define type 'Token'.
e: [ksp] --> Missing Definition type 'kotlinx.coroutines.CoroutineDispatcher' for 'com.parsuomash.sdk.domain.usecase.UseCase'. Fix your configuration to define type 'CoroutineDispatcher'.
e: [ksp] --> Missing Definition type 'android.content.SharedPreferences' for 'com.parsuomash.sdk.domain.usecase.UseCase'. Fix your configuration to define type 'SharedPreferences'.
e: [ksp] --> Missing Definition type 'android.app.Application' for 'com.parsuomash.sdk.di.Session'. Fix your configuration to define type 'Application'.
e: [ksp] --> Missing Definition type 'android.content.Context' for 'com.parsuomash.sdk.di.Session'. Fix your configuration to define type 'Context'.
e: [ksp] --> Missing Definition type 'androidx.lifecycle.SavedStateHandle' for 'com.parsuomash.sdk.SDKViewModel'. Fix your configuration to define type 'SavedStateHandle'.
e: [ksp] --> Missing Definition type 'com.parsuomash.sdk.SDK' for 'com.parsuomash.sdk.SDKViewModel'. Fix your configuration to define type 'SDK'.
internal val tokenModule = module {
  factory {
    Token("dkjgfiuebvldvyl")
  }
}

@JvmInline
value class Token(val value: String)

internal val coroutinesModule = module {
  single {
    Dispatchers.IO
  } withOptions {
    named("IO")
    createdAtStart()
  }

  single {
    Dispatchers.Default
  } withOptions {
    named("Default")
    createdAtStart()
  }

  single {
    Dispatchers.Main
  } withOptions {
    named("Main")
    createdAtStart()
  }
}

internal val sharedPrefModule = module {
  single {
    provideSharedPref(androidContext())
  } withOptions {
    named("SDKSharedPref")
    createdAtStart()
  }
}

private fun provideSharedPref(
  context: Context,
  name: String = context.packageName
): SharedPreferences = context.getSharedPreferences(name, MODE_PRIVATE)

private val sdkModule = module {
    single {
      provideSDK(androidContext()) {
        token = sharedPreferences.getString("token", "null") ?: "null"
      }
    } withOptions {
      createdAtStart()
    }
  }

But in this way there is no longer a Missing Definition error.

@Module
internal class CoroutinesModule {
  @Singleton(createdAtStart = true)
  @Named("IO")
  fun provideIODispatcher() = Dispatchers.IO

  @Singleton(createdAtStart = true)
  @Named("Default")
  fun provideDefaultDispatcher() = Dispatchers.Default

  @Singleton(createdAtStart = true)
  @Named("Main")
  fun provideMainDispatcher() = Dispatchers.Main
}
ghasemdev commented 11 months ago

Perhaps a tag is needed to exclude the regular modules of koin.

@IgnoreModule
internal val sharedPrefModule = module {
  single {
    provideSharedPref(androidContext())
  } withOptions {
    named("SDKSharedPref")
    createdAtStart()
  }
}

@arnaudgiuliani

arnaudgiuliani commented 9 months ago

Yeah, android elements must be ignored somewhere. It's a "known" issue of current implementation 👍

arnaudgiuliani commented 9 months ago

@ghasemdev interesting to also add somewhere @Ignore to avoid checking it if needed. Let's challenge it 🤔

arnaudgiuliani commented 9 months ago

Let's see #101