Hey, just converted java class to kotlin and After...
# android
a
Hey, just converted java class to kotlin and After adding kapt to my gradle , the dagger component is not generated any more, anyone here can help?
apply plugin: 'kotlin-kapt'
...
implementation 'com.google.dagger:dagger:2.11'
kapt 'com.google.dagger:dagger-compiler:2.11'
g
Do you have any build errors?
e
Have you tried to
./gradlew clean
and/or rebuild project?
d
Did you add
Copy code
kapt {
    generateStubs = true
}
to gradle file?
😞 1
👎 3
a
WelcomeContract.Presenter> cannot be provided without an @Provides- or @Produces-annotated method
g
You don’t need generateStubs = true
a
yeah, i don't have it .
@efemoney yeah done all that.
@Module class WelcomeModule { @Provides fun providePresenterFactory(interactor: DragonInteractor): () -> WelcomePresenter { return { WelcomePresenter(interactor) } } }
e
Where is your @Provides method for WelcomeContract.Presenter ?
g
But this function returns lambda instead instance of class
e
Exactly
a
aha?
d
@gildor from which version of kotlin plugin?
a
what's the correct syntax ?
g
Copy code
@Provides fun providePresenterFactory(interactor: DragonInteractor): WelcomePresenter {
        return WelcomePresenter(interactor) 
}
e
☝️
a
hah ! convert failed !
g
could you please show your original java code?
e
Yeah you should probably report that on YT. If it was the converter that did that
a
YT ?
g
YouTrack
e
YouTrack
👍 1
g
Please show original code before you filled issue
@denis.shakinov Not sure, but it been there even before 1.1. For now you should use only kapt3. kapt1 has a lot of problems and known bugs and not supported anymore. You need only kotlin-kapt plugin
l
exactly what Andrey says. kapt3 only and therefore no
generateStubs
a
The lambda is already there 😕
g
I see, you original code returns PresenterFactory, that, as I suppose is SAM interface.
It’s not a fail of conversion, but yeah, this conversion doesn’t work properly with Dagger
Copy code
@Provides fun providePresenterFactory(interactor: DragonInteractor): PresenterFactory<WelcomeContract.Presenter> {
        return PresenterFactory { WelcomePresenter(interactor) }
    }
}
@alouanemed Try this
d
@gildor so using kotlin plugin 1.1.4 and having kotlin-kapt in my gradle file means that I’m already using kapt3 and i can delete generateStubs everywhere?
g
@denis.shakinov yes
d
thank you
a
it says PresenterFactory ;Interface doesn't have constructor
g
@alouanemed PresenterFactory is Kotlin or Jva interface?
a
kotlin
interface PresenterFactory<T : BasePresenter<*>> { fun create(): T }
g
Oh, in this case you cannot use SAM conversion
a
why is that?
g
@alouanemed then you need to implement this interface:
Copy code
@Provides
fun providePresenterFactory(interactor: DragonInteractor): PresenterFactory<WelcomeContract.Presenter> {
    return object : PresenterFactory<WelcomeContract.Presenter> {
        override fun provide(): WelcomeContract.Presenter {
            return WelcomePresenter(interactor)
        }
    }
}
a
I see, what if I revoked back the Factory interface to java?
the old verison should work, right ?
g
@alouanemed Yeah this should work - https://kotlinlang.slack.com/archives/C0B8M7BUY/p1502793037000209?thread_ts=1502792341.000112&amp;cid=C0B8M7BUY But I think there are 3 better options: 1) Implement interface as anonymous object, looks not so good, but works - https://kotlinlang.slack.com/archives/C0B8M7BUY/p1502793957000002?thread_ts=1502792341.000112&amp;cid=C0B8M7BUY 2) Write some simple wrapper for PresenterFactory to hold lambda 3) Use qulifier annotation on lamda, it will allow you to use lambda directly, but require refactor all client usages
for option 2:
Copy code
class SimplePresenterFactory<T : BasePresenter<*>>(private val factory: () -> T) : PresenterFactory<T> {
    override fun create(): T {
        return factory()
    }
}
and then to use it:
Copy code
@Provides
fun providePresenterFactory(interactor: DragonInteractor): PresenterFactory<WelcomeContract.Presenter> {
    return SimplePresenterFactory { WelcomePresenter(interactor) }
}
a
Thank you for the detailed answer. I opt ed for the first one (converting Factory to java) that one is fixed, but am getting this bunch of errors in all my sub components : Component.java5 error: incompatible types: NonExistentClass cannot be converted to Annotation e: e: @error.NonExistentClass() e: ^
@ActivityScope @Subcomponent(modules = arrayOf(WelcomeModule::class)) interface WelcomeComponent {
g
Try to clean your project and build again
If you still has this problem after clean it means that you have some other problem and should check gradle build logs, reason is definitely there
👌 1