在参考https://dagger.dev/multibindings.html时,有一节讨论了@AutoAnnotationclass MyComponentTest { @Test void testMyComponent() { MyComponent myComponent = DaggerMyComponent.create(); assertThat(myComponent.myKeyStringMap() .get(createMyKey("abc", Abc.class, new int[] {1, 5, 10})) .isEqualTo("foo"); } @AutoAnnotation static MyKey createMyKey(String name, Class<?> implementingClass, int[] thresholds) { return new AutoAnnotation_MyComponentTest_createMyKey(name, implementingClass, thresholds); }}不知怎的,我从来没有让它发挥作用。我要添加以下内容到 gradle implementation 'com.google.auto.value:auto-value:1.5.2' annotationProcessor 'com.google.auto.value:auto-value:1.5.2'并且还添加 android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
1 回答

慕神8447489
TA贡献1780条经验 获得超1个赞
简而言之,AutoAnnotation 是一个 Java 代码生成器库,它生成可用于多重绑定工作的值对等注释键(因为 Java 类不像 Kotlin 数据类,因此需要这样的工具来使其值对等更容易)。
Google 的 AutoValue 文档给出的示例并不是开箱即用的。需要进行一些修改,例如 1. 必须将 MyComponentTest 以及函数公开。2. AutoAnnotation 代码不应位于测试文件夹中,而应位于实际的源文件夹中。3. 为了使 AutoAnnotation 能够与 Dagger 2 配合使用,我们需要以下设置
android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
我在https://github.com/elye/demo_android_dagger_autoannotation中制作了示例代码
添加回答
举报
0/150
提交
取消