【问题标题】:dagger 2 not generating component class匕首2不生成组件类
【发布时间】:2018-01-17 12:00:21
【问题描述】:

我第一次尝试在我的一个应用中使用 dagger 2。我在匕首组件类中遇到错误,因为该类没有生成。请帮我让它工作。

链接到我的应用https://github.com/kantigaricharan/Zolo

这是错误

    02:45:55.663 [ERROR] [system.err] /Users/vamsikrishna/Downloads/Zolo/app/src/main/java/com/example/saicharan/zolo/dagger/component/AppComponent.java:17: error: com.example.saicharan.zolo.dashboard.DashboardInteractorImpl cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
02:45:55.663 [ERROR] [system.err]     void inject(MyApp myApp);
02:45:55.663 [ERROR] [system.err]          ^
02:45:55.663 [ERROR] [system.err]       com.example.saicharan.zolo.dashboard.DashboardInteractorImpl is injected at
02:45:55.663 [ERROR] [system.err]           com.example.saicharan.zolo.MyApp.dashboardInteractor
02:45:55.664 [ERROR] [system.err]       com.example.saicharan.zolo.MyApp is injected at
02:45:55.664 [ERROR] [system.err]           com.example.saicharan.zolo.dagger.component.AppComponent.inject(myApp)
02:45:55.722 [ERROR] [system.err] 1 error

这是我在匕首中的组件类

   @Singleton @Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApp myApp);
    void inject(DashboardInteractorImpl dashboardInteractorImpl);
}

这是模块

    @Module
public class AppModule {
    private final MyApp myApp;
    public AppModule(MyApp myApp){this.myApp=myApp;}

    @Provides @Singleton
    Context providesApplicationContext(){
        return myApp;
    }
    @Provides @Singleton
    SessionManagement getSession(Context context){
        return  new SessionManagement(myApp);
    }
    @Provides @Singleton
    DatabaseHelper getDhelper(Context context){
        return new DatabaseHelper(myApp);
    }
}

应用类

    public class MyApp extends Application {

    protected AppComponent appComponent;
    private static MyApp instance;
    @Inject
    DashboardInteractorImpl dashboardInteractor;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
       // return (MyApp) context.getApplicationContext();
          return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
        appComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .build();
        appComponent.inject(this);
    }
    public AppComponent getAppComponent(){
        return appComponent;
    }
}

DasboardInteractorImpl

   public class DashboardInteractorImpl implements DashboardInteractor {

    private final DatabaseHelper dHelper;
    private final SessionManagement sManager;
    DashboardPresenterImpl mDashboardPresenter;

    public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
        this.mDashboardPresenter=mDashboardPresenter;

        dHelper = new DatabaseHelper(MyApp.getContext());
        sManager= new SessionManagement(MyApp.getContext());
    }
     //SOME LOGIC HERE..
}

我能知道我的应用出了什么问题吗?

【问题讨论】:

  • 您在创建模块和组件后是否重新构建了项目?
  • 去掉应用插件:'com.neenbedankt.android-apt' - 如果可能的话,你所有的apt 都应该替换为annotationProcessor
  • @eurosecom 是的,我已经这样做了,但仍然没有解决错误
  • @DavidRawson 当我尝试同步 gradle 时,它​​现在给 Gradle dsl 方法找不到合适的方法
  • 你仍然有 apt 用于 ButterKnife apt 'com.jakewharton:butterknife-compiler:8.8.0' - 检查 ButterKnife 页面以获取正确的行以放入 gradle

标签: android dagger-2 dagger


【解决方案1】:

当您希望 Dagger 2 为您提供类时,请确保使用 @Inject 注释对构造函数进行注释:

@Inject
public DashboardInteractorImpl(DashboardPresenterImpl mDashboardPresenter){
    this.mDashboardPresenter=mDashboardPresenter;

    dHelper = new DatabaseHelper(MyApp.getContext());
    sManager= new SessionManagement(MyApp.getContext());
}

另外,去掉在你的应用中注入的行:

@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
   void inject(MyApp myApp);

   //delete the line below:
   //void inject(DashboardInteractorImpl dashboardInteractorImpl);
}

您还需要确保 Dagger 2 可以提供 DashboardPresenterImpl,方法是使用 @Inject 注释演示者的构造函数或在模块中编写 @Provides 方法。

【讨论】:

  • 我在演示者和交互者中都添加了@Inject,现在它给出了错误
  • DashboardView 不能在没有@Provides-annotated 方法的情况下提供。
  • 仪表板视图只是一个界面
  • 让我做一件事,我会在 github 上更新我的代码,这样你就可以在那里查看我的整个代码。
  • 你在吗??
【解决方案2】:

尝试使用以下修改

// Component
@Singleton @Component(modules = AppModule.class)
public interface AppComponent {
    void inject(MyApp myApp);

    DashboardInteractorImpl getDashboardInteractorImpl();
}

// Module
@Module 
public class AppModule { 
    private final MyApp myApp;
    public AppModule(MyApp myApp){this.myApp=myApp;}

    @Provides @Singleton 
    Context providesApplicationContext(){
        return myApp;
    } 
    @Provides @Singleton 
    SessionManagement getSession(Context context){
        return  new SessionManagement(context);
    } 
    @Provides @Singleton 
    DatabaseHelper getDhelper(Context context){
        return new DatabaseHelper(context);
    } 
}

// Application
public class MyApp extends Application {

    protected AppComponent appComponent;
    private static MyApp instance;
    @Inject 
    DashboardInteractorImpl dashboardInteractor;

    public static MyApp getInstance() { 
        return instance;
    } 

    public static Context getContext(){
       // return (MyApp) context.getApplicationContext(); 
          return instance.getApplicationContext();
    } 

    @Override 
    public void onCreate() { 
        instance = this;
        super.onCreate(); 
        appComponent = DaggerAppComponent
                .builder() 
                .appModule(new AppModule(this))
                .build(); 
        appComponent.inject(this);
    } 
    public AppComponent getAppComponent(){ 
        return appComponent;
    } 
} 

// DashboardInteractorImpl
public class DashboardInteractorImpl implements DashboardInteractor {

    private final DatabaseHelper dHelper;
    private final SessionManagement sManager;
    private DashboardPresenterImpl mDashboardPresenter;

    @Inject 
    public DashboardInteractorImpl(DatabaseHelper databaseHelper,
                SessionManagement sessionManagement
                DashboardPresenterImpl dashboardPresenter){

        dHelper = databaseHelper;
        sManager = sessionManagement;
        mDashboardPresenter = dashboardPresenter;
    }
     //SOME LOGIC HERE..
}

// DashboardPresenterImpl
public class DashboardPresenterImpl implements DashboardPresenter {

    @Inject     
    public DashboardPresenterImpl(ABC abc){
        //make sure ABC is provided by the module or inject on constructed
    }
}

【讨论】:

  • 我将在 github 上分享我的项目.. 你可以看看它。请纠正我??.. 理解匕首 2 对我来说非常重要,而我没有完全理解它。
  • @charanreddy 你读过我在 Dagger 2 上的所有 3 篇文章吗:blog.mindorks.com/…
【解决方案3】:

您可以通过两种方式注入DashboardInteractorImpl

您必须在 App 模块中声明 DashboardInteractorImpl 依赖关系。

或者

你可以写@Inject构造函数。 我没有看到任何完全填充此依赖项的代码。 所以你可以做两件事 在模块中提供此DashboardPresenterImpl, DashboardInteractorImpl 要么 为每个此类DashboardPresenterImplDashboardInteractorImpl 创建@Inject 构造函数。

【讨论】:

  • 在演示者和交互者的构造函数上使用@Inject 后.. 我得到循环依赖错误
  • 您能否将代码下载到您的桌面并在您的 android studio 上试用。了解如何在我的应用程序上实现 dagger 对我来说非常重要。
  • 您的演示者是否依赖于 DashboardInteractorImpl ?循环依赖意味着两者都是相互中继的。
  • 请通过代码一次..这样你就可以清楚地了解我想要做什么并纠正我
  • 我没有找到任何具有 DashboardInteractorImpl 的构造注入或模块。
【解决方案4】:

如果您在 Kotlin 中使用或编写代码,请在 build.gradle 中添加此行代码

apply plugin: 'kotlin-kapt'

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多