What is the difference between Compile and Implementation in the Android Studio build.Radle file?

Asked

Viewed 3,752 times

4

Whenever I will add some library manually in the file build.Gradle (Module: app) of Android Studio 3, I use the format implementation, because this is the way that Android Studio itself uses by default, but whenever I search the internet which library is needed for a specific feature, I find examples using the format Compile.

In all cases I always replace Compile for implementation and never gave problem, but I’m curious to know the difference between the two formats add 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'
    //...
}
  • As far as I know, implementation has been supported in the newer version of Gradle

2 answers

7

The configuration compile is obsolete

It’s one of the big changes coming from Gradle:3.0, which Google announced in 2017 on google I/O

The configuration compile is now deprecated and you must replace by implementation or api. Where the setting api should be used to declare dependencies in which they will be exported by the API library, while implementation should be used to declare dependencies in which they are of internal component use. Example:

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

The dependencies that appear in the settings of api will be transitively exposed to library consumers and as such will appear on the consumer’s compilation path.

Dependencies found in the implementation, on the other hand, they will not be exposed to consumers and therefore will not be leaked into the consumers' compilation classpath. This allows a fastest compilation thanks to the reduced size of the classpath.

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

Replace in your code:

  • Compile by implementation
  • testCompile by testImplementation
  • debugging
  • androidTestCompile by androidTestImplementation

This article speaks a little of this and other changes that occurred in the new version. In addition to showing how to migrate from an older version of Gradle to the more current one.

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

2


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

It is one of the break changes that comes with Gradle: 3.0 that Google announced on IO17 Gradle: 3.0

To compile configuration is now obsolete and should be replaced by implementation or api

Of documents of Gradle :

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

Dependencies that appear in settings api will be exposed transitively to library consumers and as such will appear in the consumer compilation classpath.

Dependencies found in configuration implementation, for on the other hand, they will not be exposed to consumers and therefore will not be leaked into the consumer build classpath. This comes with several benefits:

Dependencies do not leak in the classpath ranking of consumers anymore, then you will never accidentally depend on a dependency transitive faster build thanks to reduced size of the classpath less recompilations when deployment dependencies change: consumers would not need to be recompiled publishing cleaner: when used in conjunction with the new plugin Maven-Publish, Java libraries produce POM files that distinguish exactly the which is necessary to compile against the library and what is needed to use the library at runtime (in other words, do not mix what is necessary to compile the 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 api and implementation.

Just replace:

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

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

implementation x api: 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 api y, z will also be exposed.

Source: What’s the Difference between implementation and Compile in Gradle Ask

Browser other questions tagged

You are not signed in. Login or sign up in order to post.