Dagger2错误:组件依赖于非分层范围排序中的范围组件

时间:2016-11-13 10:19:39

标签: android dagger-2

有人可以帮我解决这个问题吗,我正在尝试将dagger2添加到我的应用程序中以解决此问题。

Error:(14, 1) error: com.bmx.presentation.authentication.login.LoginComponent depends on scoped components in a non-hierarchical scope ordering:
@com.bmx.di.scope.ActivityScope com.bmx.data.remote.AuthenticationRepositoryComponent
@com.bmx.di.scope.ActivityScope com.bmx.presentation.authentication.login.LoginComponent


@Module
public class AppModule {
  private Application mApplication;
    public AppModule(Application mApplication) {
        this.mApplication = mApplication;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }

    @Provides
    @Singleton
    public Resources provideResources(Context context) {
        return context.getResources();
    }
}
---------------------------------
@Module
public class NetModule {
    String mBaseUrl;

    public NetModule(String mBaseUrl) {
        this.mBaseUrl = mBaseUrl;
    }
    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(application.getCacheDir(), cacheSize);
        return cache;
    }
    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        return gsonBuilder.create();
    }
    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.cache(cache);
        return client.build();
    }
    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }
    @Provides
    @Singleton
    public BmxRestService provideBmxRestService(Retrofit retrofit) {
        return retrofit.create(BmxRestService.class);
    }
}
---------------------------------------------
@Singleton
@Component(modules = {NetModule.class,AppModule.class})
public interface ApiComponent {
    BmxRestService getBmxRestService();
    Application getApplication();
}
-----------------------------------------------------
public interface AuthenticationRepository {
   Observable<List<UserProfile>> doLogin(String userName, String password);

   Observable<List<UserProfile>> doSignUp(String firstName, String lastName,String email,String password);

}
--------------------------------------------------------------
public class AuthenticationRepositoryImpl implements AuthenticationRepository {
        private BmxRestService BmxRestService;
        @Inject
        public AuthenticationRepositoryImpl(@NonNull final BmxRestService BmxRestService) {
            this.BmxRestService = BmxRestService;
        }
     //implementation
}
-------------------------------------------
@ActivityScope
@Module
public class AuthenticationRepositoryModule {
    @Provides
    @ActivityScope
    public AuthenticationRepository provideAuthenticationRepository(BmxRestService BmxRestService) {
        return new AuthenticationRepositoryImpl(BmxRestService);
    }
}
-----------------------------
@ActivityScope
@Component(modules = {AuthenticationRepositoryModule.class},dependencies = ApiComponent.class)
public interface AuthenticationRepositoryComponent {
    AuthenticationRepository getAuthenticationRepository();
}
-----------------------------------
@Module
public class LoginPresenterModule {

    private final LoginContract.View mView;

    public LoginPresenterModule(LoginContract.View mView) {
        this.mView = mView;
    }

    @Provides
    @ActivityScope
    LoginContract.View provideLoginContractView(){
        return this.mView;
    }

}
----------------------------------------------

@ActivityScope
@Component(dependencies = AuthenticationRepositoryComponent.class, modules = LoginPresenterModule.class)
public interface LoginComponent {
    void inject(LoginActivity activity);
}

----------------------------------------
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter{

    private  Scheduler mainScheduler, ioScheduler;

    private  AuthenticationRepository mAuthenticationRepository;

    @Inject
    public LoginPresenter(@NonNull final AuthenticationRepository authenticationRepository,@NonNull final LoginContract.View loginView) {
        super(loginView);
        this.mView  =loginView;
        this.mAuthenticationRepository =authenticationRepository;
        this.mainScheduler = AndroidSchedulers.mainThread();
        this.ioScheduler = Schedulers.io();
    }
  //implementation

}

----------------------
public class LoginActivity extends AppCompatActivity implements LoginContract.View {

    private static final String TAG = "LoginActivity";

    @Inject
    LoginPresenter mLoginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
  }
}
-----------------------
public class BmxApp extends Application {

    private ApiComponent apiComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        initializeNetComponent();
    }

    private void initializeNetComponent() {
        apiComponent =   DaggerApiComponent.builder().netModule(new NetModule(BuildConfig.BASEURL)).appModule(new AppModule(this)).build();
    }

    public ApiComponent getApiComponent() {
        return apiComponent;
    }

}

---------------
@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

0 个答案:

没有答案