DaggerAppComponent未创建

随着匕首2.10我曾经能够通过做创建应用程序组件

sAppComponent = DaggerAppComponent.builder() .appModule(new AppModule(this)) .sessionModule(new SessionModule()) .netModule(new NetModule()) .dataModule(new DataModule()) .build(); 

我已经使用AndroidInjector的活动,一切都很好。 现在我切换到2.11,我无法find创建应用程序组件的方式。 在谷歌教程中,我看到:

 DaggerYourApplicationComponent.create() .inject(this); 

被添加到应用程序的onCreate中。 在我的情况DaggerYourApplicationComponent = DaggerAppComponent 。 问题是DaggerAppComponent类不再被创建。

我有:

 public class App extends android.support.multidex.MultiDexApplication implements HasActivityInjector { @Inject DispatchingAndroidInjector mDispatchingActivityInjector; @Override public void onCreate() { super.onCreate(); sAppComponent = DaggerAppComponent.create().inject(this); //here the error 

和:

 @Singleton @Component(modules = { AppModule.class, MainActivityModule.class, ... }) public interface AppComponent { void inject(App app); ... } 

在build.gradle文件中我有:

 def daggerVer = 2.11 compile "com.google.dagger:dagger:$daggerVer" compile "com.google.dagger:dagger-android-support:$daggerVer" annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer" 

好的对不起,噪音。 我愚蠢的错误:当我切换到2.11我复制build.gradle部分从谷歌教程的依赖:

 annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer" 

没有列出我不知道的原因。 下面列出的依赖关系一切正常:

 def daggerVer = 2.12 // or latest version implementation "com.google.dagger:dagger:$daggerVer" implementation "com.google.dagger:dagger-android-support:$daggerVer" annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer" annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer" 

如果你正在使用Kotlin

 apply plugin: 'kotlin-kapt' dependencies { implementation "com.google.dagger:dagger:$daggerVer" implementation "com.google.dagger:dagger-android-support:$daggerVer" kapt "com.google.dagger:dagger-android-processor:$daggerVer" kapt "com.google.dagger:dagger-compiler:$daggerVer" } 

Kotlin中 ,我们必须添加kapt编译器插件来使用Dagger 2。

在你的应用程序gradle中,添加这个插件

 apply plugin: 'kotlin-kapt' 

并添加依赖如下

 dependencies { implementation "com.google.dagger:dagger:$latest_version" kapt "com.google.dagger:dagger-compiler:$latest_version" implementation "com.google.dagger:dagger-android:$latest_version" kapt "com.google.dagger:dagger-android-processor:$latest_version" implementation "com.google.dagger:dagger-android-support:$latest_version" kapt "com.google.dagger:dagger-android-support:2.12" } 

请参阅Kotlin 文档