我目前正在研究在我的Android应用中使用Dagger2
。
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
我已经成功地使用以下代码@Inject
ViewModels及其相关的repositories:
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@Singleton
public class DaggerViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
public DaggerViewModelFactory(final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@SuppressWarnings("unchecked")
@Override
public <T extends ViewModel> T create(final Class<T> modelClass) {
Provider<? extends ViewModel> creator = creators.get(modelClass);
if (creator == null) {
for (Map.Entry<Class<? extends ViewModel>, Provider<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.get();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public abstract class ViewModelModule {
@Binds
abstract ViewModelProvider.Factory bindViewModelFactory(final DaggerViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(StilettoViewModel.class)
abstract ViewModel provideStilettoViewModel(final StilettoViewModel stilettoViewModel);
}
然而,我的部分ViewModel是androidx.lifecycle.AndroidViewModel
。
我创建了这个ViewModelFactory:
import android.app.Application;
import java.util.Map;
import javax.inject.Provider;
import javax.inject.Singleton;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@Singleton
public class DaggerAndroidViewModelFactory extends ViewModelProvider.AndroidViewModelFactory {
private final Application application;
private Map<Class<? extends AndroidViewModel>, Provider<AndroidViewModel>> creators;
/**
* Creates a {@code AndroidViewModelFactory}
*
* @param application an application to pass in {@link AndroidViewModel}
*/
public DaggerAndroidViewModelFactory(@NonNull final Application application) {
super(application);
this.application = application;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
return super.create(modelClass);
}
}
还有这个 Dagger 模块
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModelProvider;
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public abstract class ViewModelModule {
@Binds
abstract ViewModelProvider.AndroidViewModelFactory bindAndroidViewModelFactory(final DaggerAndroidViewModelFactory factory);
@Binds
@IntoMap
@ViewModelKey(MyAndroidViewModel.class)
abstract AndroidViewModel provideArticlesViewModel(final ArticlesViewModel articlesViewModel);
}
这段代码无法编译,因为出现了以下错误
error: [Dagger/MissingBinding] MyAndroidViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method.
是否可以在Dagger2中注入androidx.lifecycle.AndroidViewModel
?
我的实现有什么问题吗?