Java @Dagger 2多重绑定教程的自动批注不起作用。如何让它工作?
在参考中,有一节讨论了Java @Dagger 2多重绑定教程的自动批注不起作用。如何让它工作?,java,android,annotations,dagger-2,Java,Android,Annotations,Dagger 2,在参考中,有一节讨论了@AutoAnnotation class MyComponentTest { @Test void testMyComponent() { MyComponent myComponent = DaggerMyComponent.create(); assertThat(myComponent.myKeyStringMap() .get(createMyKey("abc", Abc.class, new int[] {1, 5, 10})
@AutoAnnotation
class 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);
}
}
并加上
android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
要了解AutoAnnotation和Dagger 2的工作原理,首先我需要了解AutoValue 然后是自动注释 之后,我可以使用AutoAnnotation来探索上面的Dagger 2示例 简而言之,AutoAnnotation是一个Java代码生成器库,它生成可用于多绑定工作的值等价的注释键(因为Java类与Kotlin数据类不同,因此需要这样的工具来简化值等价) 谷歌的AutoValue文档给出的示例并非开箱即用。需要进行一些修改,例如:。 1.必须公开MyComponentTest以及函数。 2.自动注释代码不应该在测试文件夹中,而应该在实际的源文件夹中。 3.为了让AutoAnnotation与Dagger 2一起工作,我们需要以下设置
android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
特别感谢@jeff bowman帮助解释了中的自动注释工作,我在中制作了一个示例代码
android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true