On updating Android Gradle plugin 3.0 in your project, you might have noticed that you are getting warning:
“Warning:Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be removed at the end of 2018”
This is because compile keyword has been now deprecated in favour of implementation and api but directly changing compile to implementation gives you dependency errors. You also can’t use api everywhere or you will get dexMerger errors.
In this blog, we will discuss both API and Implementation and when to use what.
Assume that we have four library modules:
The dependency tree would somewhat look like this:
Now if we include LibEngineA in LibCarA as implementation project(‘:LibEngineA’) And we add LibCarA in our app module than app module won’t be able to use LibEngineA functionalities directly it can only access LibCarA functions.
Now let’s import LibEngineB in LibCarB using api keyword as api project(‘:LibEngineB’) And import LibCarB in our AppModule. You will be able to use LibCarB and LibEngineB functionalities as well.
Let’s understand the difference on compilation level;s that why using implementation makes build time faster than API.
In the first scenario where we are importing LibEngineA in LibCarA using keyword implementation. Any change in LibEngineA gradle will only recompile LibEngineA and LibCarA. Also using implementation won’t conflict with imported library modules, other dependencies if the same library is imported in both modules thus there won’t be any dexMerger issues. As any other class which does not import LibEngineA directly cannot use any implementation of it.
In the second scenario where we are importing LibEngineB in LibCarB using api. If any change is implemented inside LibEngineB, Gradle needs to recompile LibEngineB, LibCarB and all other modules which import LibCarB as any other module might use implementation of LibEngineB.
So in conclusion its preferred to use implementation and to avoid API keyword until or unless it’s really necessary. It improves build time and when working on projects with lots of modules dependent on each other it will help a lot in resolving dex merger issues to find the perfect api and implementation keyword use.
Please feel free to reach out if you have any questions. In case you need any help with development, installation, integration, up-gradation and customization of your Business Solutions. We have expertise in Enterprise Application Development Solutions. Connect with us for more information. [email protected]