Configuring Gradle with “gradle.properties”

The joy of Kotlin (20 Part Series)

1 How Kotlin makes editing your Gradle build less frustrating
2 Better dependency management in Android Studio 3.5 with Gradle buildSrcVersions
16 more parts…
3 with(ConfigObject) { “Language $KOTLIN”.isSparkingJoy() }
4 Use the Gradle build-scan!
5 Configuring Gradle with “gradle.properties”
6 Kotlin is Not (Only) Android
7 How to learn Kotlin: browser vs IDE, books vs tutorials, for newbies and Java devs
8 How to become Effective with Kotlin? Answers from Marcin Moskala
9 From marketing to backend developer in one year – the story of Adele Carpenter
10 Contribute to the Kotlin Libraries Playground for #hacktoberfest
11 Comment apprendre Kotlin? IDE et navigateur, livres et tutoriels, débutants et dev Java
12 From Java to Kotlin in 20 minutes ️
13 De Java à Kotlin en 20 minutes
14 How to build a GraphQL Gateway with Spring Boot and Kotlin
15 Practice what’s new in Java
16 How to Write a Command-Line Tool with Kotlin Multiplatform
17 GitHub Actions: a New Hope in YAML Programming Wasteland
18 Is There an Equivalent of Spring Boot for Kotlin?
19 Weeks of Debugging Your Build can Save Hours of Learning Gradle
20 Typesafe Github Workflows explained to a 5 years old

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

The joy of Kotlin (20 Part Series)

1 How Kotlin makes editing your Gradle build less frustrating
2 Better dependency management in Android Studio 3.5 with Gradle buildSrcVersions
16 more parts…
3 with(ConfigObject) { “Language $KOTLIN”.isSparkingJoy() }
4 Use the Gradle build-scan!
5 Configuring Gradle with “gradle.properties”
6 Kotlin is Not (Only) Android
7 How to learn Kotlin: browser vs IDE, books vs tutorials, for newbies and Java devs
8 How to become Effective with Kotlin? Answers from Marcin Moskala
9 From marketing to backend developer in one year – the story of Adele Carpenter
10 Contribute to the Kotlin Libraries Playground for #hacktoberfest
11 Comment apprendre Kotlin? IDE et navigateur, livres et tutoriels, débutants et dev Java
12 From Java to Kotlin in 20 minutes ️
13 De Java à Kotlin en 20 minutes
14 How to build a GraphQL Gateway with Spring Boot and Kotlin
15 Practice what’s new in Java
16 How to Write a Command-Line Tool with Kotlin Multiplatform
17 GitHub Actions: a New Hope in YAML Programming Wasteland
18 Is There an Equivalent of Spring Boot for Kotlin?
19 Weeks of Debugging Your Build can Save Hours of Learning Gradle
20 Typesafe Github Workflows explained to a 5 years old

原文链接:Configuring Gradle with “gradle.properties”

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容