Dagger and Kotlin. Dagger doesn't generate component classes
Asked Answered
B

9

49

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?.

Bruis answered 19/12, 2015 at 22:31 Comment(2)
did you have kapt 'com.google.dagger:dagger-compiler:2.0.2' in build.gradle?Elater
Follwing will also works well to fix this issue with plugins apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' and dependencies implementation "com.google.dagger:dagger:$dagger_version" implementation "com.google.dagger:dagger-android:$dagger_version" implementation "com.google.dagger:dagger-android-support:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" kapt "com.google.dagger:dagger-android-processor:$dagger_version"Maltreat
E
105

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

Elater answered 20/12, 2015 at 0:41 Comment(4)
where'd you learn about the "generateStubs" configuration? i was missing this bit and luckily stumbled across this answer.Rootlet
Did the sequence of items in dependencies count? First it didn't work for me because compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" were added to the end of dependencies list. Then I move it to top and it worked.Hayton
Follwing will also works well to fix this issue with plugins apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' and dependencies implementation "com.google.dagger:dagger:$dagger_version" implementation "com.google.dagger:dagger-android:$dagger_version" implementation "com.google.dagger:dagger-android-support:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" kapt "com.google.dagger:dagger-android-processor:$dagger_version"Maltreat
If it is java based android project, what we should give in gradle file?Dislike
T
22

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'
)
Tourane answered 28/4, 2017 at 1:30 Comment(3)
thanks for finding this just yesterday :D do you have any source for this? how did you find out?Thomasina
To make it clear: apply plugin: 'kotlin-kapt' should be in your module build.gradleWeatherby
Thanks @charlag. Updated the answer.Tourane
L
14

UPDATE FOR KOTLIN 1.1.4

generateStubs does not work anymore. Feel free to make a build with the latest Kotlin and it would tell you in the Messages section of Android Studio that it is not necessary anymore. Here's an up-to-date list of dependencies for Dagger2 for Android and Kotlin

apply plugin: 'kotlin-kapt'

//...
// Other Gradle stuff
//...

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.4-3"

    compile 'com.google.dagger:dagger-android:2.12'
    kapt 'com.google.dagger:dagger-android-processor:2.12'
    compileOnly 'com.google.dagger:dagger:2.12'
    kapt 'com.google.dagger:dagger-compiler:2.12'
}
Lavernalaverne answered 12/10, 2017 at 14:48 Comment(0)
M
7

This issue can be fixed with the bellow change which is different from original answer

Following will also work well to fix this issue

with plugins

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

and dependencies

implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version" 
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

For reference check out this Gist

Maltreat answered 13/12, 2017 at 15:33 Comment(3)
Have you been able to get this working in an android library project?Illuminance
Yes @IlluminanceMaltreat
adding "kapt "com.google.dagger:dagger-android-processor:2.+" apart from others for dagger worked for me.Griselgriselda
D
1

Use these dependencies if you are using kotlin as the primary language for android application development

// Dagger dependencies
def dagger_version = "2.44"
implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version" // if you use the support libraries
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
Diaster answered 8/10, 2022 at 3:27 Comment(1)
It works but, i don't understand. Why we have to implement both dagger and dagger android? At the documentation it says implement only dagger-android.Odellodella
O
0

My case must be some exclusion rule in Dagger's implementation

com.mycompany.native -> Dagger doesn't generate code

com.mycompany.other -> Dagger generates code

I wasted quite some time on this :-( I hope it helps someone else!

Oversize answered 2/11, 2021 at 13:52 Comment(2)
What is the mean of native/other?Odellodella
It's the package nameOversize
A
0

Sorry, I didn’t create a new issue, but decided to answer under similar questions, because maybe someone find it useful!

I faced to ridiculous issue! «Make Project» do not generate dagger files. They are generated while preparing run your App! But I was trying to solve problem for hours and didn't think of simple trying to click Run..)))

So, maybe you faced to the same problem: Neither «Sync Project with Gradle» nor «Clean» and «Build Project» help generate dagger files.

-> Then just Run your App!

After the first Run App my Dagger started generated files if I click Make Project.

Almeta answered 1/9, 2022 at 20:29 Comment(0)
P
0

For anyone still looking for an answer in 2023, You just need the following dependencies:

 plugins {
       id 'com.android.application'
       id 'org.jetbrains.kotlin.android'
       id 'kotlin-kapt'
       }

dependencies {
    def dagger_version = "2.44" //or any latest versions
    implementation 'com.google.dagger:dagger:$dagger_version"'
    kapt 'com.google.dagger:dagger-compiler:$dagger_version"'
    }

And you need to be looking for the generated components in the package with the name

"com.yourcompanyname.yourprojectname(debug)"

inside java package.

You won't find the generated dagger files in the java(generated) package.

After adding all the dependencies try clean, rebuild and then run the project and you should be seeing the generated files.

Pediform answered 30/3, 2023 at 5:20 Comment(0)
L
-1

If u have problem withe DaggerComponent, You should add

apply plugin: 'kotlin-kapt'

kapt {
    generateStubs = true
}

to build.gradleit will generate kotlin code for dagger 2 otherwise project will only build on Rebuild

Levity answered 7/5, 2017 at 10:45 Comment(2)
Works. Remember to add kapt 'com.google.dagger:dagger-android-processor:2.14.1' and kapt 'com.google.dagger:dagger-compiler:2.14.1' in the dependenciesActinozoan
This is deprecated.Bixler

© 2022 - 2025 — McMap. All rights reserved.