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)

public class MediaPlayerUI
public MediaPlayer mPlayer;

public class GingerbreadMediaPlayer extends MediaPlayer {...}

public class IceCreamSandwichMediaPlayer extends MediaPlayer {...}


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.