The Android Gradle plugin 3.0 introduces the implementation and api keywords, replacing the deprecated compile keyword, to improve build times and manage dependencies more effectively.
Abstract
In Android Gradle plugin 3.0, the compile keyword has been replaced with implementation and api. The api keyword functions similarly to the old compile keyword, while implementation offers a more efficient way to manage dependencies. Using implementation instead of api can significantly improve build times in projects with multiple modules, as it limits the scope of dependencies to the module that directly imports them. This prevents unnecessary recompilation of other modules when changes are made to a specific module's implementation.
Opinions
The implementation keyword in Android Gradle plugin 3.0 is beneficial for projects with multiple modules, as it can significantly improve build times.
Using implementation instead of api limits the scope of dependencies to the module that directly imports them, preventing unnecessary recompilation of other modules.
The api keyword functions similarly to the old compile keyword, maintaining compatibility with existing projects.
Replacing all instances of compile with implementation and then addressing any leaking dependencies with the api keyword is recommended for a smooth transition to the new dependency management system.
The article suggests that the new dependency management system can lead to a more efficient and faster build process, especially in larger projects with many modules.
The author encourages readers to try replacing compile with implementation in their projects and to share their thoughts and experiences.
The article includes a sample project demonstrating the differences between api and implementation, as well as build reports comparing the two approaches.
Implementation Vs Api in Android Gradle plugin 3.0
While using Android Gradle plugin 3.0 in your project, you might have noticed that compile keyword has been now deprecated in favour of implementation and api. Let’s understand both of them with an example.
Let us assume a project with four library modules.
LibraryA
LibraryB
LibraryC
LibraryD
For which the dependency tree looks like this:
All Library modules contain a simple class file.
LibraryD:
LibraryC:
LibraryB:
LibraryA:
From above Class files, it is observed that LibraryA and LibraryB is dependent on LibraryC and LibraryD respectively. Therefore, these dependencies needs to be added to build.gradle files.
Compile (2.0) or Api (3.0):
New api keyword is exactly the same as the old compile keyword. Thus, if all compile is replaced with api, it works just fine. Now, let’s add dependency of LibraryD using api keyword in LibraryB.
dependencies {
. . . .
api project(path: ':libraryD')
}
Similarly, LibraryB is added to the app module.
dependencies {
. . . .
api project(path: ':libraryB')
}
Now, both LibraryB and LibraryD can be accessed from the app module. In sample App, both libraries are accessed like this:
Implementation (3.0):
It’s time to find out how implementation is different than api. Again back to the example, this time let’s import LibraryC to LibraryA using implementation keyword.
Now, if we try to access LibraryC from app module, Android studio will throw an error.
Which implies that LibraryC can’t be accessed directly from App module if we use implementation instead of api. So, what is the benefit of this?
Implementation vs api:
In the first scenario, LibraryD is compiled by using api. If any change is implemented inside LibraryD, gradle needs to recompile LibraryD, LibraryB and all other modules which import LibraryB as any other module might use implementation of LibraryD.
First Scenario
But in second scenario, if any implementation in LibraryC is changed, Gradle just needs to recompile LibraryC and LibraryA as any other class which does not import LibraryCdirectly cannot use any implementation of it.
Second Scenario
If you are working on a project with lots of modules,(we have heard some crazy build time story in thisFragmented Podcast episode) this strategy can speed up the build process significantly. I tried this on our sample project and there was a slight improvement of few seconds. Here are the build reports for all scenarios.
Full Build:
Change in LibraryD:
Change in LibraryC:
TL;DR:
Just replace all compile with the implementation and try to build the project. If it builds successfully, well and good. otherwise look for any leaking dependency you might be using and import those libraries using api keyword.
If you liked this article please click the 💚 below to recommend. I would love to hear your thoughts in the comment section or at Twitter. Thanks a lot !