Kamilski81
Kamilski81

Reputation: 15117

How do make Dagger optional when buildling an SDK in Android/Java?

I am an SDK that will be public for other developers. I have build my SDK using Dagger thus far, but I wouldn't want developers to rely on Dagger when using the SDK. Is there a way to set the @Inject dependencies based on whether dagger has been enabled or not? Are there any good solutions for this?

For example, I would like this to be injected if the user is using Dagger, otherwise somehow be set by my system.

@Inject
SensorManager mSensorManager;

Upvotes: 0

Views: 277

Answers (1)

Gregor Zeitlinger
Gregor Zeitlinger

Reputation: 439

You could check if the dagger.ObjectGraph class can be loaded - and only then call ObjectGraph.create.

If dagger is not included, the mSensorManager would be null.

Having said that, it seems to be fragile setup to make a decision based on the existence of a library in the classpath. However, without more context, it's hard to say what a better solution would be.

Upvotes: 1

Related Questions