DEV Community

Cover image for Configuring Gradle with ""
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Posted on • Updated on

Configuring Gradle with ""

Using as your build tool? Read on.

I found the file a nice part of Gradle.

What is hard is to find an overview of this information: you can create your own settings, Gradle has its own, Kotlin its own, Android its own, ...

So I thought I would provide an overview of all of that.

You should not add a setting before you have read the docs to understand what it does. Which is why I added every time the link to the documentation.

The file

The friendly Gradle docs inform you that

In Gradle, properties can be defined in the build script, in a file or as parameters on the command line.
It’s common to declare properties on the command line for ad-hoc scenarios. For example you may want to pass in a specific property value to control runtime behavior just for this one invocation of the build. Properties in a build script can easily become a maintenance headache and convolute the build script logic. The helps with keeping properties separate from the build script and should be explored as viable option. It’s a good location for placing properties that control the build environment.

Putting there your own settings

First you can use it to put your own settings. For example, if you have an Android project, you can put there


# Common Android settings
Enter fullscreen mode Exit fullscreen mode

then you can reuse the same app/build.gradle snippet all the time

android {
    compileSdkVersion rootProject.findProperty("android.compileSdkVersion") as Integer

    defaultConfig {
        targetSdkVersion findProperty("android.targetSdkVersion") as Integer
        minSdkVersion findProperty("android.minSdkVersion") as Integer
        applicationId findProperty("android.applicationId")
        versionCode findProperty("android.minSdkVersion") as Integer
        versionName findProperty("android.versionName")
Enter fullscreen mode Exit fullscreen mode

Putting your dependencies versions

This is what my Gradle plugin automatically does for you:


# Dependencies and Plugin versions with their available updates
# Generated by $ ./gradlew refreshVersions
# You can edit the rest of the file, it will be kept intact
# See
#                # available=2.1.0
#                # available=1.3.50
#     # available=1.1.0
#    # available=1.1.0
# ....
Enter fullscreen mode Exit fullscreen mode

Read the docs at gradle :refreshVersions" generates with versions and available updates

Gradle settings

The top two are especially great to improve your build performance.

org.gradle.daemon.idletimeout= 10800000
org.gradle.console=auto to JDK home)
#org.gradle.workers.max=(max # of worker processes)
# org.gradle.priority=(low,normal)
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Enter fullscreen mode Exit fullscreen mode

Read the docs at

Kotlin settings
Enter fullscreen mode Exit fullscreen mode

Read the docs at

Enter fullscreen mode Exit fullscreen mode

Read the docs at

Android settings

Enter fullscreen mode Exit fullscreen mode

If you think like me that the Android view is worse in every respect than the Project view and needs to go

Enter fullscreen mode Exit fullscreen mode

Read the docs at

Enter fullscreen mode Exit fullscreen mode

Read the docs at

Enter fullscreen mode Exit fullscreen mode

Read the docs at

Other Android flags

android.enableR8.libraries = true
android.enableR8 = true
Enter fullscreen mode Exit fullscreen mode

Check the code-source at

Top comments (3)

chhh profile image
Dmitry Avtonomov

Suppose I have a flat folder structure with two support libraries

|-- app-1                  
|   |-- build.gradle.kts    [dependency(project(':lib-2')), dependency('guava:28.0')]
|   `-- settings.gradle.kts [includeFlat('lib-2'))]
|-- lib-1                  
|   |-- build.gradle.kts    [dependency('guava:26.0')]
|   `-- settings.gradle.kts
`-- lib-2                  
    |-- build.gradle.kts    [dependency('guava:27.0'); dependency(project(':lib-1'))]
    `-- settings.gradle.kts (includeFlat('lib-1'))

So 'lib-1' is some old stuff. 'lib-2' is newer and uses 'lib-1', wants to supersede guava 26.0 with 27.0. And the 'app-1' wants to use 'lib-2', but also use some features from guava 28.0.
How would I go about converting to so that the right versions are used in all projects. Every time I set up a composite build I start having problems with gradle complaining that "some lib/plugin is already added and should not have its version listed in the build."

jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

That works, my own project has exactly the same structure.

You have to update Gradle because there is no good way to setup plugin versions before 5.6

$ ./gradlew wrapper --gradle-version 5.6.2

There is a boilerplate resolutionStrategy that you have to copy/paste in settings.gradle.kts

Read the docs here

chhh profile image
Dmitry Avtonomov

Thanks! Didn't notice first that you used issue tracker as a wiki :)