在 Gradle 属性中引用 属性

Reference property in Gradle Properties

我的gradle.properties.kts文件有以下内容:

build_version=develop
build_version_code=2
include_vas=false

在我的build.gradle.kts中看起来如下:

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

我正在将当前的 gradle 脚本转换为 kotlin DSL。我目前面临的挑战是在 defaultConfig:

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

我指的是在gradle.properties中定义的versionCode。下面的代码是在转换之前是如何完成的。

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

我如何在 Kotlin 中执行此操作

没有干净的方法来获取它,但是您可以使用 Project interface and use it's property() 方法从中获取您的 gradle.properties.kts 变量,例如下面的代码:

project.property('your_variable_name_here')

对于你的例子:

versionCode project.property('build_version_code')

这是从 属性 文件设置所有变量的方法。

我最终按照@Jeel Vankhede 的建议做了以下事情:

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
}

自从 Gradle Kotlin DSL(包含在 Gradle v4.7 中)的 v0.16.3 以来,可以使用 属性 委托来表示 [=12= 中声明的值] 如下:

val yourVariable: TypeOfYourVariable by project

您的示例如下所示:

val build_version_code: String by project

defaultConfig {
    ...
    versionCode = build_version_code    
    ...
}

您可以通过以下方式访问项目变量,例如端口:

val port: String by project

如果未设置端口,上述操作将失败。如果不确定,请使用

val port: String? by project

那么你可以这样做:

 if (port != null) {...}