49

I'm trying to create my app component, but Dagger does not generate my app component. here is MyApplication class

class MyApplication : Application() {

companion object {
    @JvmStatic lateinit var graph: ApplicationComponent
}
@Inject
lateinit var locationManager : LocationManager

override fun onCreate() {
    super.onCreate()
    graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build()
    graph.inject(this)
  }
}

and here is my AppComponent class

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface ApplicationComponent {
    fun inject(application: MyApplication)
}

here is screenshot enter image description here

this is my project on github

here is error log

Error:(7, 48) Unresolved reference: DaggerApplicationComponent
Error:(28, 17) Unresolved reference: DaggerApplicationComponent
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
Information:BUILD FAILED
Information:Total time: 21.184 secs
Error:e: .../MyApplication.kt: (7, 48): Unresolved reference: DaggerApplicationComponent
e: Unresolved reference: DaggerApplicationComponent
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Information:4 errors
Information:0 warnings
Information:See complete output in console

16 Answers16

52

my solution is to add

apply plugin: 'kotlin-kapt'

and remove

kapt {
    generateStubs = true
}
Pauland
  • 1,974
  • 16
  • 25
49

This helped me to fix this issue

Add this in the top of the build.gradle

apply plugin: 'kotlin-kapt'

Inside android tag add

kapt {
    generateStubs = true
}

And then replace

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

to

kapt 'com.google.dagger:dagger-compiler:2.11'

Now Rebuild the project by

Build -> Rebuild project
Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37
29

Please try enabling stubs generation, this might be the reason why the class is not visible at this stage of the build process. In your build.gradle file, top level:

kapt {
    generateStubs = true
}
AndroidEx
  • 15,524
  • 9
  • 54
  • 50
7

I've already downloaded your Github project. Thanks for sharing!

The answer for your issue is pretty simple:

Build -> Rebuild project

Dagger dependencies files will be recreated and app after would launched with any problem.

I checked already this with Android Studio 2.1.2 version. It works

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
5

you should remove

kapt {
generateStubs = true}

and add to the top of application gradle file

apply plugin: 'kotlin-kapt'

then Dagger will take care of the rest :)

ediBersh
  • 1,135
  • 1
  • 12
  • 19
4

Add this on Top of build.gradlle

apply plugin: 'kotlin-kapt'

Add this in Dependencies

kapt 'com.google.dagger:dagger-compiler:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'

After that rebuild your project . i hope it will work. Thanks

3

In my case missing kapt compiler dependency. Please make sure to have below dependency too.

kapt 'com.google.dagger:dagger-compiler:2.15'

app build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.usb.utility"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.dagger:dagger-android:2.15'
    compile 'com.google.dagger:dagger-android-support:2.15' // if you use the support libraries
    kapt 'com.google.dagger:dagger-android-processor:2.15'
    kapt 'com.google.dagger:dagger-compiler:2.15'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Pankaj Kant Patel
  • 2,050
  • 21
  • 27
2

Also for Java users, the solution is simply to Rebuild your project.

otoloye
  • 677
  • 7
  • 11
2

Answer is simple :

Build -> Rebuild project

It works for me.

tugbacevizci
  • 81
  • 1
  • 7
2

Only two steps should be required for mixed Java/Kotlin projects:

  1. Add apply plugin: 'kotlin-kapt' to the top of the build file
  2. Replace annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' with kapt 'com.google.dagger:dagger-compiler:2.14.1'
Meanman
  • 1,474
  • 20
  • 17
2

In the new version of hilt, you need to use SingletonComponent::class instead of ApplicationComponent::class

Simple UX Apps
  • 611
  • 2
  • 10
  • 20
1

For anyone using the newest Dagger version (above 2.30):

ApplicationComponent has been replaced by SingletonComponent.

@Module
@InstallIn(SingletonComponent::class)
class RoomModule() {
   . . .
}

See this answer.

nitro_nitrat
  • 131
  • 7
0

Please try adding this to your build.gradle

android {
    dexOptions {
        incremental false
    }

EDIT: apparently a year later this can happen if you don't apply kotlin-kapt plugin. Also make sure you use kaptinstead of annotationProcessor.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • https://github.com/developer--/Where_is_WIFI/blob/master/app/build.gradle –  Aug 09 '16 at 09:10
  • What happens if you remove android-apt plugin? – EpicPandaForce Aug 09 '16 at 09:12
  • I'm not sure what's wrong then :( but please check [this example](https://github.com/damianpetla/kotlin-dagger-example) and see if there's any difference and if the "known limitations" here match your issue – EpicPandaForce Aug 09 '16 at 09:16
0

In my case Rebuild didn't help. I injected two dependencies in <module> component (BookComponent):

fun inject(fragment: ListFragment)

fun inject(fragment: NotFoundFragment)

When I removed one inject, the project had built. But both injects together broke build process. Then I simply swapped them:

fun inject(fragment: NotFoundFragment)

fun inject(fragment: ListFragment)
CoolMind
  • 26,736
  • 15
  • 188
  • 224
-1

This must solve your problem:

Update (March 29, 2020)

Inside your app-level build.gradle inside dependencies block, add these lines:

     //dagger2
     api 'com.google.dagger:dagger:2.24'
     api 'com.google.dagger:dagger-android:2.24'
     api 'com.google.dagger:dagger-android-support:2.24'

     annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
     kapt 'com.google.dagger:dagger-compiler:2.24'

     annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
     kapt 'com.google.dagger:dagger-android-processor:2.24'

     compileOnly 'javax.annotation:jsr250-api:1.0'
     implementation 'javax.inject:javax.inject:1'

Inside android block of app-level build.gradle,

kapt {
        generateStubs = true
    }

At the top of the app-level build.gradle, Do this in exactly below order.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

Finally, You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Settings for New Projects>search"Annotation processor" enter image description here

After this, do from Menu Build > Rebuild. You are done!

Test:

@Component
public interface ApplicationComponent {

}

Now, you can use DaggerApplicationComponent that was generated at compile-time for your ApplicationComponent interface.

public class MyApplication extends Application {

    ApplicationComponent applicationComponent = DaggerApplicationComponent.create();


}
Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
-1

In my case, I did everything in al answers but did not fix it!

my solution: go to Directory path of your project and delete

build, app>build, .idea , .Gradle

and go back to the android studio and rebuild the project and Done!

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44