google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

Cannot use new compiler with dagger.hilt.android.plugin #2086

Closed lukas1 closed 4 years ago

lukas1 commented 4 years ago

Version: 2.29

As per release notes of Dagger 2.29 (https://github.com/google/dagger/releases/tag/dagger-2.29)

I've tried to change the compiler dependency to:

kapt "com.google.dagger:hilt-compiler:2.29-alpha"

This however doesn't work, if I have applied dagger.hilt.android.plugin. It says:

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android-compiler dependency was found.
bcorso commented 4 years ago

Thanks! We can send out a patch for that soon.

In the mean time, you can continue using com.google.dagger:hilt-android-compiler. The new hilt-compiler artifact is just a rename.

bcorso commented 4 years ago

Update: 2.29.1 should now be available with the fix.

ThanosFisherman commented 4 years ago

Still having this issue on a multi-module android project with android gradle plugin 4.1.0

bcorso commented 4 years ago

Hi @ThanosFisherman,

Can you check that you are using version com.google.dagger:hilt-android-gradle-plugin:2.29.1-alpha of the plugin?

Also check the error message for the failing Gradle module and verify that you've added the Hilt dependencies to that Gradle module. Note: You may need to go to the raw error message to find the failing Gradle module, as shown below:

image

If none of that helps, we would need a sample project or more explicit steps to reproduce the problem.

ThanosFisherman commented 4 years ago

Hi @bcorso,

Thanks for pointing me to the right direction. Upon second look I realized that I had some unwanted lines in my gradle scripts that would consequently cause hilt to throw this error. After deleting those lines everything builds without errors, but now I do get runtime errors that I wouldn't get with hilt 2.28-alpha

java.lang.InstantiationException: java.lang.Class<AuthViewModel> has no zero argument constructor

But I suppose the error above is unrelated with this issue.

I'm also confused with the number of dependencies I should include as of the new hilt 2.29.1 Could you please verify that everything I included is correct?

I use hilt gradle plugin in my root gradle build file

com.google.dagger:hilt-android-gradle-plugin:2.29.1-alpha

then in my app level gradle file I added the following

kapt("com.google.dagger:hilt-compiler:2.29.1") and implementation("com.google.dagger:hilt-android:2.29.1") also for viewmodel I use implementation("androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02") and lastly I add the plugin id like so id("dagger.hilt.android.plugin")

And that's all. I did not included the hilt-core dependency whatsoever

lukas1 commented 4 years ago

@ThanosFisherman

java.lang.InstantiationException: java.lang.Class has no zero argument constructor

Sounds like you maybe did not use @ViewModelInject for the constructor of your AuthViewModel...

something like this

class AuthViewModel @ViewModelInject constructor(
  private val someDependency: SomeType
) : ViewModel() {}

You're saying you included this: implementation("androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02")

You actually need this dependency to enable that @ViewModelInject annotation. You wouldn't need that package if you wouldn't use that annotation. But in your case, you do need that package and you do need to use that annotation.

A few weeks ago I wrote a TLDR article with minimum setup of Dagger Hilt necessary, might be helpful to you to understand which dependencies you need to add: https://medium.com/@vyletel/dagger-hilt-tldr-aa4fa361bf84

ThanosFisherman commented 4 years ago

I'm already using @ViewModelInject in my ViewModels I did not have any problems with 2.28-alpha

Maybe I am missing something. I'll have a look at your article and if I still can't figure out what's causing this runtime crash, I'll create a sample project that reproduces this issue and post back. Probably I will open a new ticket as well.

Thanks

mochadwi commented 4 years ago

I'm also occurred this problem when migrating to hilt-compiler and shows the same error message

Does it needs to invalidate caches & restart first? *or deleting gradle/caches

ThanosFisherman commented 4 years ago

@mochadwi have a look at this https://github.com/google/dagger/issues/2147 maybe you are missing something in your dependencies like I did

mochadwi commented 4 years ago

Agreed. After updating, add missing hilt dependency, invalidating, cleaning & rebuilding it works in my case.

mollykewl2024 commented 3 years ago

Maybe , You didn,t have """ classpath "com.google.dagger:hilt-android-gradle-plugin:2.30.1-alpha""" in your dependencies

Drjacky commented 3 years ago

@bcorso Could you please take a look into https://github.com/google/dagger/issues/2661

diegoflassa commented 3 years ago

O solved mine by adding the following dependencies

// Dagger & Hilt implementation("com.google.dagger:hilt-android:2.40") kapt("com.google.dagger:hilt-android-compiler:2.40") implementation("androidx.hilt:hilt-common:1.0.0") kapt("androidx.hilt:hilt-compiler:1.0.0") implementation("androidx.hilt:hilt-navigation-fragment:1.0.0") implementation("androidx.hilt:hilt-work:1.0.0")

kavatari commented 2 years ago

@diegoflassa thanks, the only one thing that helped me here.