Dagger 2 Provides with Kotlin Anonymous functions Build Fail

With Dagger and kotlin. When i compile the following code.

@Module
 class NetworkModule {

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

}

The Build Fails with following unclear error :

     /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)

After 2 days of struggle, I figured out that it was due to assigning anonymous function to the provides function. I updated the code and removed the anonymous function then it compiled without any error.

I hope this will help others who are struggling with the same issue.

Working Code.

@Module
class NetworkModule {

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

Can anyone explain why the previous code with anonymous function did not compile?

1 answer

  • answered 2018-10-17 08:42 EpicPandaForce

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

    Vs.

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

    The second one would work.