When to use api over implementation.

Recently on my android adventures, I decided to split up a project at work into modules and make my life simpleršŸ˜ƒ.

One of my modules is a themes module that cuts across all modules providing the necessary themes, styles and colours to other components, but creating the module and implementing it in other modules as shown didn't serve me as I had expected.

dependencies{
  implementation project(':themes')
}

crop-ttScreenshot from 2019-09-24 14-10-07.png

This was because my themes module was using the material design library which was not implemented in the other modules.

themes module build.gradle

So the next logical option might have been to go onto each module and implement the material design library above over and over again but this seemed extremely redundant.

Where api comes in...

From the Gradle documentation:

The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.

Therefore in my case, I need to make the material design library accessible across my modules by just adding the themes module, and this is done simply by changing the implementation keyword to api as shown below and voila..its donešŸ„³

crop-tttScreenshot from 2019-09-24 14-16-36.png

Resources

Gradle Docs

Comments (3)

Harun Wangereka's photo

One question.

Why would one prefer api over implementation ?

David Odari's photo

from my case I found it useful only when you need to expose a library to other modules,for example lets say a library like firebase ui built on top of other firebase libraries other than implementing all this libraries again while you can use api to expose them to other modules in the event you need to go beyond the abstraction

Harun Wangereka's photo

Nice.I get it now.