Android Library Dagger 2: Dependency Injection in Applications Using @Component Interface to Obtain Objects


Now that you have every connection ready, you have to obtain an instance of this interface and invoke its methods to obtain the object you need:

VehicleComponent component = Dagger_VehicleComponent.builder().vehicleModule(new VehicleModule()).build();
vehicle = component.provideVehicle();
Toast.makeText(this, String.valueOf(vehicle.getSpeed()), Toast.LENGTH_SHORT).show();

When you try to create a new object of the interface with the @Component annotation, you have to do it using the prefix Dagger_<NameOfTheComponentInterface>, in this case Dagger_VehicleComponent, and then use the builder method to call every module within.