Mark Lapasa Mark Lapasa - 10 days ago 5
Android Question

Dagger2: Is it possible to inject based on Android Version?

Is it possible I can use Dagger2 to inject a concrete implementation based on SDK version?

For example

// MediaPlayerComponent.class
@Component(modules = {MediaPlayerModule.class}
public interface MediaPlayerComponent
{
void inject(MediaPlayerUI ui)
}

// MediaPlayerUI.java
public class MediaPlayerUI
{
@Inject
public MediaPlayer mPlayer;
}

// GingerbreadMediaPlayer.java
public class GingerbreadMediaPlayer extends MediaPlayer {...}

// IceCreamSandwichMediaPlayer.java
public class IceCreamSandwichMediaPlayer extends MediaPlayer {...}

Answer

Yes, just decide which of both implementations should be returned in the MediaPlayerModule module in the concrete method annotated with @Provides, the one that returns a generic MediaPlayer.

Comments