3

I am using Dagger for dependency injection in Kotlin. Now here are the necessary classes

ActivityModule

@Module class ActivityModule (val activity : Activity){
    @Provides
    @ActivityContext
    fun provideContext() : Context{
        return activity
    }
}

ActivityComponent

@PerActivity
@Component(dependencies = arrayOf(ApplicationComponent::class), modules = arrayOf(ActivityModule::class))
interface ActivityComponent {
    fun inject(activity: MainActivity)
}

MainActivity

@Inject @ActivityContext lateinit var context:Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DaggerActivityComponent.builder()
.applicationComponent(MainApplication.getInstance().getApplicationComponent()).activityModule(ActivityModule(this)).build()
.inject(this);

Now I am getting the following error

error: android.content.Context cannot be provided without an @Provides- or @Produces-annotated method.
e: 

e:     public abstract void inject(@org.jetbrains.annotations.NotNull()
e:                          ^
e:       android.content.Context is injected at
e:           app.feed.com.ui.MainActivity.context
e:       app.feed.com.ui.MainActivity is injected at
e:           app.feed.com.injection.component.ActivityComponent.inject(p0)

I am using the same code in java and its working fine but here in kotlin its giving the error, also ApplicaionComponent dependencies are working fine. Meanwhile i am using the following versions of gradle and kotlin

buildscript {
    ext.kotlin_version = '1.1.2-3'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Kindly help me to figure out what am I doing wrong

tynn
  • 38,113
  • 8
  • 108
  • 143
Debu
  • 615
  • 7
  • 21
  • show dependencies too, especially dagger – EpicPandaForce Oct 24 '17 at 07:12
  • `compile 'com.google.dagger:dagger:2.12' kapt 'com.google.dagger:dagger-compiler:2.12' kapt 'com.google.dagger:dagger-android-processor:2.12'` – Debu Oct 24 '17 at 07:14
  • What's the point of this injection, you're providing itself? – Mark Oct 24 '17 at 07:19
  • In actual scenario the injection happens in some fragment – Debu Oct 24 '17 at 07:26
  • You're using a qualifier `@ActivityContext`. Either remove it in your module so that you provide a plain `Context`, or also add it in your MainActivity so that you request a `@ActivityContext Context`. – David Medenjak Oct 24 '17 at 07:27
  • Possible duplicate of [How do I fix Dagger 2 error '... cannot be provided \[...\]'?](https://stackoverflow.com/questions/44912080/how-do-i-fix-dagger-2-error-cannot-be-provided) – David Medenjak Oct 24 '17 at 07:28
  • 1
    @DavidMedenjak, I see he has declared `@Inject @ActivityContext lateinit var context:Context` in activity. – azizbekian Oct 24 '17 at 07:29
  • 1
    @azizbekian Thank you, I overlooked that. @Debu I still believe this is a problem with the qualifier, please include the source of the `@ActivityContext` annotation and try if it would work without it – David Medenjak Oct 24 '17 at 07:39
  • its working if I remove `@ActivityContext` , now what if I want to provide separate context for activity and application?? – Debu Oct 24 '17 at 07:50
  • @Debu as I said, please include the code of the annotation so that we can verify it – David Medenjak Oct 24 '17 at 08:54
  • `@Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface ActivityContext { } ` – Debu Oct 24 '17 at 11:32
  • @Debu You should try switching to a Kotlin annotation – David Medenjak Oct 24 '17 at 13:50

1 Answers1

5

You injected property

@Inject @ActivityContext lateinit var context: Context

only annotates the property with @ActivityContext. So Dagger is looking for a provider for a simple Context without any qualifier. Instead you have to annotate the field with it

@Inject @field:ActivityContext lateinit var context: Context
tynn
  • 38,113
  • 8
  • 108
  • 143