我目前正在开发一个android应用,并使用匕首进行依赖注入。尝试将依赖项注入片段时遇到麻烦。 我正在使用以下模块中提供的蓝牙服务。
@Module
public abstract class BluetoothModule {
@Provides
@Singleton
@NonNull
static RxBleClient provideRxBleClient(Context context) {
RxBleClient client = RxBleClient.create(context);
RxBleClient.setLogLevel(RxBleLog.VERBOSE);
return client;
}
@Binds
@Reusable
@Production
abstract IBluetoothService provideBlueToothService(BluetoothService bluetoothService);
@Binds
@Reusable
@Test
abstract IBluetoothService provideMockBlueToothService(MockBluetoothService bluetoothService);
}
由于我使用的是MVP模式,因此我想将此服务注入Presenter(构造函数注入),然后将Presenter注入片段(使用字段注入)。如果我将主持人注入到主要活动中,一切都会很好。
@Module
public abstract class MainActivityModule {
@Provides
@ActivityScoped
static MainActivityPresenter provideMainActivityPresenter(
SchedulerProvider schedulerProvider,
@Test IBluetoothService bluetoothService) {
return new MainActivityPresenter(schedulerProvider, bluetoothService);
}
}
但是,当我将BluetoothService int FragmentPresenter注入时
@Module
public abstract class MapFragmentModule {
@Provides
@Reusable
static MapPresenter provideMapPresenter(SchedulerProvider provider,
@Test IBluetoothService bluetoothService) {
return new MapPresenter(provider,bluetoothService);
}
}
我有以下错误
error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] com.wavy.services.bluetooth.IBluetoothService cannot be provided without an @Provides-annotated method.
public interface ApplicationComponent extends AndroidInjector<DaggerApplication>{
^
com.wavy.services.bluetooth.IBluetoothService is injected at
com.wavy.ui.map.MapPresenter.<init>(…, bluetoothService)
com.wavy.ui.map.MapPresenter is injected at
com.wavy.ui.map.MapFragment.mapPresenter
com.wavy.ui.map.MapFragment is injected at
com.wavy.ui.MainActivity.mapFragment
com.wavy.ui.MainActivity is injected at
dagger.android.AndroidInjector.inject(T)
component path: com.wavy.config.dagger.components.ApplicationComponent → com.wavy.config.dagger.builders.ActivityBuilder_BindMainActivity.MainActivitySubcomponent
所以我在github上发现了与我类似的问题 Issue Link
解决方案是不要在Fragment构造函数中使用@Inject批注,所以我做到了,之后又遇到另一个错误
error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] com.wavy.ui.map.MapFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public interface ApplicationComponent extends AndroidInjector<DaggerApplication>{
^
A binding with matching key exists in component: com.wavy.config.dagger.builders.FragmentBuilder_BindMapFragment.MapFragmentSubcomponent
com.wavy.ui.map.MapFragment is injected at
com.wavy.ui.MainActivity.mapFragment
com.wavy.ui.MainActivity is injected at
dagger.android.AndroidInjector.inject(T)
component path: com.wavy.config.dagger.components.ApplicationComponent → com.wavy.config.dagger.builders.ActivityBuilder_BindMainActivity.MainActivitySubcomponent
这就是我提供片段的方式
@ContributesAndroidInjector(modules = {MapFragmentModule.class})
abstract MapFragment bindMapFragment();
蓝牙服务
public class BluetoothService implements IBluetoothService {
private final RxBleClient rxBleClient;
@Inject
public BluetoothService(RxBleClient rxBleClient) {
this.rxBleClient = rxBleClient;
}
//code omitted for clarity
}
地图片段
public class MapFragment extends BaseFragment {
@Inject
MapPresenter mapPresenter;
@Inject
public MapFragment() {
// Required empty public constructor
}
//code omitted for clarty
}
地图演示者
public class MapPresenter extends BasePresenter<MapFragment> {
private final IBluetoothService bluetoothService;
@Inject
public MapPresenter(SchedulerProvider schedulerProvider,
IBluetoothService bluetoothService) {
super(schedulerProvider);
this.bluetoothService = bluetoothService;
}
//code ommited for clarity
}
ActivityBuilder
@Module
public abstract class ActivityBuilder {
@ActivityScoped
@ContributesAndroidInjector(modules = {MainActivityModule.class, FragmentBuilder.class})
abstract MainActivity bindMainActivity();
}
AppComponent
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
ApplicationModule.class,
ActivityBuilder.class,
BluetoothModule.class,
RetrofitRestClientModule.class,
RxModule.class,
ApiModule.class,
RoomDatabaseModule.class
})
public interface ApplicationComponent extends AndroidInjector<DaggerApplication>{
void inject(WavyApplication application);
@Override
void inject(DaggerApplication instance);
@Component.Builder
interface Builder{
@BindsInstance
Builder application(Application application);
ApplicationComponent build();
}
}
非常感谢您提出解决此问题的想法
答案 0 :(得分:0)
您可能忘记了将片段模块包括在活动子组件中。你应该有这样的东西:
abstract class MapFragmentBuilderModule {
@ContributesAndroidInjector(modules = {MapFragmentModule.class})
abstract MapFragment bindMapFragment();
}
,然后在您的活动模块中:
@ActivityScoped
@ContributesAndroidInjector(modules = [MainModule::class, MapFragmentBuilderModule::class])
internal abstract fun mainActivity(): MainActivity