Reputation: 2997
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
Upvotes: 21
Views: 23581
Reputation: 299
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) {...}
Upvotes: 3
Reputation: 2582
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
...
}
Upvotes: 29
Reputation: 2997
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
}
Upvotes: 4
Reputation: 12138
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.
Upvotes: 8