Skip to main content

Command Palette

Search for a command to run...

When to use api over implementation.

Published
0 min read
When to use api over implementation.
D

I enjoy all things Mobile and Software Engineering. I am currently a Software Engineer and Android Google Developer Expert based in Germany 🇩🇪 but from Kenya 🇰🇪, whose primary focus has been Mobile for the past 6 years.

I share my experiences, the good, the bad, the ugly, and sometimes random tech talks :)

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

H

One question.

Why would one prefer api over implementation ?

D

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

2
H

Nice.I get it now.