cashapp / InflationInject

Constructor-inject views during XML layout inflation
Apache License 2.0
904 stars 55 forks source link

Cannot find symbol AssistedInject #133

Closed abhishekredspace closed 4 years ago

abhishekredspace commented 4 years ago

Hello, I am trying to use AssistedInject library with Android WorkManager framework to inject object in Worker class. But my implementation throws the following error.

@dagger.Module(includes = {AssistedInject_SampleAssistedInjectModule.class})
                           ^
  symbol: class AssistedInject_SampleAssistedInjectModule

I am using the latest version, this is my dependency

// Assisted Inject
    compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.5.2'
    annotationProcessor 'com.squareup.inject:assisted-inject-processor-dagger2:0.5.2'

and this is my implementation

@Component(
    modules = [
        SampleAssistedInjectModule::class,
        WorkerBindingModule::class
    ]
)
interface SampleComponent {
    fun factory(): WorkerFactoryImpl
}

@AssistedModule
@Module(includes = [AssistedInject_SampleAssistedInjectModule::class])
interface SampleAssistedInjectModule

@MapKey
@Target(AnnotationTarget.FUNCTION)
annotation class WorkerKey(val value: KClass<out ListenableWorker>)

@Module
interface WorkerBindingModule {
    @Binds
    @IntoMap
    @WorkerKey(FavoriteWorker::class)
    fun bindHelloWorldWorker(factory: FavoriteWorker.Factory): MCWorkerFactory
}
@AssistedInject.Factory
interface MCWorkerFactory {
    fun create(appContext: Context, params: WorkerParameters): ListenableWorker
}

class WorkerFactoryImpl @Inject constructor(
    private val workerFactories: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<MCWorkerFactory>>
) : WorkerFactory() {
    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker? {
        val foundEntry =
            workerFactories.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) }
        val factoryProvider = foundEntry?.value
            ?: throw IllegalArgumentException("unknown worker class name: $workerClassName")
        return factoryProvider.get().create(appContext, workerParameters)
    }
}

and this is my worker class where I use it,

class FavoriteWorker
@AssistedInject constructor(
    @Assisted private val appContext: Context,
    @Assisted private val workerParameters: WorkerParameters,
    val foo: Foo
) : Worker(appContext, workerParameters) {

    override fun doWork(): Result {

        Timber.i("Abhishek worker started")

        return Result.success()
    }
    @AssistedInject.Factory
    interface Factory : MCWorkerFactory
}

Reading on SO and other forums tells there is something with Kapt but I am not sure what exactly is going on, any help appreciated.

JakeWharton commented 4 years ago

Everything looks good. All you need is to be using kapt and not annotationProcessor since you're using Kotlin. Documentation on kapt is available here: https://kotlinlang.org/docs/reference/kapt.html.