在提到https://dagger.dev/multibindings.html时,有一节谈到了@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);
}
}不知何故,我从来没有让它工作过。
我可以向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发布于 2019-10-09 15:17:52
为了理解AutoAnnotation和Dagger2是如何工作的,我首先需要理解AutoValue
AutoValue sample: error: cannot find symbol class AutoValue_Animal
然后是AutoAnnotation
What is @AutoAnnotation for? How could it be used?
在此之后,我可以使用AutoAnnotation探索上面的Dagger 2示例。
简而言之,AutoAnnotation是一个Java代码生成器库,它生成可用于多绑定工作等值注释键(因为Java类不像Kotlin数据类,因此需要这样的工具来使其更容易)。
谷歌的AutoValue文档给出的例子并不是开箱即用的。需要进行几次修改,例如: 1.必须公开MyComponentTest以及函数。2.测试代码不应该在AutoAnnotation文件夹中,而应该在实际的源文件夹中。3.为了让AutoAnnotation与Dagger 2协同工作,我们需要以下设置
android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true我用https://github.com/elye/demo_android_dagger_autoannotation编写了一个示例代码
https://stackoverflow.com/questions/58256068
复制相似问题