Unresolved reference DaggerApplicationComponent - android

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

my solution is to add
apply plugin: 'kotlin-kapt'
and remove
kapt {
generateStubs = true
}

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

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
}

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

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 :)

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

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'
}

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

Answer is simple :
Build -> Rebuild project
It works for me.

Only two steps should be required for mixed Java/Kotlin projects:
Add apply plugin: 'kotlin-kapt' to the top of the build file
Replace annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' with kapt 'com.google.dagger:dagger-compiler:2.14.1'

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

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.

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)

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.

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"
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();
}

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!

Related

how to fix unresolved reference lifecycleScope?

Trying to build a simple example that uses kotlin coroutines in an activity:
lifecycleScope.launch {
val result = httpGet("http://hmkcode-api.appspot.com/rest/api/hello/Android")
textView.setText(result)
}
Can't get rid of error "unresolved reference lifecycleScope"
Relevant part of gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.2"
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycle:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
implementation "android.arch.lifecycle:extensions:$androidArchVersion"
}
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.3'
languageVersion = '1.3'
}
}
As per the Lifecycle KTX documentation, you must include the lifecycle-runtime-ktx artifact if you want Coroutine specific extensions.
Note that lifecycle-runtime-ktx was only introduced in Lifecycle 2.2.0, so you'll need to Migrate to AndroidX, then upgrade to Lifecycle 2.2.0 if you want to use that functionality.
Late response here but I also faced this issue and finally managed to resolve it by changing the inherited Android Activity to an AppCompatActivity.

Databinding annotation processor kapt warning

In my app module's build.gradle, I have added
dependencies {
kapt('com.android.databinding:compiler:3.1.2')
...
}
but I'm still receiving the compiler warning for
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.1.2'.
Everything functions, I just hate having warnings hanging around.
Any help is much appreciated!
I had same warnings until I upgraded to the latest Android Gradle build plugin and Kotlin. Now they are gone. Here is the configuration I use.
project.gradle
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
module.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
...
dataBinding {
enabled = true
}
}
dependencies {
// no kapt declaration for databinding here
}
Hope it helps.
Add following in you app build.gradle
kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
This will do the trick.
$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle
Also, add this to your module build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
You are missing apply plugin: 'kotlin-kapt' i think.

Error:Execution failed for task ':app:kaptDebugKotlin'

I'm new to using Kotlin and trying to set it up with Dagger2, I've seen some few examples but none of them seem to work for me.
I keep getting this
Error:Execution failed for task ':app:kaptDebugKotlin'.
Internal compiler error. See log for more details
I have my build.gradle (Module: app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.exampleapp"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
kapt {
generateStubs = true
}
dexOptions {
javaMaxHeapSize "2048M"
}
}
ext {
supportLibVer = '25.0.0'
daggerVer = '2.8'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Support lib
compile "com.android.support:appcompat-v7:${supportLibVer}"
kapt "com.google.dagger:dagger-compiler:${daggerVer}"
compile "com.google.dagger:dagger:${daggerVer}"
provided "javax.annotation:jsr250-api:${javaxVer}"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
}
Run your application with ./gradlew clean build command to see what's exactly wrong with your code. Just paste it into the Terminal in Android Studio.
If you are using the Room database and getting a KAPT error, just check your
Database declarations
Data Access Object declarations
Data class fields
It's a problem arising due to improper usage of annotations of Room.
For more information use your build logs.
I faced this problem for a while. What helped me a lot was reading the build tab because it gave the reasons the library was failing.
Here is the tab
I had many problems,
1. I haven't added the new entity I created into the #Database annotation
2. I haven't added the #Dao annotation in my interface
3. I haven't updated some variables names that was wrote in a #Query annotation
So I had to kill problem by problem, finally it could run later.
In Addition, I was cleaning my project and rebuilding to ensure code doesn't get stuck. Also close and open Android Studio.
Futhermore, you can check this answer to help you find the error enable more log on error
In my case I replaced this
implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
by
implementation 'com.google.dagger:dagger:2.27'
annotationProcessor "com.google.dagger:dagger-compiler:2.27"
and solved the problem
I faced this problem for a while. My mistake was using private access specifier with #Inject field.
If you are using Dagger then check for #Inject private fields or to know the exact cause add this as Command-line options:
--stacktrace --info --scan
On Mac, go to Android Studio > Preferences > Build, Execution, Deployment > Compiler
On Windows, go to File > Settings > Build, Execution, Deployment > Compiler
In my case, I forgot to add the room db entities to the database
#Database(version = 1,
entities = [DummyEntity::class]
)
Issue can be connected with Room and Kotlin 1.4.10.
Try to change android.arch.persistence to androidx.room for Room dependencies:
Use
kapt "androidx.room:room-compiler:$roomVersion"
instead of
kapt "android.arch.persistence.room:compiler:$roomVersion"
If You are using Hilt and Field Injection Then Remove Private From Field Injected Object this worked For me
#Inject
private lateinit var helper: Helper
to
#Inject
lateinit var helper: Helper
Worked for me:
I also had the same problem and solved it by adding this to gradle.properties
org.gradle.java.home=<go to project structure, copy JDK location and past here>
This ensures that gradlew uses the same JDK as Android Studio
The issue is probably related to the use of Room. I used the command Łukasz Kobyliński suggested in his comment
./gradlew clean build
and in my case I had to add a converter for Date type.
You can find the converter in the official docs: https://developer.android.com/training/data-storage/room/referencing-data#type-converters
I have solved this problem. In my case, there were irrelevant dagger
dependencies
that the IDE did not notify me about:
implementation 'com.google.dagger:dagger:2.35.1'
kapt 'com.google.dagger:dagger-compiler:2.28'
After updating them, the problem disappeared and it became possible to use the
latest version of Kotlin!
implementation 'com.google.dagger:dagger:2.37'
kapt 'com.google.dagger:dagger-compiler:2.37'
Just Replace kept Keyword to annotationProcessor and everything works fine.
my mistake was using suspend when then function returns LiveData.Room's coroutines integration brings ability to return suspend values but when the value itself is asnyc, there is no reason to use it.
i changed :
#Delete
suspend fun Delete(premiumPackageDBEntity: PremiumPackageDBEntity)
#Query("SELECT * FROM available_premium_package ")
suspend fun GetAll(): LiveData<List<PremiumPackageDBEntity>>
to :
#Delete
suspend fun Delete(premiumPackageDBEntity: PremiumPackageDBEntity)
#Query("SELECT * FROM available_premium_package ")
fun GetAll(): LiveData<List<PremiumPackageDBEntity>>
and the problem solved.
In my case, I forgot to add newly created entities into "entities" section of #Database declaration using Room library.
--stacktrace --info --scan commandline options are a great help to find the exact cause.
I just faced a similar bug. If you're using an old dependency for Room, update and rebuild your project.
I faced similar problem when setting up dagger2. It was finally resolved when I changed this line:
kapt "com.google.dagger:dagger-compiler:${daggerVer}"
to this
annotationProcessor "com.google.dagger:dagger-compiler:${daggerVer}"
For me I deleted these folders.
.gradle
.idea
Close android studio , delete the folders and reopen the project
//implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.2.1"
//kapt "android.arch.persistence.room:compiler:2.4.2"
implementation "androidx.room:room-ktx:2.2.1"
//annotationProcessor "androidx.room:room-compiler:2.4.2"
I didn't use the ones I used. My problem was solved when I used these two. We get a kaptDebug error because there is no match with the Room. My problem was related to the implement.
if all that tasks are not work, just run your app. You will see error log clearly.
First change
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
to
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Now you have to tweak your project Gradle file and update the version of Kotlin being used which should be something like below:
ext {
kotlin_version = '1.3.10'
gradleVersion = '3.1.0'
}
I got the same error and solved it simply by following these steps
1- open File menu -> then choose Project Structure or press Ctrl+Alt+Shift+s
2- open Modules from the left
3- In Source Compatibility press the drop down menu and choose Java 8 or 1.8
4- In Target Compatibility press the drop down menu and choose Java 8 or 1.8
5- press ok then sync and rebuild your project or run it
In my case build.gradle
replaced
id 'kotlin-android-extensions' to
id 'kotlin-parcelize'
as it said on build
added
buildFeatures {
viewBinding true
}
also had a few syntax mistakes like forgetting : at Dao
#Query("SELECT * FROM table_satis WHERE satisId ==:satisID")
In my case I'm using ViewBinding instead of DataBinding. And when I got the same problem I solved it with adding plugin apply plugin: 'kotlin-parcelize' to gradle.
try add to gradle.properties:
kapt.use.worker.api=false
kapt.incremental.apt=false
I wrote accidentally #EntryPoint instead of #AndroidEntryPoint. Changing that error was fixed.
I faced a similar problem. It was resolved when I changed this line:
ext.kotlin_version = "1.5.10"
to
ext.kotlin_version = "1.4.10"
In some of the Cases if the id of the View is incorrect it always shows this error.Recheck if the id of the view is correct.
In my case, i just updated the recently added dependencies(a newer version was available), and it worked for me.
In my case, I was using
kapt "com.github.bumptech.glide:compiler:$version_glide"
without applying the implementation.
implementation "com.github.bumptech.glide:glide:$version_glide"
You should check all kapt dependencies in the build.gradle (Module), not just glide
What worked for me was to add all of these dependencies:
room_version = '2.4.3'
// Room
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
kapt "androidx.room:room-compiler:$room_version"

Dagger 2 component not generated

In my module, in my base Application class
component = DaggerCompClassComponent.builder()
.classModule(new ModuleClass()).build();
it can not find DaggerCompClassComponent.
I have on module build.gradle
apply plugin: 'com.neenbedankt.android-apt'
.........................
apt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
provided 'javax.annotation:jsr250-api:1.0'
and in Project build.gradle,
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
I have done build / rebuild / clean / restart project. I have a Component class where I inject objects and a ModuleClass where I provide objects to inject.
What can be the cause for not generating Dagger Component . class ?
EDIT:
This is my ModuleClass, adnotated with #Module:
#Provides
#Singleton
public Interceptor provideInterceptor() {
return new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("AppName-Android", BuildConfig.VERSION_NAME + "-" + BuildConfig.VERSION_CODE)
.addHeader("Content-Type", "application/json");
return chain.proceed(builder.build());
}
};
}
#Provides
#Singleton
OkHttpClient provideOkHttpClient(Interceptor interceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
return builder.build();
}
#Provides
#Singleton
Retrofit provideRetrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(BaseApplication.getRes().getString(R.string.api_base_url))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
#Provides
#Singleton
WebServiceCall provideWebService(Retrofit retrofit) {
return retrofit.create(WebServiceCall.class);
}
And this is my Component Class:
#Component(modules = ModuleClass.class)
#Singleton
public interface ComponentClass {
void inject(Interceptor o);
void inject(OkHttpClient o);
void inject(Retrofit o);
void inject(WebServiceCall o);
}
When developing on Kotlin, you should add the following lines next to their annotationProcessor counterparts:
kapt 'com.google.dagger:dagger-android-processor:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'
and add apply plugin: 'kotlin-kapt' at the start of the same file.
That section looks like this for me:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt' // <- Add this line
apply plugin: 'io.fabric'
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"
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();
}
Maybe you forgot to annotate ModuleClass with #Module ?
If you have several modules in your AndroidStudio (modules in terms of Android Studio, not Dagger), another possible reason of fail is that you've forgot to put annotation processors into the all modules' build.gradle.
We've divided our app into several modules, updated dependencies from using implementation to using api but forgot to handle annotation processors accordingly.
So, you can have this lines only in a root module:
api 'com.google.dagger:dagger-android:2.16'
// if you use the support libraries
api 'com.google.dagger:dagger-android-support:2.16'
But this ones should be specified in all modules dependencies:
annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
// if you use injections to Android classes
annotationProcessor 'com.google.dagger:dagger-android-processor:2.16'
If Dagger2 can not generate its components it means that your code have some errors with Scopes/Modules. Check our your #Provides/Inject methods.
UPD:
You should inject your components into cases where you need instances of classes provided by module.
like
inject(MainActivity main);
In build.gradle's app, you need to add necessary dependencies for Dagger to generate corresponding classes, as below:
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
you should change dagger version which you are using.
refer full dagger document in this
There are some minor misconceptions/faults in your code above, here's a working implementation:
Application.java:
component = DaggerComponentClass.builder().classModule(new ModuleClass()).build();
The generated class will be named DaggerComponentClass, not DaggerCompClassComponent. If you can't run your app in Android Studio to get it built, try Build->Clean project and Build->Rebuild project in the menu. If everything is OK Dagger will have compiled DaggerComponentClass which will be located in the same package as ComponentClass.
ComponentClass.java:
#Component(modules = ModuleClass.class)
public interface ComponentClass {
void inject(AClassThatShouldGetInstancesInjected instance);
}
A Component in Dagger2 has methods named inject that receive the instance to get instances injected into it, not the other way around. In the code above the class AClassThatShouldGetInstancesInjected will typically call componentClass.inject(this); to get instances injected into itself.
ModuleClass.java:
#Module
public class ModuleClass {
#Provides
#Singleton
public Interceptor provideInterceptor() {/*code*/}
//Your Providers...
}
The Module is correct in your code, make sure its annotated.
If you are using Kotlin, make sure to add kapt (Kotlin annotation processor) Gradle plugin to your build script and use kapt Gradle dependency type instead of annotationProcessor for Dagger Compiler.
apply plugin: 'kotlin-kapt
kapt deps.daggercompiler
implementation deps.dagger
For anyone doing this in the year 2022 on Android Studio, if you want to just use some Dagger capabilities, you only need to add this in your Gradle dependencies:
implementation 'com.google.dagger:dagger:2.41'
kapt 'com.google.dagger:dagger-compiler:2.41'
then this on your plugins:
id 'kotlin-kapt'
You don't need to add anything else.
Now here's the key step: You need to do a clean (either in Gradle or just select Clean Project under Build menu) because for some reason incremental compilation is getting in the way.
Then rebuild your project.
If you created your Dagger components correctly you should see compilation errors at this point, or if you created them correctly the auto-completion with your component(s) should now work in the code editor.
api 'com.google.dagger:dagger-android:2.28.3'
api 'com.google.dagger:dagger-android-support:2.28.3'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.28.3'
Replace above with below dependencies in app level dependencies, if you have used above
api 'com.google.dagger:dagger:2.28.3'
annotationProcessor 'com.google.dagger:dagger-compiler:2.28.3'
I solved it by editing my dagger dependencies to this:
implementation "com.google.dagger:dagger:${versions.dagger}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
As Accepted Answer does NOT work for me:
This issue is related to Android Studio (V4.1.2 and up) and you can fix it by doing the below steps:
Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"
Also If you see other Dagger regexs, remove them too.
Now android studio will find generated component class.
In my case it wasn't being created only in Instrumentation Testing, because I was missing
kaptAndroidTest "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
Of course I'm using Kotlin and $rootProject.daggerVersion is a property on my base build file. You can replace for whatever version or gradle dependency you desire and are missing.
Try this worked for me with Kotlin. Just this much in build.gradle (Module YourProject.app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
dependencies {
.....
implementation 'com.google.dagger:dagger-android:2.35.1'
kapt 'com.google.dagger:dagger-compiler:2.15'
implementation 'javax.inject:javax.inject:1'
.....
}
//add all the dependencies otherwise component class will not be generated.
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'

Android Kotlin : Error Unresolved reference: DaggerAppComponent

I have installed Kotlin plugin today into an existing project with Dagger 2. Before Kotlin was installed I had no issues with Dagger. However, now the compiler complains :
Error:(5, 32) Unresolved reference: DaggerAppComponent
Error:Execution failed for task ':app:compileDebugKotlinAfterJava'.
> Compilation error. See log for more details
Error:(12, 21) Unresolved reference: DaggerAppComponent
Project gradle:
ext.kotlin_version = '1.1.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
Module gradle:
kapt {
generateStubs = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger:2.7'
kapt 'com.google.dagger:dagger-compiler:2.7'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
The DaggerAppComponent file IS auto generated, so I'm confused as to why there is an un resolved reference error thrown.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
And in your dependencies:
implementation "com.google.dagger:dagger:2.x"
implementation "com.google.dagger:dagger-android:2.x"
implementation "com.google.dagger:dagger-android-support:2.x"
kapt "com.google.dagger:dagger-compiler:2.x"
kapt "com.google.dagger:dagger-android-processor:2.x"
I was getting the same error message, but my case was different than yours. The unresolved reference appeared only when generating signed APK. This is how I solved it:
app/build.gradle
kapt {
generateStubs = true
}
dependencies {
//...
implementation 'com.google.dagger:dagger:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'
}
I can now deploy my signed APK without errors
At first, It gives an error after rebuild:
Unresolved reference: DaggerAppComponent
You should try to import (Alt + Enter) that component.
On my end, as of
Kotlin version 1.5.30
Dagger version 2.35.1
Dagger support version 2.28.3
There's an issue with dagger 2 (above versions) and kotlin 1.5.30. When you try to run the project using
implementation 'com.google.dagger:dagger:2.35.1'
implementation 'com.google.dagger:dagger-android:2.35.1'
implementation 'com.google.dagger:dagger-android-support:2.28.3'
kapt 'com.google.dagger:dagger-compiler:2.28.3'
kapt 'com.google.dagger:dagger-android-processor:2.28.3'
It will fail with error:
java.lang.reflect.InvocationTargetException
When i used Run with --stacktrace i found out that the issue was:
IllegalStateException: Unsupported metadata version" in
KaptWithoutKotlincTask with 1.5.0-M2
I found out that this is because Dagger uses the old version of kotlinx-metadata-jvm library & doesn't support kotlin version 1.5 and above yet.
So as a fix, i added:
kapt 'org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.2.0'
I don't know if there's an official fix to this yet, but this might help someone.
N:B: i tried changing from kapt to annotationProcessor, and though the project compiled, i still couldn't generate DaggerAppComponent
I spent a lot of time to fix this problem. In my case applied incorrect imports:
import com.google.android.datatransport.runtime.dagger.Module
import com.google.android.datatransport.runtime.dagger.Provides
I needed:
import dagger.Module
import dagger.Provides
Pay attantion
for me it was Showing Unresolved reference "DaggerAppComponent".
after spending so many time i figured,this usually happens as a result of Android Studio failed to generate DaggerAppComponent automatically.
So, we need to clean and rebuild the project , then import (ALT+Enter )
If it is still not working try File=>Invalidate Caches/restart.
may be this would help someone some day !
Do not use private for inject dagger in Kotlin, use it like this for Kotlin
#Inject
internal lateinit

Categories

Resources