在參考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
提交
取消