29

Build error after update from Kotling 1.3.21 to 1.3.30:

AppComponent.java:16: error: [Dagger/MissingBinding]    
   java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,
   javax.inject.Provider<androidx.lifecycle.ViewModel>> 
   cannot be provided without an @Provides-annotated method.

Reproduced on two different projects with similar dependencies on Kotlin, Dagger and Architecture components.

I suspect it somehow related to the recent kapt updates in kotlin 1.3.30: https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/

Tried to disable/enable the kapt options from the article, tried gradle clean, invalidate caches, nothing helps. Only downgrading to 1.3.21 projects build successfully.

yaroslav
  • 863
  • 1
  • 8
  • 20
  • 3
    It is a humble request to the community to help with a solution to this problem. Maybe someone has already solved this issue. – yaroslav Apr 12 '19 at 08:18

2 Answers2

37

This bug was already reported by someone on GitHub and on YouTrack. This should be fixed once Kotlin version 1.3.31 gets released.

Update: Kotlin 1.3.31 is out, so make sure to update your Kotlin version!


The workaround for Kotlin 1.3.30 listed on GitHub is to use a Java annotation instead of Kotlin for ViewModelKey, or you may downgrade back to Kotlin 1.3.21.

/**
 * Workaround in Java due to Dagger/Kotlin not playing well together as of now
 * https://github.com/google/dagger/issues/1478
 */
@MapKey
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewModelKey {
    Class<? extends ViewModel> value();
}

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • 1
    Downgrade your kotlin version to `1.3.21` – aslamhossin Apr 20 '19 at 05:06
  • Just use a java class instead... you saved me. – Forke Apr 23 '19 at 13:34
  • FYI: 1.3.31 didn't solve it on my side. I still need to use the Java workaround. – Forke Apr 26 '19 at 07:54
  • @Forke Interesting. I just gave it a quick try earlier, and it seemed to work for me. Did you do a clean build and cleared the build cache? It may be caching issue or a completely different issue altogether – David Medenjak Apr 26 '19 at 08:53
  • I made a mistake installing the new version but leaving the old version configured in the gradle file. Works now. Thanks for the update! – Forke May 10 '19 at 18:56
0

Upgrade dagger version and kotlin version to the latest

annotation class should work.

Currently the latest version is :-

Dagger version :- 2.23.2

Kotlin version :- 1.3.41

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52