Dagger 2 component not generated - android

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'

Related

Can you use Hilt in a Java-only Android project?

According to the official integration guide, you need to add
plugins {
id 'kotlin-kapt'
...
}
and
dependencies {
implementation "com.google.dagger:hilt-android:{hilt_version}"
kapt "com.google.dagger:hilt-compiler:{hilt_version}"
}
to your build.grade file. However, when I do a gralde sync, I get the following error:
Plugin [id: 'kotlin-kapt'] was not found in any of the following
sources:
is it possible to use Dagger-Hilt in a pure java project? Or do you have to either use plain Dagger or use Kotlin?
Yes, you can use the following instead of kotlin-kapt:
dependencies {
implementation "com.google.dagger:hilt-android:{hilt_version}"
annotationProcessor 'com.google.dagger:hilt-compiler:{hilt_version}'
}
plugins {
id 'dagger.hilt.android.plugin'
}

Android Hilt won't work with plugins added

I have questions about android hilt.
I have added hilt plugin.
//build.gradle(:project)
buildscript {
ext.hilt_version = '2.37'
dependencies {
...
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
//build.gradle(:app)
plugins {
...
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
}
//MyApplication.kt
#HiltAndroidApp
class MyApplication : Application() {...}
When I build the project,
I get the error message saying
"Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?"
Do you have any idea?
I am also having same problem in my new projects. This error had gone after lowering kotlin version to 1.5.10. I think hilt has compatibility issues with latest kotlin plugin.
As mentioned by others, Kotlin 1.5.20 has a bug in Kapt which causes this issue.
It is fixed in 1.5.21. Just increase the version and you're good.
There is a bug in 1.5.20
To avoid this bug there is a workaround.
in build.gradle (app) file paste this code below.
generally, in the hilt Gradle plugin, these options are set automatically. in 1.5.20 it is not. if you set it manually it will resolve the issue.
kapt {
javacOptions {
option("-Adagger.fastInit=ENABLED")
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}}

RoomDatabase_Impl does not exist

I when try to implement a Room Database, I get the following error:
java.lang.RuntimeException: cannot find implementation for com.udacity.gradle.builditbigger.Database.HilarityUserDatabase. HilarityUserDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
I tried adding the relevant kotlin dependencies to my gradle file (shown below) but when I do, all of my Databinding classes that would normally be generated with any issues are now generating errors in my gradle console. Is there way for me to use the DataBinding library and the Room Pesistence Library?
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
dependencies{
kapt "android.arch.persistence.room:compiler:1.0.0"
}
It did happen to me before, make sure that you have all 3 dependencies in build.gradle
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
kapt 'android.arch.persistence.room:compiler:1.0.0'
Also, a "Project Clean" after gradle synch will help as well.
Make sure kotlin-kapt is included in app-level gradle file.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
and make sure you use kapt instead of annotationProcessor. That solved my problem.
And also check Room Model, DAO, and Database files for #Entity, #Dao and #Database annotations.
For usage of Room, LiveData and ViewModel you need these libraries:
•implementation "android.arch.persistence.room:runtime:1.0.0"
•implementation "android.arch.lifecycle:extensions:1.1.0"
•kapt "android.arch.persistence.room:compiler:1.0.0"
•kapt "android.arch.lifecycle:compiler:1.1.0"
LiveData and ViewModel allows you to use the DataBinding technique.
For more info check the official page: https://developer.android.com/topic/libraries/architecture/adding-components.html
I was facing the same issue, later found that I am not using the #Database annotation for AppDatabase
Use this
#Database(entities = {RowEntity.class, WifiDetailEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase
{ ...
For the newer version of the room compiler don't need to add both dependencies, Just do the following -
kapt 'android.arch.persistence.room:compiler:2.2.3'
As explained above, for ease
1- Add the following in the build.gradle(Module:app) at the very top of the file
apply plugin: 'kotlin-kapt'
2- Then add
//Room for database
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
3- Sync the file and clean the project. Done
Had a similar problem while doing Codelabs. I was able to solve it by adding the updated Room dependencies on my build.gradle(module).
You can copy and paste the implementation declarations from the Room docs page in the Android Developer site. (Note: Check in your list of dependencies for
implementation 'androidx.room:room-runtime:2.2.5'
or similar, make sure to remove it and only leave the one you copied and pasted, which would look like this
implementation "androidx.room:room-runtime:$room_version"
)
Room | Android Developers #Declaring Dependencies
Add these Dependencies to your build.gradle file
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
MhzDev answer is fine these are the updated version of the dependencies

Android Dagger 2.11 with Kotlin, ContributesAndroidInjector Annotation issue

I'm using Dagger 2.11 with Kotlin. Everything is fine with Dagger but when i add ContributesAndroidInjector annotation to project i get this error:
e:
...build/tmp/kapt3/stubs/devDebug/com/raqun/android/di/AppComponent.java:6: error: dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
e:
e: public abstract interface AppComponent extends dagger.android.AndroidInjector<MyApp> {
e:
Here're the dependencies i use:
$rootProject.ext.daggerVersion = 2.11
compile "com.google.dagger:dagger-android:$rootProject.ext.daggerVersion"
compile "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
I already added:
kapt {
generateStubs = true
}
and
apply plugin: 'kotlin-kapt'
What i'm missing or doing wrong?
Thanks for your help.
Not: I already tried cleaning gradle and re-building project.
The problem is about my dependencies. Here're the working dependencies for Dagger 2.11 and Kotlin.
compile "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
Thanks all for help.

Unresolved reference DaggerApplicationComponent

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!

Categories

Resources