Search code examples
javaandroiddagger-2rx-android

error in returning MembersInjectors.injectMembers


I'm working on an Android application which has dagger dependencies. When injecting class through constructor injection it's throwing an error that it cannot find symbol. If I provide the dependency through an @Provides method defined inside a module, everything works fine.

import java.util.List;
import rx.Observable;
import rx.Observer;
import javax.inject.Inject;
/**
 * Created by ibrahim on 22/05/18.
 * get injected View that MainView
 */
public class BakePresenter extends BasePresenter<MainView> implements Observer<BakingResponse> {


  @Inject
  protected BakeApiService mApiService;

  @Inject
  protected BakeMapper mBakeMapper;

  @Inject
  public BakePresenter() {
    }
    public void geBaking() {
      getView().onShowDialog("Loading.....");

      Observable<BakingResponse> bakePresenterObservable= mApiService.getBake();
      subscribe(bakePresenterObservable,this);
  }

  @Override
  public void onCompleted() {
    getView().onShowToast();
    getView().onHideDialog("Loading completed.....");

  }

  @Override
  public void onError(Throwable e) {
    getView().onShowToast();
    getView().onHideDialog("Loading error....."+String.valueOf(e.getMessage()));

  }

  @Override
  public void onNext(BakingResponse bakingResponse) {
    List<Bake> bakeList= mBakeMapper.mapBake(bakingResponse);
    getView().onBakeLoaded(bakeList);
  }
}

this is Bakemodule class

@Module
public class BakeModule {

    private MainView mView;

    public BakeModule (MainView view){
        this.mView=view;
    }


    @PreActivity
    @Provides
    MainView provideMainView(){
        return mView;
    }

 
    BakeApiService provideBakeApiService(Retrofit retrofit){
        return retrofit.create(BakeApiService.class);
    }
   
    @PreActivity
    @Provides
    public BakeApiService bakeApiService(final MembersInjector<BakeApiService> injector, final Retrofit retrofit) {
        Log.d("service", "station service created");
        BakeApiService bakeApiService = provideBakeApiService(retrofit) ;

        injector.injectMembers(bakeApiService);
        return bakeApiService;
    }
}

this is BakeComponent

@PreActivity
@Component (modules = BakeModule.class,dependencies = ApplicationComponent.class)
public interface BakeComponents {

    /**
     * @param activity the Components want inject into inside MainActivity
     */


    void inject(MainActivity activity);
    MainView gMainView();
}

this is error

this image for all class with error

this is build.gradle

apply plugin: 'com.android.application'


android {
    compileOptions.incremental = false

    compileSdkVersion 27
    defaultConfig {
        applicationId "com.ibrahim.baking_app"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
    dataBinding {
        enabled = true
    }
}

dependencies {
// Other dependencies should go here


    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'



    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.okhttp:okhttp:2.7.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'
    implementation 'io.reactivex:rxjava:1.3.0'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.github.bumptech.glide:glide:4.7.1'

    compileOnly 'org.glassfish:javax.annotation:10.0-b28'
    implementation "com.google.dagger:dagger:2.14.1"
    compileOnly 'javax.annotation:jsr250-api:1.0'
    compile 'javax.inject:javax.inject:1'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
    compileOnly 'org.glassfish:javax.annotation:10.0-b28'


    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:support-annotations:27.1.1'
    annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.4.0'

    //Unit Testing
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'org.mockito:mockito-core:2.18.3'
    androidTestImplementation 'org.powermock:powermock-module-junit4:1.6.2'
    androidTestImplementation 'org.powermock:powermock-api-mockito:1.6.2'
}

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


Solution

  • implementation "com.google.dagger:dagger:2.14.1"
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
    

    You are using mismatched versions of the Dagger annotation processor and library. Though you should never use a different code generator version than the library you include, you are encountering this specific problem because as of Dagger 2.14, MembersInjector.injectMembers was removed, and code generated by Dagger 2.14 and beyond does not refer to that method.

    As in the Dagger readme file, delete those two lines and add these instead, which will keep you on the latest version of Dagger 2:

    implementation 'com.google.dagger:dagger:2.x'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.x'