What's the difference between Compile and Implementation in the Android Studio build.gradle file?

2

When I'm going to add some library manually to the Android Studio 3 build.gradule (Module: app) file, I use the implementation format >, because this is the way that Android Studio itself uses by default, but whenever I search the internet for a specific resource, I find examples using the compile format.

In all cases I always replace compile with implementation and never gave a problem, but I'm curious to know the difference between the two formats of adding libraries to the project.

dependencies {
    //...
    implementation 'com.google.android.gms:play-services:11.8.0'
    //...
}

dependencies {
    //...
    compile 'com.google.android.gms:play-services:11.8.0'
    //...
}
    
asked by anonymous 29.01.2018 / 00:04

2 answers

1

I found the answer in Stackoverflow in English, the translation follows:

It is one of the breakthrough changes that comes with gradle: 3.0 that Google

The% configuration is now obsolete and should be replaced with compile or implementation

From the documents of gradle :

dependencies {
    api 'commons-httpclient:commons-httpclient:3.1'
    implementation 'org.apache.commons:commons-lang3:3.5' 
}
     

Dependencies that appear in the api settings will be exposed   libraries and, as such, will   in the consumer compilation classpath.

     

The dependencies found in the api configuration, for   the other hand, they will not be exposed to consumers and therefore will not be   leaks in the consumer compilation classpath. This comes with   several benefits:

     

Dependencies do not leak into the consumer classpath ranking anymore,   then you will never accidentally depend on a dependency   faster compilation thanks to the reduced size of the   classpath less recompilations when implementation dependencies   change: consumers would not need to be recompiled publication   cleaner: When used in conjunction with the new maven-publish plugin,   Java libraries produce POM files that   needed to compile against the library and what it is   required to use the library at run time (in other   words, do not mix what you need to compile your own   library and what is needed to compile against the library).

     

The build configuration still exists, but should not be used as it will not offer the guarantees offered by implementation and    api .

Just replace:

  • % by% by% by%
  • % by% by% by%
  • % by% by% by%
  • % by% by% by%

Clarifying: The consumer is the module that uses the library. In the case of Android, it is the Android application.

implementation x compile : If your application depends on the x library, it depends on y, z. If you use implementation only x api will be exposed, but if you use testCompile y, z will also be exposed.

Source: What's the difference between implementation and compile in gradle Ask

    
29.01.2018 / 00:22
3

The compile setting is deprecated

It's one of the great changes coming from gradle: 3.0, which google announced in 2017 on google I / O

The compile setting is now deprecated and you should replace with implementation or api . Where api configuration should be used to declare dependencies on which to export from the API library, whereas implementation must be used to declare dependencies in which the component is used internally. Example:

dependencies {
   api 'commons-httpclient:commons-httpclient:3.1'
   implementation 'org.apache.commons:commons-lang3:3.5'
}

Dependencies that appear in the api setting will be transitively exposed to the library's consumers and as such will appear on the consumer compile path.

Dependencies found in the implementation setting, on the other hand, will not be exposed to consumers and therefore will not be leaked to the consumer compilation classpath. This allows faster compilation thanks to the reduced size of the classpath.

If you have a knowledge of English, here's the documentation .

Replace in your code:

  • compile by implementation
  • testCompile by testImplementation
  • debugCompile by debugImplementation
  • androidTestCompile by androidTestImplementation

This article talks a little about this and others changes that occurred in the new version. In addition to showing how to migrate from an older version of gradle to the most current one.

  

The Compile setting still exists, but should not be used because it does not provide the guarantees that the api and implementation settings provide.

    
29.01.2018 / 00:16