Dagger 2提供Kotlin匿名功能Build Fail

时间:2018-10-17 07:58:07

标签: android dependency-injection kotlin dagger-2

和匕首和科特林一起。 当我编译以下代码时。

@Module
 class NetworkModule {

@Provides
fun retrofit() =  {
     Retrofit.Builder()
            .baseUrl(Constant.URL.BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
  }

}

构建失败,并出现以下不清楚的错误:

     /Users/saurabh/Saurabh/google-sample-mvp-rxjava/UserListApp-AndroidClient-Kotlin/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/com/example/saurabh/userappmvp/databinding/ItemUserBinding.java:49: error: cannot find symbol
  protected ItemUserBinding(DataBindingComponent _bindingComponent, View _root,
                            ^
  symbol:   class DataBindingComponent
  location: class ItemUserBinding
e: /Users/saurabh/Saurabh/google-sample-mvp-rxjava/UserListApp-AndroidClient-Kotlin/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/com/example/saurabh/userappmvp/databinding/ItemUserBinding.java:80: error: cannot find symbol
      boolean attachToRoot, @Nullable DataBindingComponent component) {
                                      ^
  symbol:   class DataBindingComponent
  location: class ItemUserBinding
e: /Users/saurabh/Saurabh/google-sample-mvp-rxjava/UserListApp-AndroidClient-Kotlin/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/com/example/saurabh/userappmvp/databinding/ItemUserBinding.java:91: error: cannot find symbol
      @Nullable DataBindingComponent component) {
                ^
  symbol:   class DataBindingComponent
  location: class ItemUserBinding
e: /Users/saurabh/Saurabh/google-sample-mvp-rxjava/UserListApp-AndroidClient-Kotlin/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/com/example/saurabh/userappmvp/databinding/ItemUserBinding.java:99: error: cannot find symbol
  public static ItemUserBinding bind(@NonNull View view, @Nullable DataBindingComponent component) {
                                                                   ^
  symbol:   class DataBindingComponent
  location: class ItemUserBinding
e: /Users/saurabh/Saurabh/google-sample-mvp-rxjava/UserListApp-AndroidClient-Kotlin/app/build/tmp/kapt3/stubs/debug/com/example/saurabh/userappmvp/dependency/UserComponent.java:9: error: [Dagger/MissingBinding] com.example.saurabh.userappmvp.datasource.remote.UserOperation cannot be provided without an @Provides-annotated method.
    public abstract void inject(@org.jetbrains.annotations.NotNull()
                         ^
      com.example.saurabh.userappmvp.datasource.remote.UserOperation is injected at
          com.example.saurabh.userappmvp.datasource.remote.RemoteDbHelper.<init>(userOperation)
      com.example.saurabh.userappmvp.datasource.remote.RemoteDbHelper is injected at
          com.example.saurabh.userappmvp.dependency.RepositoryModule.remoteDataSource(remoteDbHelper)
      @javax.inject.Named("remote") com.example.saurabh.userappmvp.datasource.UserRepositoryContract is injected at
          com.example.saurabh.userappmvp.datasource.UserRepository.<init>(…, remote)
      com.example.saurabh.userappmvp.datasource.UserRepository is injected at
          com.example.saurabh.userappmvp.userlist.presentation.UserListFragment.repository
      com.example.saurabh.userappmvp.userlist.presentation.UserListFragment is injected at
          com.example.saurabh.userappmvp.dependency.UserComponent.inject(com.example.saurabh.userappmvp.userlist.presentation.UserListFragment)

经过2天的努力,我发现这是由于将匿名功能分配给了Provides功能。我更新了代码并删除了匿名函数,然后对其进行了编译,没有任何错误。

我希望这会帮助其他在同一问题上苦苦挣扎的人。

工作代码。

@Module
class NetworkModule {

@Provides
fun retrofit() : Retrofit {
    return Retrofit.Builder()
            .baseUrl(Constant.URL.BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
}   
}

谁能解释为什么以前带有匿名功能的代码无法编译?

1 个答案:

答案 0 :(得分:0)

fun retrofit(): () -> Retrofit =  {
     Retrofit.Builder()
          .baseUrl(Constant.URL.BASE_URL)
          .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
          .addConverterFactory(GsonConverterFactory.create())
          .build()
  }

fun retrofit(): Retrofit =  
     Retrofit.Builder()
          .baseUrl(Constant.URL.BASE_URL)
          .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
          .addConverterFactory(GsonConverterFactory.create())
          .build()

第二个可以工作。