Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger and Kotlin. Dagger doesn't generate component classes

I'm new with kotlin and Dagger. I have a little problem that I do not how to solve and I don't find a solution.

So this is what I have:

@Module class AppModule (app: Application) {     private var application: Application;      init {         this.application = app;     }      @Provides fun provideApplication(): Application? {         return application;     }      @Provides fun provideResources(): Resources? {         return application.resources;     } }  @Singleton @Component(modules =  arrayOf(AppModule::class)) interface AppComponent: AppComponentBase {      public class Initializer {         private constructor(){}          companion object {             fun Init(app: Application): AppComponent? {                 return DaggerAppComponent.builder().appModule(AppModule(app)).build()             }         }     } } 

AppComponentBase: This interface contain all the methods needed by this component.

Now, the problem is that this DaggerAppComponent class is not generated by Dagger if I do this DaggerAppComponent.builder().appModule(AppModule(app)).build() invocation within the companion object. If a invoke the same line any were by the companion object dagger generate de class without any problem.

An other thing I did look for a solution was create an other different class with the same structure, and importe the DaggerAppComponent as internal object, and I the same result happened.

I don't what to have the initialization of the component outside. So, there any other alternative solution, or what am I doing wrong?.

like image 269
Leo Avatar asked Dec 19 '15 22:12

Leo


2 Answers

You need to have the kapt processor in build.gradle:

kapt {     generateStubs = true }  dependencies {     ...     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"     compile 'com.google.dagger:dagger:2.0.2'     kapt 'com.google.dagger:dagger-compiler:2.0.2'     ... } 

This extension will generate the code for dagger.

Additionally, for newer gradle versions, you can also apply the plugin in your build.gradle:

apply plugin: 'kotlin-kapt'  dependencies {     ...     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"     compile 'com.google.dagger:dagger:2.0.2'     kapt 'com.google.dagger:dagger-compiler:2.0.2'     ... } 

You can check this project for reference

like image 147
Deividi Cavarzan Avatar answered Sep 21 '22 02:09

Deividi Cavarzan


I don't know when this change happened, but on 1.1.2 of the Kotlin gradle plugin you replace this (in your module's build.gradle):

kapt {     generateStubs = true } 

with this:

apply plugin: 'kotlin-kapt' 

and then make sure to replace dependencies that use annotationProcessor with kapt.

For example, the old way would be to use:

annotationProcessor (     'some.library:one:1.0'     ...     'some.library.n:n.0' ) 

and now you use:

kapt (     'some.library:one:1.0'     ...     'some.library.n:n.0' ) 
like image 36
Plumbus Avatar answered Sep 21 '22 02:09

Plumbus