Android Gradle: When to use what?

Android Gradle: When to use what?
COMMENTS (0)
Tweet

 

 

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.

Implementation Vs API:

Assume that we have four library modules:

  • LibCarA
  • LibEngineA
  • LibCarB
  • LibEngineB

The dependency tree would somewhat look like this:

Implementation_Vs_API

(Scenario 1) Using ‘implementation’ Keyword to import library:

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.

 

(Scenario 2) Using ‘api’ Keyword to import library:

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.

 

Why use Implementation instead of API ?

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.
Implementation_Vs_API

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.
Implementation_Vs_API

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]

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812