I am writing an app where I am using Dagger2 for dependency Injection. I am new to dagger. I am getting confused on how it works and I am not able to figure it out how to use this library. I have tried writing below modules and components with the help of examples in blogs. I have mentioned the purpose of that module. Please correct me if I am wrong.
App Module and Component : It should generate a singleton instance across the application and I want to use this instance to access some android resources
@Module public class AppModule { private BaseApplication baseApplication; public AppModule(BaseApplication baseApplication){ this.baseApplication = baseApplication; } @Provides @Singleton BaseApplication providesApplication(){ return baseApplication; } } @Singleton @Component(modules = {AppModule.class}) public interface AppComponent { BaseApplication getBaseApplication(); }Instantiation of AppComponent
public class BaseApplication extends Application { private AppComponent appComponent; @Override public void onCreate() { super.onCreate(); appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build(); } public AppComponent getAppComponent(){ return appComponent; } }The above code works fine. I am getting problems in the below code
Location Module and Component : Is it Ok if I don't have a constructor for the below module?
@Module public class LocationModule { @IntDef( {LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, LocationRequest.PRIORITY_HIGH_ACCURACY, LocationRequest.PRIORITY_LOW_POWER, LocationRequest.PRIORITY_NO_POWER}) @Retention(RetentionPolicy.SOURCE) public @interface LocationPriority {} @Provides @Singleton @Named("default") LocationRequest providesLocationRequest(){ return new LocationRequest() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(Constants.LOCATION_UPDATE_INTERVAL_MS) .setFastestInterval(Constants.LOCATION_UPDATE_FASTEST_INTERVAL_MS) .setSmallestDisplacement(Constants.LOCATION_UPDATE_DISPLACEMENT_METERS); } @Provides @Singleton @Named("custom") LocationRequest providesCustomLocationRequest(int interval, int fastestInterval, @LocationPriority int priority, float updateDisplacement) { return new LocationRequest() .setPriority(priority) .setInterval(interval) .setFastestInterval(fastestInterval) .setSmallestDisplacement(updateDisplacement); } @Provides @Singleton LocationSettingsRequest providesLocationSettingsRequest(LocationRequest locationRequest){ return new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest) .build(); } @Provides @Singleton FusedLocationProviderClient providesFusedLocationClient(BaseApplication baseApplication){ return LocationServices.getFusedLocationProviderClient(baseApplication); } @Provides @Singleton SettingsClient providesSettingsClient(BaseApplication baseApplication){ return LocationServices.getSettingsClient(baseApplication); } } @Singleton @Component(modules = {LocationModule.class, AppModule.class}) public interface LocationComponent { **Do we Really need a method here?** void inject(GetLocationUseCase getLocationUseCase); }
I am getting the following error
error: cannot access Nullable class file for javax.annotation.Nullable not found
after using @Inject in below class. If I remove @Inject the error goes away.
public final class GetLocationUseCase implements
UseCaseContract.BusinessLogic {
UseCaseContract.Mediator mediator;
@Inject
FusedLocationProviderClient fusedLocationProviderClientl;
@Override
public void onInitialize() {
BaseApplication application = mediator.requestUserContext();
DaggerLocationComponent.builder().appModule(new
AppModule(application)).locationModule(new
LocationModule()).build().inject(this);
}
Why am I getting that error? Any help would be appreciated. Thanks in advance.