从1.3.21更新到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.

推荐答案

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

Kotlin相关问答推荐

DataSnapshot.getValue()未记录的奇怪行为

判断字符串是否除了.&" ",","@""""

如何在Jetpack Compose中从领域查询中读取数据?

Kotlin-stdlib中的模拟扩展函数

Kotlin编译器如何决定是否可以在任何给定点调用Suspend方法?

&x是T&q;和&q;(x为?T)!=空(&Q;)?

在 Kotlin 中将 Array 转换为 IntArray 时丢失值

Java/Kotlin中类似Rust般的注释编译?

将 SharedPreferences 中的值公开为流

如何将jooq multiset的结果映射到Hashmap(Java Map)?

使用纯 Kotlin 函数作为 Junit5 方法源

如何为 material.Slider 视图创建绑定适配器?

来自类型参数的属性的自定义 getter

如何修复 ViewPager2 中的Design assumption violated错误?

将 Firebase 数据快照反序列化为 Kotlin 数据类

以编程方式重新启动 Spring Boot 应用程序/刷新 Spring 上下文

Kotlin/JS,Gradle 插件:无法加载@webpack-cli/serve命令

Kotlin中具有多个参数的绑定适配器

将字符串编码为Kotlin中的UTF-8

判断EditText是否为空kotlin android