Configuring Gradle with "gradle.properties"

jmfayard

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

Posted on September 26, 2019

Configuring Gradle with "gradle.properties"

Using https://gradle.org as your build tool? Read on.

I found the gradle.properties 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 gradle.properties

The friendly Gradle docs inform you that

In Gradle, properties can be defined in the build script, in a gradle.properties 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 gradle.properties 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.
https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#declare_properties_in_gradle_properties_file

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

## gradle.properties

# Common Android settings
android.compileSdkVersion=28
android.applicationId=com.example
android.targetSdkVersion=28
android.minSdkVersion=21
android.versionCode=2
android.versionName=1.2
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:

## gradle.properties

# 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 https://github.com/jmfayard/buildSrcVersions/issues/77
plugin.com.github.ben-manes.versions=0.25.0
plugin.de.fayard.buildSrcVersions=0.6.1
version.com.android.tools.build..gradle=3.5.0
version.play-services-location=17.0.0
version.bottom-navigation-bar=2.1.0
version.lifecycle-extensions=2.0.0
#                # available=2.1.0
version.org.jetbrains.kotlin=1.3.31
#                # available=1.3.50
version.appcompat=1.1.0-rc01
#     # available=1.1.0
version.cardview=1.0.0
version.core-ktx=1.0.2
#    # available=1.1.0
# ....
Enter fullscreen mode Exit fullscreen mode

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

Gradle settings

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

org.gradle.caching=true
org.gradle.parallel=true
org.gradle.caching.debug=false
org.gradle.configureondemand=false
org.gradle.daemon.idletimeout= 10800000
org.gradle.console=auto
#org.gradle.java.home=(path to JDK home)
#org.gradle.warning.mode=(all,none,summary)
#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
// https://docs.gradle.org/current/userguide/build_environment.html#sec:configuring_jvm_memory
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties

Kotlin settings

kotlin.code.style=official
kotlin.caching.enabled=true
kotlin.incremental=true
kotlin.incremental.js=true
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://kotlinlang.org/docs/reference/using-gradle.html

kapt.use.worker.api=true
kapt.incremental.apt=true
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://kotlinlang.org/docs/reference/kapt.html

Android settings

studio.projectview=true
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

android.enableJetifier=true
android.useAndroidX=true
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://developer.android.com/jetpack/androidx

android.databinding.incremental=true
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://developer.android.com/topic/libraries/data-binding/start

android.enableSeparateAnnotationProcessing=true
Enter fullscreen mode Exit fullscreen mode

Read the docs at https://developer.android.com/studio/build/optimize-your-build

Other Android flags

android.enableR8.fullMode=true
android.enableR8.libraries = true
android.enableR8 = true
android.debug.obsoleteApi=true
android.enableBuildCache=true
android.enableGradleWorkers=true
android.useMinimalKeepRules=true
Enter fullscreen mode Exit fullscreen mode

Check the code-source at
https://android.googlesource.com/platform/tools/base/+/mirror-goog-studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/options/BooleanOption.kt

πŸ’– πŸ’ͺ πŸ™… 🚩

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related