Speeding Up Your Android Gradle Builds (Google I/O '17)

By: Android Developers

295   3   16826

Uploaded on 05/19/2017

Build speed impacts your productivity as a developer. The Android Gradle build system is flexible and powerful, but could also be complex. In this session, we will discuss how the Android Gradle build system works and explore new strategies to improve build performance. You will walk away with practical tips on how to accelerate your own project builds and increase your developer productivity!

Watch more Android talks at I/O '17 here: https://goo.gl/c0LWYl
See all the talks from Google I/O '17 here: https://goo.gl/D0D4VE

Subscribe to the Android channel: http://goo.gl/GEh1ds

#io17 #GoogleIO #GoogleIO2017

Comments (8):

By anonymous    2017-09-20

I think this topic needs a bit more coverage because maybe is not so immediate for every developers.

Gradle compile keyword has been deprecated in favor of the new api and implementation keywords.

I will not explain api, because it's the same thing as using the old compile, so if you replace all your compile with api everything will works as always.

To understand the implementation keyword we need an example.

EXAMPLE

We have this library called MyLibrary where internally we are using another library called InternalLibrary. Something like this:

//internal library module
public class InternalLibrary {
    public static String giveMeAString(){
        return "hello";
    }
}

//my library module
public class MyLibrary {
    public String myString(){
        return InternalLibrary.giveMeAString();
    }
}

The build.gradle dependencies of MyLibrary its like this:

dependencies {
    api project(':InternalLibrary')
}

Now in your code you want to use MyLibrary so you should have a build.gradle with this dependency

dependencies {
    api project(':MyLibrary')
}

In your application code, with the api keyword (or using the old compile) you can access both MyLibrary and InternalLibrary.

//so you can access the library (as it should)
MyLibrary myLib = new MyLibrary();
System.out.println(myLib.myString());

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

In this way you are potentially "leaking" the internal implementation of something that you shouldn't use because it's not directly imported by you.

To prevent this, Gradle has created the new implementation keyword, so now if you switch api to implementation in your MyLibrary

dependencies {
    implementation project(':InternalLibrary')
}

And in your app build.gradle

dependencies {
    implementation project(':MyLibrary')
}

you won't be able to call InternalLibrary.giveMeAString() in your app code anymore. While if MyLibrary uses the api keyword to import InternalLibrary, in your app you will be able to call InternalLibrary.giveMeAString() without problems, independently if you use api or implementation to add MyLibrary to your app.

Using thi sort of boxing strategy the Android gradle plugin knows that if you edit something in InternalLibrary it will trigger the recompilation of MyLibrary only. It will not trigger the recompilation of your entire app because you don't have access to InternalLibrary. This mechanism when you have a lot of nested dependencies 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.

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

Original Thread

By anonymous    2017-09-20

It is one of the breaking changes coming with gradle:3.0 that Google announced at IO17 gradle:3.0

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

From the gradle docs :

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.


tl;dr:just replace compile with implementation, testCompile with testImplementation and androidtestcompile with androidTestImplementation

Original Thread

By anonymous    2017-09-20

Replace implementation with compile

compile was recently deprecated and replaced by implementation or api

Original Thread

By anonymous    2017-11-13

Try this :

  1. From the Build menu -> press the Clean Project button.
  2. When task completed, press the Rebuild Project button from the Build menu.

compile is now deprecated so it's better to use implementation or api

Original Thread

By anonymous    2017-12-11

After watching The last Google I/O session about Speeding up your Android Gradle builds and applying those tips and experiment with them for a while my build time dramatically decreased, you can also check the documentation here.

Original Thread

Popular Videos 904

Submit Your Video

If you have some great dev videos to share, please fill out this form.