Dagger2 注释处理器不工作

Dagger2 annotation processor is not working

我尝试在我的 Android 项目中使用 Dagger2; 当我使用 apt 时,一切都是 right.But AndroidStudio 3.0 不支持 apt,所以我使用注释 processor.But 单击 "Make Project" 后没有创建 Dagger2 代码; 而且我确定在 AndroidStudio 中启用了注释处理,因为 Butterknife 注释处理器没问题。 接下来是 build.gradle:

dependencies {
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
    compile 'com.google.dagger:dagger-android:2.13'
    compile 'com.google.dagger:dagger-android-support:2.13'
    compile 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}

这些是 dagger 2 和 butter 在 android studio 3.0 中的依赖项

  //ButterKniffe
compile "com.jakewharton:butterknife:8.8.1"
kapt "com.jakewharton:butterknife-compiler:8.8.1"

//dagger
compile "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"

gradle:3.0 带来的重大变化之一是 Google announced at IO17 gradle:3.0

编译配置为now deprecated,应替换为实现api 来自 gradle docs :

Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers.

Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:

  • List item dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
  • faster compilation thanks to reduced classpath size
  • less recompilations when implementation dependencies change: consumers would not need to be recompiled
  • cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).

The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

在 android studio 3.0 版本

中使用它
implementation 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'