I'm trying to figure out what is the difference between api
and implementation
configuration while building my dependencies.
In the documentation, it says that implementation
has better build time, but,
seeing this comment in a similar question I got to wonder if is it true.
Since I'm no expert in Gradle, I hope someone can help. I've read the documentation already but I was wondering about an easy-to-understand explanation.
Android – Gradle Implementation vs API configuration
androiddependenciesgradleimplementation
Related Topic
- Android-studio – Gradle in Android Studio
- Python – return variable outputs in Python function
- Android – Gradle DSL method not found: ‘runProguard’
- Android – No matching client found for package name (Google Analytics) – multiple productFlavors & buildTypes
- Gradle – What’s the difference between implementation, api and compile in Gradle
Best Answer
Gradle
compile
keyword was deprecated in favor of theapi
andimplementation
keywords to configure dependencies.Using
api
is the equivalent of using the deprecatedcompile
, so if you replace allcompile
withapi
everything will works as always.To understand the
implementation
keyword consider the following example.EXAMPLE
Suppose you have a library called
MyLibrary
that internally uses another library calledInternalLibrary
. Something like this:Suppose the
MyLibrary
build.gradle
usesapi
configuration independencies{}
like this:You want to use
MyLibrary
in your code so in your app'sbuild.gradle
you add this dependency:Using the
api
configuration (or deprecatedcompile
) you can accessInternalLibrary
in your application code:In this way the module
MyLibrary
is potentially "leaking" the internal implementation of something. You shouldn't (be able to) use that because it's not directly imported by you.The
implementation
configuration was introduced to prevent this. So now if you useimplementation
instead ofapi
inMyLibrary
:you won't be able to call
InternalLibrary.giveMeAString()
in your app code anymore.This sort of boxing strategy allows Android Gradle plugin to know that if you edit something in
InternalLibrary
, it must only trigger the recompilation ofMyLibrary
and not the recompilation of your entire app, because you don't have access toInternalLibrary
.When you have a lot of nested dependencies this mechanism can speed up the build a lot. (Watch the video linked at the end for a full understanding of this)
CONCLUSIONS
When you switch to the new Android Gradle plugin 3.X.X, you should replace all your
compile
with theimplementation
keyword *(1). Then try to compile and test your app. If everything it's ok leave the code as is, if you have problems you probably have something wrong with your dependencies or you used something that now is private and not more accessible. *Suggestion by Android Gradle plugin engineer Jerome Dochez (1))If you are a library mantainer you should use
api
for every dependency which is needed for the public API of your library, while useimplementation
for test dependencies or dependencies which must not be used by the final users.Useful article Showcasing the difference between implementation and api
REFERENCES (This is the same video splitted up for time saving)
Google I/O 2017 - How speed up Gradle builds (FULL VIDEO)
Google I/O 2017 - How speed up Gradle builds (NEW GRADLE PLUGIN 3.0.0 PART ONLY)
Google I/O 2017 - How speed up Gradle builds (reference to 1*)
Android documentation