Mark Lapasa Mark Lapasa - 1 year ago 79
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 {...}

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download