Description
openedon Jul 16, 2024
Edit When I originally wrote this, I thought it was related to the use of expect/actual in multiplatform. Upon further testing, it appears to be just related to the use of a typealiased annotation in any context
I have a multiplatform project where I use Dagger in jvm/android. To accomplish this in a way that still allows for use of standard injection annotations in common sources, I use expect/actual + typealiases to actualize them in jvm/android compilations to the real dagger annotations.
// commonMain
@Target(CONSTRUCTOR, FUNCTION, PROPERTY_SETTER) expect annotation class Inject()
@Target(CONSTRUCTOR) expect annotation class AssistedInject()
@Target(VALUE_PARAMETER) expect annotation class Assisted(val value: String = "")
@Target(CLASS) expect annotation class AssistedFactory()
// androidMain/jvmMain
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import javax.inject.Inject
actual typealias Inject = Inject
actual typealias AssistedInject = AssistedInject
actual typealias Assisted = Assisted
actual typealias AssistedFactory = AssistedFactory
However, it appears that dagger-ksp then misses these types because it doesn't resolve the aliased type of the annotations. I'm not sure if this is a dagger issue or a KSP issue (i.e. is KSP not correctly resolving that the annotated symbol is actually an alias to the intended type).
Two alternative solutions:
- Allow specifying custom annotations to replace dagger's expected ones. This solution is what the kotlin parcelize plugin used to support K2/KMP parceling. That is to say, expose compiler options to remap/add mappings of the annotations dagger looks for, such that I could say "hey
my.custom.AssistedFactory
should also be treated as@AssistedFactory
". - Repackage dagger's annotations into a multiplatform project such that they can be used natively in a KMP project.