implementation vs api in Gradle
Understand how dependency visibility and build performance change when using implementation vs api.
In Android interviews, one common question is the difference between implementation and api in Gradle. Understanding the difference is important, as it impacts both dependency visibility and build performance.
Let’s understand with the example.
Example: A project with two modules: “core” and “ui”. The “ui” module has a dependency on the “core”. And, the “core” module has a dependency on “logging-library”.
ui → core → logging-library
implementation
ui → core → implementation(logging-library)
- The dependency is only accessible to the module where it is declared. 
- If logging-library is declared as implementation in the core, the ui module will not have access to it. 
- Faster build times, as changes in the dependency do not trigger recompilation of dependent modules. 
api
ui → core → api(logging-library)
- The dependency is accessible to the module where it is declared and to all modules that depend on this module. 
- If logging-library is declared as api in the core, the ui module will have access to it and can use its API. 
- Slower build times compared to implementation, as changes in the dependency may trigger recompilation of all dependent modules. 
Final note: Use implementation by default for better performance, and switch to api only when exposure is necessary. Choosing wisely as it impacts both dependency visibility and build performance.
Prepare for your Android Interview: Android Interview Questions and Answers
Thanks
Amit Shekhar
Founder, Outcome School



Well explained 👌
That was great, thanks!