Android 匕首片段中的依赖注入问题
我目前正在开发一款android应用程序,并使用dagger进行依赖注入。我在尝试将依赖项注入片段时遇到了麻烦。 我正在使用以下模块中提供的蓝牙服务Android 匕首片段中的依赖注入问题,android,dagger-2,dagger,Android,Dagger 2,Dagger,我目前正在开发一款android应用程序,并使用dagger进行依赖注入。我在尝试将依赖项注入片段时遇到了麻烦。 我正在使用以下模块中提供的蓝牙服务 @Module public abstract class BluetoothModule { @Provides @Singleton @NonNull static RxBleClient provideRxBleClient(Context context) { RxBleClient cli
@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注入fragment(使用字段注入)。如果我在主要活动中加入演示者,一切都会顺利进行
@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
蓝牙服务
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
}
@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();
}
}
地图演示者
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
}
@Module
public abstract class ActivityBuilder {
@ActivityScoped
@ContributesAndroidInjector(modules = {MainActivityModule.class, FragmentBuilder.class})
abstract MainActivity bindMainActivity();
}
AppComponent
public class MapFragment extends BaseFragment {
@Inject
MapPresenter mapPresenter;
@Inject
public MapFragment() {
// Required empty public constructor
}
//code omitted for clarty
}
@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();
}
}
@Singleton
@组件(模块={
AndroidSupportInjectionModule.class,
ApplicationModule.class,
ActivityBuilder.class,
BluetoothModule.class,
RestClientModule.class,
RxModule.class,
apimule.class,
RoomDatabaseModule.class
})
公共接口应用程序组件扩展了AndroidJector{
无效注入(波形应用);
@凌驾
无效注入(DaggerApplication实例);
@组件生成器
界面生成器{
@装订
建造商申请书(申请书);
ApplicationComponent build();
}
}
我非常感谢您提供解决此问题的任何想法,谢谢您的帮助您可能忘了在活动子组件中包含片段模块。你应该有这样的东西:
abstract class MapFragmentBuilderModule {
@ContributesAndroidInjector(modules = {MapFragmentModule.class})
abstract MapFragment bindMapFragment();
}
然后在活动模块中:
@ActivityScoped
@ContributesAndroidInjector(modules = [MainModule::class, MapFragmentBuilderModule::class])
internal abstract fun mainActivity(): MainActivity
您是否尝试将@Inject添加到要注入的依赖项的构造函数中?是的,当然,我的依赖项已将@Inject添加到构造函数中。不幸的是,情况并非如此。正如您所介绍的,我已经更新了代码,以便您可以自己查看。如果有任何更多的代码,你想让我提供,请这样说。