gradlebuild.gradledependency-managementgradle-plugintransitive-dependency

What's the difference between implementation, api and compile in Gradle?


After updating to Android Studio 3.0 and creating a new project, I noticed that in build.gradle there is a new way to add new dependencies instead of compile there is implementation and instead of testCompile there is testImplementation.

Example:

 implementation 'com.android.support:appcompat-v7:25.0.0'
 testImplementation 'junit:junit:4.12'

instead of

 compile 'com.android.support:appcompat-v7:25.0.0'
 testCompile 'junit:junit:4.12'

What's the difference between them and what should I be using?


Solution

  • tl;dr

    Just replace:

    It is one of the breaking changes coming with Android Gradle plugin 3.0 that Google announced at IO17.

    The compile configuration is now deprecated and should be replaced by implementation or api

    From the Gradle documentation:

    dependencies {
        api("commons-httpclient:commons-httpclient:3.1")
        implementation("org.apache.commons:commons-lang3:3.5")
    }
    

    Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers.

    Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:

    • dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
    • faster compilation thanks to reduced classpath size
    • less recompilations when implementation dependencies change: consumers would not need to be recompiled
    • cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that Distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).

    The compile configuration still exists, but should not be used as it will not offer the guarantees that the api and implementation configurations provide.


    Note: If you only use a library in your app module -the common case- you won't notice any difference.
    you will only see the difference if you have a complex project with modules depending on each other, or you are creating a library.


    What is transitive dependency?

    "transitivity" refers to the ability of a dependency to include its own set of dependencies.

    For example, if Project A depends on Project B, and Project B depends on Project C, then Project A transitively depends on Project C.

    When you declare a dependency with implementation, the transitive dependencies are not exposed to other modules that depend on this module.

    When you declare a dependency with api, the transitive dependencies are exposed, meaning that other modules that depend on this module will also have access to the transitive dependencies.