I'm having an issue using Dagger 2 component dependencies in Kotlin. I have the following component and module:
@Component(modules = arrayOf(ApplicationModule::class))
public interface ApplicationComponent {
public fun inject(application: MainApplication)
public fun inject(homeActivity: MainActivity)
fun locationManager(): LocationManager
}
@Module
public class ApplicationModule(private val application: Application) {
@Provides
fun provideApplicationContext(): Context {
return application
}
@Provides
fun provideLocationManager(): LocationManager {
return application.getSystemService(LOCATION_SERVICE) as LocationManager
}
}
And then I have the following component and module that use the previous component as a dependency:
@Component(dependencies = arrayOf(ApplicationComponent::class), modules = arrayOf(TestModule::class))
interface TestComponent {
fun inject(testActivity: TestActivity)
}
@Module
class TestModule {
@Provides
internal fun provideTestClass(locationManager: LocationManager): TestInjectionClass {
return TestInjectionClass(locationManager)
}
}
However this results in an error:
FATAL EXCEPTION: main
Process: com.example.dagger.kotlin, PID: 27350
java.lang.NoSuchMethodError: No virtual method provideTestClass$app_compileDebugKotlin(Landroid/location/LocationManager;)Lcom/example/dagger/kotlin/dependency/TestInjectionClass; in class Lcom/example/dagger/kotlin/dependency/TestModule; or its super classes (declaration of 'com.example.dagger.kotlin.dependency.TestModule' appears in /data/app/com.example.dagger.kotlin-2/base.apk)
at com.example.dagger.kotlin.dependency.TestModule_ProvideTestClass$app_compileDebugKotlinFactory.get(TestModule_ProvideTestClass$app_compileDebugKotlinFactory.java:22)
at com.example.dagger.kotlin.dependency.TestModule_ProvideTestClass$app_compileDebugKotlinFactory.get(TestModule_ProvideTestClass$app_compileDebugKotlinFactory.java:8)
at com.example.dagger.kotlin.dependency.TestActivity_MembersInjector.injectMembers(TestActivity_MembersInjector.java:26)
at com.example.dagger.kotlin.dependency.TestActivity_MembersInjector.injectMembers(TestActivity_MembersInjector.java:8)
at com.example.dagger.kotlin.dependency.DaggerTestComponent.inject(DaggerTestComponent.java:43)
at com.example.dagger.kotlin.dependency.TestActivity.onCreate(TestActivity.kt:19)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
So it doesn't seem to generate provideTestClass$app_compileDebugKotlin
in the generated class for TestModule
. The exact same structure does seem to work in Java. I read somewhere that there initially were some issues combining Dagger2 and Kotlin, but that they were solved by now. I uploaded a sample here which shows the issue.