错误:找不到符号变量DaggerAppComponent

Ash*_*mar 10 android dependency-injection dagger dagger-2

在尝试集成最新的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

AppComponent

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent
{
    void inject(BaseApplication application);
}
Run Code Online (Sandbox Code Playgroud)

的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用的依赖:

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'
Run Code Online (Sandbox Code Playgroud)

在这方面的任何帮助将受到高度赞赏.

Ash*_*mar 23

好像我使用了错误的依赖项:

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'
Run Code Online (Sandbox Code Playgroud)

如果您在dagger.android中使用类,则应使用上述依赖项.

正确的依赖关系是:

compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
Run Code Online (Sandbox Code Playgroud)

  • 我已经看过很多次了 - 使用“dagger.android 中的类”是什么意思? (2认同)

Ash*_*eez 6

你需要这两行

implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
Run Code Online (Sandbox Code Playgroud)

使用 kotlin 时使用 kapt 而不是 annotationProcessor。生成的类如 Dagger+AppComponentClass,例如:DaggerAppComponent


Gau*_*ari 5

添加以下依赖项解决了我的 Java 项目问题

annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
Run Code Online (Sandbox Code Playgroud)

对于 Kotlin 项目(添加后kotlin-kapt):

kapt "com.google.dagger:dagger-compiler:2.27"
Run Code Online (Sandbox Code Playgroud)