Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference property in Gradle Properties

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

like image 590
George Avatar asked May 29 '19 14:05

George


People also ask

What is the use of Buildscript in Gradle?

Gradle builds a script file for handling two things; one is projects and other is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a web application or it might represent a ZIP that is assembled from the JARs produced by other projects.

What is GRADLE_USER_HOME?

GRADLE_USER_HOME. Specifies the Gradle user home directory (which defaults to $USER_HOME/. gradle if not set). JAVA_HOME. Specifies the JDK installation directory to use for the client VM.


Video Answer


3 Answers

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    
    ...
}
like image 89
Zsolt Boldizsár Avatar answered Oct 21 '22 09:10

Zsolt Boldizsár


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.

like image 20
Jeel Vankhede Avatar answered Oct 21 '22 08:10

Jeel Vankhede


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
}
like image 23
George Avatar answered Oct 21 '22 09:10

George