295

I'm trying to figure it 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 a easy-to-understand explanation.

Shubhamhackz
  • 4,435
  • 2
  • 31
  • 56
reinaldomoreira
  • 3,568
  • 2
  • 12
  • 27
  • 1
    Have you read [here](https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#new_configurations)? – MatPag Jun 07 '17 at 13:27
  • as in matter of fact, I did, but, as I said, that comment made wonder about it. so I'm kinda lost now – reinaldomoreira Jun 07 '17 at 13:30
  • You probably will switch your libraries dependencies from `compile` to `api`. The libraries you use internally could use some private implementations which is not exposed in the final library so they are transparent to you. Those "internal-private" dependencies can be switched to `implementation` and when Android gradle plugin will compile your app it will skip the compilation of those dependencies resulting in a smaller build time (but those dependencies will be available at runtime). Obviously you can do the same thing if you have local module libraries – MatPag Jun 07 '17 at 13:38
  • So if I use implementation instead of compile for dependencies like v7 or any other library it is not supposed to work? because it is working as far as I tested (not that much) – reinaldomoreira Jun 07 '17 at 13:46
  • No, i think i did not explained this really well. As a rule of thumb in one of the Google I/O 2017 video, one of the Google engineers suggested to change all the `compile` statement to `implementation`, and use `api` only if you see something not working. (I will search the video for your when i have time) – MatPag Jun 07 '17 at 13:50
  • So confusing :D Thank you – reinaldomoreira Jun 07 '17 at 14:06
  • 1
    Here's a short graphical explanation of 'api' and 'implementation': https://jeroenmols.com/blog/2017/06/14/androidstudio3/ – albert c braun Jun 19 '17 at 03:58
  • 1
    that's an awesome post! thank you @albertbraun – reinaldomoreira Jun 19 '17 at 11:23
  • Reading Jeroen Mols blog helped me too. Basically if you're building an app, you won't expose the API so just use `implementation` in your apps. The difference is only there when developing libraries. – andho Aug 12 '17 at 15:27
  • This one is also a good article: https://blog.gradle.org/incremental-compiler-avoidance – Snicolas Aug 24 '17 at 21:36

5 Answers5

530

Gradle compile keyword was deprecated in favor of the api and implementation keywords to configure dependencies.

Using api is the equivalent of using the deprecated compile, so if you replace all compile with api 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 called InternalLibrary. Something like this:

    // 'InternalLibrary' module
    public class InternalLibrary {
        public static String giveMeAString(){
            return "hello";
        }
    }
    // 'MyLibrary' module
    public class MyLibrary {
        public String myString(){
            return InternalLibrary.giveMeAString();
        }
    }

Suppose the MyLibrary build.gradle uses api configuration in dependencies{} like this:

dependencies {
    api project(':InternalLibrary')
}

You want to use MyLibrary in your code so in your app's build.gradle you add this dependency:

dependencies {
    implementation project(':MyLibrary')
}

Using the api configuration (or deprecated compile) you can access InternalLibrary in your application code:

// Access 'MyLibrary' (granted)
MyLibrary myLib = new MyLibrary();
System.out.println(myLib.myString());

// Can ALSO access the internal library too (but you shouldn't)
System.out.println(InternalLibrary.giveMeAString());

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 use implementation instead of api in MyLibrary:

dependencies {
    implementation project(':InternalLibrary')
}

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 of MyLibrary and not the recompilation of your entire app, because you don't have access to InternalLibrary.

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 the implementation 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 use implementation 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

Weishi Z
  • 1,478
  • 4
  • 16
  • 36
MatPag
  • 29,651
  • 11
  • 74
  • 87
  • "Potentially a noob developer could have used the appcompat declared internally in your library instead of providing it's own dependency" Why would you declare the same dependency in multiple modules when all modules depend on a super module where you can define the dependency once? – David Jul 25 '17 at 16:58
  • 6
    I noticed that api doesn't seem to work well in library modules. If I use it, I still can't access the dependencies from my app project. I can only access the code in that library itself. – Allan W Aug 01 '17 at 20:24
  • 2
    This is fine and works on debug-builds but when using ProGuard (on release-versions) `MyLibrary#myString()` will crash because ProGuard will have `InternalLibrary` removed. What's the best-practice for android-libs to be used in ProGuard'ed apps? – hardysim Aug 23 '17 at 11:58
  • @hardysim you should use `api` for the dependencies imported in your library – MatPag Aug 23 '17 at 12:16
  • @MatPag right, that's what I got when reading the docs (only in-project-modules should use implementation). But it would be way better to have this "app needs to import everything itself" for external libs as well. I think material-dialogs does this by providing a ProGuard-keep-class-rule to keep the libs it uses. This way the 3rd-party libs are not visible to the app but still working on runtime? Is this a good way? – hardysim Aug 24 '17 at 06:15
  • 3
    I think the answer is not accurate, the application can use whatever scope it wants for the MyLibrary. It will see or not the InternalLibrary depending whether or not the MyLibrary uses api / implementation. – Snicolas Aug 24 '17 at 21:39
  • @Snicolas yes, i will clarify this updating the answer as soon as i have time to update it – MatPag Aug 24 '17 at 22:45
  • @Snicolas I've updated my answer, can you check it if now is more clear? – MatPag Aug 26 '17 at 10:04
  • 3
    thanks man. awesome explanation, much better than the one given in android's official docs – Henry May 15 '18 at 17:57
  • 3
    that's a beautiful explanation. Theory and concrete mixed brilliantly. Well done. Thanks for that – Peter Kahn Sep 13 '19 at 14:07
  • Great answer , but newbies (like me) may not distinguish between "library" and "app" rightaway that is mentioned - here's a good link that they should read to better appreciate the answer- https://docs.gradle.org/current/userguide/library_vs_application.html – Plaiska Feb 19 '20 at 16:59
  • 1
    In mylibrary.aar [MyLibrary/build/outputs/aar/ mylibrary.aar] if I open this and check for InternalLibrary class files, i don't see them. Can somebody explain this? Note: i have used ------->api project(':InternalLibrary') – Bipin May 15 '20 at 23:52
  • I have a project with an old library that only works with "compile", not "api" or "implementation". – ElYeante Apr 20 '21 at 21:53
196

I like to think about an api dependency as public (seen by other modules) while implementation dependency as private (only seen by this module).

Note, that unlike public/private variables and methods, api/implementation dependencies are not enforced by the runtime. This is merely a build-time optimization, that allows Gradle to know which modules it needs to recompile when one of the dependencies changes its API.

dev.bmax
  • 7,278
  • 2
  • 27
  • 38
  • 33
    Love the simplicity of this answer thank you very much – Kevin Gilles Jan 04 '19 at 10:04
  • 2
    The real difference (AFAICT) is that the generated pom file puts `api` dependencies in "compile" scope (they will be included as dependencies in your library and anything that depends on your library) and `implementation` dependencies in "runtime" scope (they better be on the classpath when your code is running, but they aren't needed to compile other code that uses your library). – Shadow Man Jan 23 '19 at 01:19
  • @ShadowMan It's an implementation detail of the plugin, responsible for generating the POM file, how it maps **Gradle** scopes to **Maven** scopes. – dev.bmax Jan 23 '19 at 08:16
  • 1
    You should use `implementation` for any dependency that is required to run (and for your library to compile), but that shouldn't be automagically pulled into projects that use your library. An example would be jax-rs, your library might use RESTeasy, but it shouldn't pull those libs into any project that uses your library, since they might want to use Jersey instead. – Shadow Man Jan 23 '19 at 19:37
  • 1
    this is how you know someone get its stuff :D thanks for simple and clear answer – Elias Fazel May 12 '19 at 23:11
29

Consider you have app module which uses lib1 as a library and lib1 uses lib2 as a library. Something like this: app -> lib1 -> lib2.

Now when using api lib2 in lib1, then app can see lib2 codes when using: api lib1 or implementation lib1 in the app module.

BUT when using implementation lib2 in lib1, then app can not see the lib2 codes.

Ehsan Mashhadi
  • 1,449
  • 12
  • 22
5

Answers from @matpag and @dev-bmax are clear enough to make people understand different usages between implementation and api. I just want to make an extra explaination from another angle, hopes to help for peoples that have the same question.

I created two projects for testing :

  • project A as a java library project named 'frameworks-web-gradle-plugin' depends on 'org.springframework.boot:spring-boot-gradle-plugin:1.5.20.RELEASE'
  • project B depends on project A by implementation 'com.example.frameworks.gradle:frameworks-web-gradle-plugin:0.0.1-SNAPSHOT'

The dependencies hierarchy descripted above looks like:

[project-b] -> [project-a] -> [spring-boot-gradle-plugin]

Then I tested following scenarios:

  1. Make project A depends on 'org.springframework.boot:spring-boot-gradle-plugin:1.5.20.RELEASE' by implementation .

    Run gradle dependencies command in a terminal in poject B root dir,with following screenshot of output we can see that 'spring-boot-gradle-plugin' appears in runtimeClasspath dependencies tree, but not in compileClasspath's, I think that's exactly why we can't make use of library that declared using implementation, it just won't through compilation.

    enter image description here

  2. Make project A depends on 'org.springframework.boot:spring-boot-gradle-plugin:1.5.20.RELEASE' by api

    Run gradle dependencies command in a terminal in poject B root dir again. Now 'spring-boot-gradle-plugin' appears both in compileClasspath and runtimeClasspath dependencies tree.

    enter image description here

A significant difference I noticed is that the dependency in producer/library project declared in implementation way won't appear in compileClasspath of consumer projects, so that we can't make use of corresponding lib in the consumer projects.

Rong.l
  • 148
  • 1
  • 10
3

From gradle documentation:

Let’s have a look at a very simple build script for a JVM-based project.

plugins {
    id 'java-library'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.hibernate:hibernate-core:3.6.7.Final'
    api 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.+'
}

implementation

The dependencies required to compile the production source of the project which are not part of the API exposed by the project. For example the project uses Hibernate for its internal persistence layer implementation.

api

The dependencies required to compile the production source of the project which are part of the API exposed by the project. For example the project uses Guava and exposes public interfaces with Guava classes in their method signatures.

Camilo Silva
  • 6,674
  • 4
  • 33
  • 52