Grzegorz Gajewski
Grzegorz Gajewski

Reputation: 43

Adding dependencies to the android library 'api' vs 'implementation'

Let's assume that I'm creating Android library and want to use ConstraintLayout. I see two different approaches by other lib creators:

1. Add it with implementation keyword. The downside is if someone adds my lib to project and doesn't have constraint-layout in dependencies the build will fail with errors like that: error: attribute 'com.example.test:layout_constraintBottom_toBottomOf' not found.

2. Add it with 'api'. That way it compiles fine, but if any other library uses constraint-layout with other versions it can produce error Android dependency 'com.android.support.constraint:constraint-layout:1.1.2' has different version for the compile (1.0.0) and runtime (1.1.2)

Which way has the lowest maintenance cost for the clients code?

Upvotes: 1

Views: 366

Answers (1)

Peter Ledbrook
Peter Ledbrook

Reputation: 4482

It's not about the lowest maintenance cost for the client code. It's about whether the client needs the transitive dependency — ConstraintLayout — in order to compile against your library. Whether you use implementation or api, the transitive dependency will always be a runtime dependency of the client.

Here's the question you need to ask yourself: are you using ConstraintLayout or any other elements of the transitive library in the method signatures or properties of your library's public API, i.e. the stuff that's used by the client? If yes, then you should declare the constraint layout library as api. Otherwise, use implementation.

Upvotes: 1

Related Questions