loading...
Cover image for Configuring Gradle with "gradle.properties"

Configuring Gradle with "gradle.properties"

jmfayard profile image Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄ ・Updated on ・3 min read

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

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")
    }
}

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
# ....

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

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

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

kapt.use.worker.api=true
kapt.incremental.apt=true

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

Android settings

studio.projectview=true

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

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

android.databinding.incremental=true

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

android.enableSeparateAnnotationProcessing=true

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

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

Discussion

pic
Editor guide
Collapse
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 gradle.properties 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."

Collapse
jmfayard profile image
Jean-Michel Fayard πŸ‡«πŸ‡·πŸ‡©πŸ‡ͺπŸ‡¬πŸ‡§πŸ‡ͺπŸ‡ΈπŸ‡¨πŸ‡΄ Author

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 github.com/jmfayard/buildSrcVersio...

Collapse
chhh profile image
Dmitry Avtonomov

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