Cannot access Nullable Dagger2 Android

Viewed 3036

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.

  1. 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();
    }
    
  2. 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

  3. 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.

4 Answers

In case someone like me out there. I forgot to add @Provides annotation to the Dagger Module.

In my case I had a class with @Inject annotated constructor which extended another 3rd party class which had @Nullable annotation on one of the method parameters. Once I removed this extension everything started to work normally.

After wasting about 1 day on this issue, I found out that I need to temporarily add findbugs dependency like this:

implementation 'com.google.code.findbugs:jsr305:3.0.2'

Then dagger will print the actual error.

Related