Difference of using api or implementation in the dependencies of Android in Gradle

5

When updating Android studio to the new version 3.1.1 it has indicated me that I should change things in the dependencies compile by implementation I have uploaded the compilation version to 27 ...

But gradle when making build, it returns the error:

  

Android dependency 'com.android.support.constraint: constraint-layout'   has different version for the compile (1.0.2) and runtime (1.1.0)   classpath You should manually set the same version via   DependencyResolution

I have rebooted the cache with (File -> Invalidate Caches/Restart -> Invalidate and Restart). but it still gives the error

Of all the modules I use, only one uses constraint-layout and I have it in implementation 'com.android.support.constraint:constraint-layout:1.1.0' in principle should be well established.

Searching for SO I found the recommendation to use api before implementation

as follows

api 'com.android.support.constraint:constraint-layout:1.1.0'

And it has been solved mysteriously ...

My doubt is What is the difference in using implementation or api ?

    
asked by Webserveis 23.04.2018 в 17:05
source

1 answer

3

Something similar to yours happened to me 1 , so I was reading the section Use the new dependency settings from the Android documentation.

There they explain the difference (the bold ones are mine):

  

implementation

     

The dependency is available at the time of compilation for   the module and only at the time of execution for the consumer of the   module. For large compilations covering several projects, the   use of implementation instead of api or compile can generate   important improvements in compile time , because it reduces the   number of projects that the compilation system should return to   compile. Most app and test modules should use   this configuration .

  

api

     

The dependency is available for the module at the time of the   compilation, and it is also available to the module consumer   at the time of compilation and at the time of execution. Is   configuration has the same behavior as compile (which is now   obsolete) and you should normally use it only in the modules of the   library . App modules must use implementation , unless   that you want to expose your API to another test module.

     

As in the case of the current stable versions of the complement of   Android, the previous configurations are available for   dependencies of specific classes or compilation types. By   example, you can use api for the dependency to be available   for all variants, or redApi to make it available   only for the network variants of the module.

     

And finally, a note:

  

Note: compile , provided and apk are still available. Without   However, they will disappear in the next major version of the   Android add-on.

1 Related question: How to apply the new Android Studio dependency settings ?

    
answered by 23.04.2018 / 18:13
source