Error: cannot find symbol variable DaggerAppComponent
Error: cannot find symbol variable DaggerAppComponent
在尝试集成最新的 Dagger 2 版本时,我遇到了 Dagger 自动生成的问题。尽管有几个 Rebuilds 和 Make Module App 过程,但 Dagger 不会自动生成 DaggerAppComponent。
申请class:
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'
在这方面的任何帮助将不胜感激。
把代码写到这里,
private void initAppComponent()
{
/* DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();*/
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent .inject(this)
}
其他的是
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent
{
void inject(BaseApplication application);
}
为什么需要在构建组件的地方注入相同的 class 您可以轻松地在应用程序 class 上获取上下文和应用程序。 Dagger 可以帮你找个依赖class.
好像我使用了错误的依赖项:
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'
添加以下依赖项为我解决了 Java 项目
的问题
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
对于 Kotlin 项目(添加 kotlin-kapt
之后):
kapt "com.google.dagger:dagger-compiler:2.27"
我遇到了同样的问题...解决我问题的方法实际上是将视图模型添加到 ViewmodelModulle,然后将注释 @Inject 添加到我的视图模型的构造函数中。这对你来说可能是一个不同的问题,但在我的情况下,这真的很有帮助。我的代码编译没有任何问题
@Inject <----- 这是构造函数中遗漏的。
public MessageViewModel(Application application) {
super(application);
mApp = application;
你需要这两行
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
使用 kotlin 时使用 kapt 而不是 annotationProcessor。
生成了class像Dagger+AppComponentClass,
eg:DaggerAppComponent
尝试,转到文件并
Invalidate and Restart
1.Clean 项目
2.Rebuild
3.File-->失效并重启
需要运行你的APP一次生成"DaggerAppComponent"
在尝试集成最新的 Dagger 2 版本时,我遇到了 Dagger 自动生成的问题。尽管有几个 Rebuilds 和 Make Module App 过程,但 Dagger 不会自动生成 DaggerAppComponent。
申请class:
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'
在这方面的任何帮助将不胜感激。
把代码写到这里,
private void initAppComponent()
{
/* DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();*/
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent .inject(this)
}
其他的是
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent
{
void inject(BaseApplication application);
}
为什么需要在构建组件的地方注入相同的 class 您可以轻松地在应用程序 class 上获取上下文和应用程序。 Dagger 可以帮你找个依赖class.
好像我使用了错误的依赖项:
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'
添加以下依赖项为我解决了 Java 项目
的问题annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
对于 Kotlin 项目(添加 kotlin-kapt
之后):
kapt "com.google.dagger:dagger-compiler:2.27"
我遇到了同样的问题...解决我问题的方法实际上是将视图模型添加到 ViewmodelModulle,然后将注释 @Inject 添加到我的视图模型的构造函数中。这对你来说可能是一个不同的问题,但在我的情况下,这真的很有帮助。我的代码编译没有任何问题
@Inject <----- 这是构造函数中遗漏的。
public MessageViewModel(Application application) {
super(application);
mApp = application;
你需要这两行
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
使用 kotlin 时使用 kapt 而不是 annotationProcessor。 生成了class像Dagger+AppComponentClass, eg:DaggerAppComponent
尝试,转到文件并
Invalidate and Restart
1.Clean 项目 2.Rebuild 3.File-->失效并重启
需要运行你的APP一次生成"DaggerAppComponent"