Geo*_*rge 7 android gradle android-gradle-plugin gradle-kotlin-dsl
我的gradle.properties.kts文件有以下内容:
build_version=develop
build_version_code=2
include_vas=false
Run Code Online (Sandbox Code Playgroud)
在我的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)
}
Run Code Online (Sandbox Code Playgroud)
我正在将我当前的 gradle 脚本转换为 kotlin DSL。我目前面临的挑战是在 defaultConfig 中:
android {
compileSdkVersion(Configs.compileVersion)
defaultConfig {
applicationId = Configs.applicationId
minSdkVersion(Configs.minSdkVersion)
targetSdkVersion(Configs.targetSdkVersion)
testInstrumentationRunner = Configs.testInstrumentationRunner
versionCode =
}
Run Code Online (Sandbox Code Playgroud)
我指的是 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"
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Kotlin 中做到这一点
Zso*_*sár 16
由于Gradle Kotlin DSL 的v0.16.3(包含在 Gradle v4.7 中),可以使用属性委托来声明gradle.properties如下所示的值:
val yourVariable: TypeOfYourVariable by project
Run Code Online (Sandbox Code Playgroud)
您的示例如下所示:
val build_version_code: String by project
defaultConfig {
...
versionCode = build_version_code
...
}
Run Code Online (Sandbox Code Playgroud)
没有干净的方法来获取它,但是您可以使用Project接口并使用它的property()方法从中获取gradle.properties.kts变量,如下代码所示:
project.property('your_variable_name_here')
Run Code Online (Sandbox Code Playgroud)
对于你的例子:
versionCode project.property('build_version_code')
Run Code Online (Sandbox Code Playgroud)
这是您可以从属性文件中设置所有变量的方法。
我最终按照@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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8215 次 |
| 最近记录: |