我使用改造和dagger2设置了MVVM体系结构,但是当我运行时,发现了此异常
错误:[ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor无法处理此问题 接口,因为不是所有的依赖关系都可以解决。校验 编译错误或生成代码的循环依赖性。
AppModule.java
@Module(subcomponents = ViewModelSubComponent.class)
public class AppModule {
@Singleton
@Provides
ApiService provideService(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(BaseRequest.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
.create(ApiService.class);
}
@Provides
CompositeDisposable provideCompositeDisposable() {
return new CompositeDisposable();
}
@Provides
@Singleton
public StethoInterceptor getSteltho() {
return new StethoInterceptor();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Application application, StethoInterceptor stethoInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
okHttpClient.connectTimeout(CommonUtils.TIMEOUT_IN_SEC, TimeUnit.SECONDS);
okHttpClient.readTimeout(CommonUtils.TIMEOUT_IN_SEC, TimeUnit.SECONDS);
okHttpClient.addInterceptor(getIntercepter(application));
if (BuildConfig.DEBUG) {
okHttpClient.addNetworkInterceptor(stethoInterceptor);
okHttpClient.interceptors().add(httpLoggingInterceptor);
}
return okHttpClient.build();
}
public Interceptor getIntercepter(final Application application) {
Interceptor headerAuthorizationInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
if (!CommonUtils.isNetworkConnected(application)) {
Request request = chain.request();
CacheControl cacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build();
request = request.newBuilder().cacheControl(cacheControl).build();
String rawJson = chain.proceed(request).body().string();
Log.e("TAG", String.format("req response cache raw JSON response is: %s", rawJson));
return chain.proceed(request);
} else {
CacheControl cacheControl = new CacheControl.Builder().maxAge(1, TimeUnit.HOURS).build();
Request.Builder request = chain.request().newBuilder();
request.addHeader("Accept", "application/json");
//request.addHeader("Authorization", mAuth);
request.header("Cache-Control", "public, max-age=" + 90);
request.header(CACHE_CONTROL, cacheControl.toString());
Response response = chain.proceed(request.build());
return response;
}
}
};
return headerAuthorizationInterceptor;
}
@Provides
@Singleton
public HttpLoggingInterceptor httpLoggingInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return httpLoggingInterceptor;
}
/*@Singleton
@Provides
ProjectRepository getrepository() {
return new ProjectRepository();
}*/
@Singleton
@Provides
ViewModelProvider.Factory provideViewModelFactory(ViewModelSubComponent.Builder viewModelSubComponent) {
return new ViewModelFactory(viewModelSubComponent.build());
}
MainActivityModule.java
@Module
public abstract class MainActivityModule {
@ContributesAndroidInjector(modules = FragmentBuildersModule.class)
public abstract MainActivity contributeMainActivity(); // @ContributesAndroidInjector(modules = FragmentBuildersModule.class)
}
FragmentBuildersModule.ktx
@Module
abstract class FragmentBuildersModule {
@ContributesAndroidInjector
internal abstract fun contributeHomeFragment(): HomeFragment
//@ContributesAndroidInjector
//abstract SeriesFragment contributeSeriesFragment();
}
ViewModelFactory.java
@Singleton
public class ViewModelFactory implements ViewModelProvider.Factory {
private final ArrayMap<Class, Callable<? extends ViewModel>> creators;
@Inject
public ViewModelFactory(ViewModelSubComponent viewModelSubComponent) {
creators = new ArrayMap<>();
// View models cannot be injected directly because they won't be bound to the owner's view model scope.
creators.put(ProjectViewModel.class, () -> viewModelSubComponent.homeViewModel());
// creators.put(ProjectListViewModel.class, () -> viewModelSubComponent.projectListViewModel());
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
Callable<? extends ViewModel> creator = creators.get(modelClass);
if (creator == null) {
for (Map.Entry<Class, Callable<? extends ViewModel>> entry : creators.entrySet()) {
if (modelClass.isAssignableFrom(entry.getKey())) {
creator = entry.getValue();
break;
}
}
}
if (creator == null) {
throw new IllegalArgumentException("Unknown model class " + modelClass);
}
try {
return (T) creator.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
AppComponent.java
@Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, MainActivityModule.class})//, MainActivityModule.class
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(MyApplication appApplication);
}
AppInjector.java
public class AppInjector {
private static Application application;
private AppInjector() { }
public static void init(MyApplication appApplication) {
application = appApplication;
DaggerAppComponent.builder().application(appApplication).build().inject(appApplication);
appApplication.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
handleActivity(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});
}
private static void handleActivity(Activity activity) {
if (activity instanceof HasSupportFragmentInjector) {
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity) {
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentCreated(FragmentManager fm, Fragment fragment, Bundle savedInstanceState) {
if (fragment instanceof Injectable) {
AndroidSupportInjection.inject(fragment);
}
}
}, true);
}
}
}
MyApplication.java
public class MyApplication extends Application implements HasActivityInjector {
private static PrefManager mPref;
private static MyApplication myApplication;
private Context context;
//private String dbUrl = "";
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
public static MyApplication getInstance() {
return myApplication == null ? new MyApplication() : myApplication;
}
@Override
public void onCreate() {
super.onCreate();
this.context = this;
MultiDex.install(this);
AppInjector.init(this);
Fresco.initialize(this);
RemoteConfig.initConfig();
mPref = new PrefManager(this);
debugToolsInitilization();
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
添加MainActivityModule模块时引发异常