18

My gradle.properties.kts files has the following contents:

build_version=develop
build_version_code=2
include_vas=false

In my build.gradle.kts looks as follows:

plugins {
    id(Plugins.androidApplication)
    kotlin(Plugins.kotlinAndroid)
    kotlin(Plugins.kotlinExtensions)
    kotlin(Plugins.kapt)
}


android {
    compileSdkVersion(Configs.compileVersion)
    defaultConfig {
        applicationId = Configs.applicationId
        minSdkVersion(Configs.minSdkVersion)
        targetSdkVersion(Configs.targetSdkVersion)
        testInstrumentationRunner = Configs.testInstrumentationRunner
        versionCode =
    }


  buildTypes{

  }
    repositories {
        flatDir {
            dirs("libs")
        }
    }

    dependencies {
        implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.arr"))))
        implementation(Libs.stdLib)
        implementation(Libs.sunmiui)
        implementation(Libs.slf4j)
        implementation(Libs.appCompact)
        implementation(Libs.otpView)
        implementation(Libs.vectordrawableAnimated)
        implementation(Libs.materialComponents)
        implementation(Libs.recyclerView)
        implementation(Libs.constraintLayout)
        implementation(Libs.junit)
        implementation(Libs.testRunner)
        implementation(Libs.expressoCore)
        implementation(Libs.lifecyleExtensions)
        implementation(Libs.lifecyleCompiler)
        implementation(Libs.roomRuntime)
        implementation(Libs.databindingCompiler)
        implementation(Libs.rxjava)
        implementation(Libs.rxjavaAndroid)
        implementation(Libs.glide)
        implementation(Libs.glideCompiler)
        implementation(Libs.gson)
        implementation(Libs.joda)
        implementation(Libs.countrycodePicker)
        implementation(Libs.timber)
        implementation(Libs.daggerandroidSupport)
        implementation(Libs.daggerandroidProcessor)
    }

I am in the process of converting my current gradle scripts to kotlin DSL. The current challenge I am facing is that in the defaultConfig:

android {
    compileSdkVersion(Configs.compileVersion)
    defaultConfig {
        applicationId = Configs.applicationId
        minSdkVersion(Configs.minSdkVersion)
        targetSdkVersion(Configs.targetSdkVersion)
        testInstrumentationRunner = Configs.testInstrumentationRunner
        versionCode =
    }

I refer to the versionCode that is defined in the gradle.properties. The code below was how it was done before the conversion.

defaultConfig
        {

            multiDexEnabled true
            applicationId "jfjfjrjrjr.comn.jejeu"
            minSdkVersion 24
            targetSdkVersion 28
            versionCode build_version_code as Integer
            versionName build_version
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }

How can I do this in Kotlin

George
  • 2,865
  • 6
  • 35
  • 59

4 Answers4

27

Since v0.16.3 of the Gradle Kotlin DSL (included in Gradle v4.7) one could use property delegates for the values declared in gradle.properties like follows:

val yourVariable: TypeOfYourVariable by project

Your example would look like:

val build_version_code: String by project

defaultConfig {
    ...
    versionCode = build_version_code    
    ...
}
Zsolt Boldizsar
  • 2,447
  • 2
  • 27
  • 37
  • 3
    ugh, this is pretty lame, things were easier when you could just do `findProperty("build_version_code")` – lasec0203 Jun 20 '21 at 04:47
  • property delegates example: https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties – lasec0203 Jun 20 '21 at 04:48
8

There's no clean way about obtaining it, but you can use Project interface and use it's property() method to obtain your gradle.properties.kts variables from it like below code:

project.property('your_variable_name_here')

For your instance:

versionCode project.property('build_version_code')

This is how you can set all your variables from your property file.

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • I get the following error message when I use the method above:java.lang.String cannot be cast to java.lang.Integer Possible causes for this unexpected error include: Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network) – George May 29 '19 at 16:04
  • I just forgot to mention that it will return Object type though you'll need to cast it.. use like this if variable is Integer : `versionCode project.property('build_version_code') as Integer` – Jeel Vankhede May 29 '19 at 16:23
  • I had cast it and still I get the same error: versionCode = project.property("build_version_code") as Int? versionName = project.property("build_version") as String? – George May 29 '19 at 16:32
  • Is that still a casting exception or any new error? – Jeel Vankhede May 29 '19 at 16:34
  • Still produces the same error as previously stated: java.lang.String cannot be cast to java.lang.Integer Possible causes for this unexpected error include: Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network) – George May 29 '19 at 16:36
4

What I ended up doing as suggested by @Jeel Vankhede is the following:

defaultConfig {
    multiDexEnabled = true
    applicationId = Configs.applicationId
    minSdkVersion(Configs.minSdkVersion)
    targetSdkVersion(Configs.targetSdkVersion)
    var value = Integer.parseInt(project.property("build_version_code") as String?)
    versionCode = value
    versionName = project.property("build_version") as String?
    testInstrumentationRunner = Configs.testInstrumentationRunner
}
George
  • 2,865
  • 6
  • 35
  • 59
2

You can access a Project variable, e.g port, with:

val port: String by project

The above will fail if port is not set. If unsure, use

val port: String? by project

Then you could do:

 if (port != null) {...}
ekene
  • 111
  • 8