模块必须设置匕首2

时间:2018-05-24 17:20:15

标签: java android dependency-injection kotlin dagger-2

我正在尝试使用带有kotlin的dagger 2创建依赖项。我在运行时收到此错误

  

引起:java.lang.IllegalStateException:必须设置pk.telenorbank.easypaisa.di.modules.RetrofitApiModule           在pk.telenorbank.easypaisa.di.DaggerAppComponent $ Builder.build(DaggerAppComponent.java:54)           在pk.telenorbank.easypaisa.EasypaisaApp.onCreate(EasypaisaApp.kt:22)           在android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1015)           在android.app.ActivityThread.handleBindApplication(ActivityThread.java:4834)           在android.app.ActivityThread.access $ 1600(ActivityThread.java:168)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1440)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:150)           在android.app.ActivityThread.main(ActivityThread.java:5659)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:822)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)

这是依赖图。

@Module(includes = arrayOf(NetworkModule::class))
class RetrofitApiModule(val retrofitMvpApi: RetrofitMvpApi) {
    @Provides
    @Singleton
    fun provideMvpApi(): RetrofitMvpApi {
        return retrofitMvpApi
    }
}

这是RetorfitMvpApi

@Singleton
class RetrofitMvpApi @Inject constructor(retrofit: Retrofit) : MvpApi {

    var retrofitService: RetrofitService

    init {
        retrofitService = retrofit.create(RetrofitService::class.java)
    }

    override fun login(source: String) =
            retrofitService.getPosts(source, Constants.API_KEY)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .map { rs -> rs }
                    .doOnError { t -> t.printStackTrace() }


    interface RetrofitService {
        @POST
        fun getPosts(@Query("sources") source: String,
                     @Query("apiKey") key: String): Observable<WSResponse>
    }
}

这是组件。

@Singleton
@Component(modules = arrayOf(AppModule::class, RetrofitApiModule::class))
interface AppComponent {

    fun loginComponent(loginModule: LoginModule) : LoginComponent
}

我在这里做错了什么?我正在使用dagger 2.15

2 个答案:

答案 0 :(得分:1)

您的provideMvpApi方法应该使用retrofitMvpApi的实例并返回其界面:

@Module(includes = arrayOf(NetworkModule::class))
class RetrofitApiModule() {
    @Provides
    @Singleton
    fun provideMvpApi(val retrofitMvpApi: RetrofitMvpApi): MvpApi {
        return retrofitMvpApi
    }
}

答案 1 :(得分:1)

如果Module具有默认构造函数,Dagger将自动为依赖图创建Module。如果您使用自定义构造函数,则在构建图形时必须提供Module

对于java代码:

@Module
public class ContextModule {

  private final Context context;

  public ContextModule(Context context) {
      this.context = context;
  }

  @Provides
  @GithubApplicationScope
  @ApplicationContext
  public Context provideContext(){
      return context.getApplicationContext();
  }

}

构建图表:

    githubApplicationComponent = DaggerGithubApplicationComponent.builder()
        .contextModule(new ContextModule(this))
        // not needed as Dagger automatically generate module class with no arguments
        //.networkModule(new NetworkModule())
        .build();