作者:mobiledu2502931997 | 来源:互联网 | 2023-01-28 19:32
在尝试集成最新的Dagger 2版本时,我遇到了Dagger自动生成的问题.尽管有几个Rebuild和Make Module App进程,但Dagger并没有自动生成DaggerAppComponent.
申请类:
public class BaseApplication extends Application
{
private AppComponent appComponent;
@Override
public void onCreate()
{
super.onCreate();
initAppComponent();
}
private void initAppComponent()
{
DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public AppComponent getAppComponent()
{
return appComponent;
}
}
AppComponent
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent
{
void inject(BaseApplication application);
}
的AppModule:
@Module
public class AppModule
{
private BaseApplication application;
public AppModule(BaseApplication app)
{
application = app;
}
@Provides
@Singleton
Context provideContext()
{
return application;
}
@Provides
Application provideApplication()
{
return application;
}
}
使用的依赖:
compile 'com.google.dagger:dagger-android:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'
在这方面的任何帮助将受到高度赞赏.
1> Ashwini Kuma..:
好像我使用了错误的依赖项:
compile 'com.google.dagger:dagger-android:2.x'
compile 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
如果您在dagger.android中使用类,则应使用上述依赖项.
正确的依赖关系是:
compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'