我正在尝试将 Android 应用程序的 build.gradle 文件转换为 Kotlin DSL。这个文件有一个这样的功能:
def getLastCommitHash() {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
Run Code Online (Sandbox Code Playgroud)
我将其转换为:
fun getLastCommitHash() {
val stdout = ByteArrayOutputStream()
exec {
commandLine("git", "rev-parse", "--short", "HEAD")
standardOutput = stdout
}
return stdout.toString().trim()
}
Run Code Online (Sandbox Code Playgroud)
我收到Unresolved reference: ByteArrayOutputStream错误并应用导入,将其更改为java.io.ByteArrayOutputStream()显示Unresolved reference: io错误。
我做错了什么吗?提前致谢。
我有以下项目结构
root-project
? build.gradle.kts
?
????multiplatform-project
? ? build.gradle.kts
? ?
? ????src
? ? kotlin
| | js
?
????simple-kotlin-project
? build.gradle.kts
Run Code Online (Sandbox Code Playgroud)
所以总共有 3 个项目(3 个build.gradle.kts文件)。
这是根项目build.gradle.kts
plugins {
kotlin("jvm") version kotlinVersion apply false
kotlin("kapt") version kotlinVersion apply false
kotlin("multiplatform") version kotlinVersion apply false
}
subprojects {
apply<JavaPlugin>()
if (name.contains("multiplatform-project")) {
apply(plugin = "org.jetbrains.kotlin.multiplatform")
} else {
apply(plugin = "kotlin")
}
apply(plugin = "kotlin-kapt")
dependencies {
implementation(kotlin("stdlib"))
implementation(kotlin("reflect"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M2")
implementation("org.slf4j:slf4j-api:1.7.25")
implementation("ch.qos.logback:logback-core:1.2.3")
implementation("ch.qos.logback:logback-classic:1.2.3")
testImplementation("junit:junit:$jUnitVersion")
} …Run Code Online (Sandbox Code Playgroud)我正在为 JVM 设置一个基于 Kotlin 的多模块 Gradle 项目。由于根项目不包含任何代码,因此 Kotlin 插件只能应用于子项目。
build.gradle.kts(根项目)
plugins {
kotlin("jvm") version "1.6.20" apply false
}
subprojects {
apply(plugin = "kotlin")
group = "com.example"
repositories {
mavenCentral()
}
dependencies {}
kotlin {
jvmToolchain {
check(this is JavaToolchainSpec)
languageVersion.set(JavaLanguageVersion.of(11))
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试设置工具链会导致构建在kotlin {...}扩展处失败:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun DependencyHandler.kotlin(module: String, version: String? = ...): Any defined in org.gradle.kotlin.dsl
public fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec defined …Run Code Online (Sandbox Code Playgroud) gradle kotlin gradle-kotlin-dsl gradle-multi-project-build kotlin-gradle-plugin
我试图像在groovy中那样组织我的构建文件,通过在单独的文件中重用值来重用.但我无法理解在kotlin DSL中做同样事情的语法.
这是我在root build.gradle.kts中使用的内容:
applyFrom("config.gradle.kts")
buildscript {
repositories {
google()
jcenter()
}
dependencies {
val test = project.extra["minSdkVer"]
classpath("com.android.tools.build:gradle:3.0.0-alpha4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
Run Code Online (Sandbox Code Playgroud)
以下是正在引用的config.gradle.kts中的内容:
mapOf(
Pair("minSdkVer", 22),
Pair("targetSdkVer", 25),
Pair("compiledSdkVer", 25),
Pair("buildToolsVer", "26-rc4")
).entries.forEach {
project.extra.set(it.key, it.value)
}
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:
无法在额外的属性扩展上获取属性'minSdkVer',因为它不存在
已经有无数的文章介绍了如何从使用 groovy 脚本迁移到使用 Kotlin DSL 进行 Gradle 依赖管理是管理构建脚本的理想方式以及其他提到的优势。
但是,我发现的限制是缺乏这种 Gradle 管理方式或流程,无法像以前使用 groovy 脚本那样突出显示当前依赖项的新版本可用时的情况。我发现的当前解决方案包括使用插件或实用程序扫描您的 buildSrc 文件夹并将更新作为对当前版本库的注释提供。其中一些包括以下内容:
除了我提到的几个插件之外,还有其他有效的方法来检查依赖项更新吗?
android android-studio android-gradle-plugin gradle-kotlin-dsl
我们有使用 gradle kotlin dsl 编写的构建逻辑的多模块 android 应用程序。我们使用 buildSrc 来提取常见逻辑,例如依赖项版本。我们有类似的东西:
buildSrc/src/main/kotlin/Dependencies.kt:
object Versions {
const val fooVersion = "1.2.3"
const val barVersion = "4.5.6"
}
object Libraries {
val foo = "com.example.foo:foo:$fooVersion"
val bar = "com.example.bar:bar:$barVersion"
}
object Modules {
const val app = ":app"
const val base = ":base"
const val baz = ":baz"
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以在模块的dependencies块中使用这些来避免硬编码/重复值:
app/build.gradle.kts:
dependencies {
implementation(Libs.foo)
implementation(Libs.bar)
implementation(project(Modules.base))
implementation(project(Modules.baz))
}
Run Code Online (Sandbox Code Playgroud)
我们也在 settings.gradle.kts 中使用它:
settings.gradle.kts:
include(
Modules.app,
Modules.base,
Modules.baz
)
Run Code Online (Sandbox Code Playgroud)
这适用于 gradle …
我有一个使用 Gradle 和 Kotlin DSL 的 Android 应用程序。我正在添加 Firebase 性能监控,但我希望仅针对特定构建类型启用它。
我一直在遵循Firebase - 禁用 Firebase 性能监控中提供的说明。不幸的是,提供的代码片段在 Groovy 中。
我尝试通过执行以下操作在我的应用级 Gradle 脚本中获取对 Firebase 性能监控扩展的引用:
plugins {
...
id("com.google.firebase.firebase-perf")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}
buildTypes {
getByName(BuildTypes.DEBUG) {
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> {
setInstrumentationEnabled(false)
}
}
...
}
...
dependencies {
val firebaseVersion = "17.2.1"
implementation("com.google.firebase:firebase-core:$firebaseVersion")
implementation("com.google.firebase:firebase-analytics:$firebaseVersion")
implementation("com.google.firebase:firebase-perf:19.0.5")
}
Run Code Online (Sandbox Code Playgroud)
Android Studio 在这方面没有发现任何问题并自动完成FirebasePerfExtension。不幸的是,在运行 Gradle 同步时,我得到以下信息:
Extension of type 'FirebasePerfExtension' does not exist.
Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, CrashlyticsExtension, …Run Code Online (Sandbox Code Playgroud) 我使用 Gradle Kotlin DSL,在我的项目中我有单独的build-dependenciesgradle 模块,settings.gradle.kts如下所示:
pluginManagement {
repositories {
google()
jcenter()
gradlePluginPortal()
}
}
includeBuild("build-dependencies")
Run Code Online (Sandbox Code Playgroud)
该模块包含空插件实现和Deps对象。其build.gradle.kts如下:
repositories {
google()
jcenter()
gradlePluginPortal()
}
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}
gradlePlugin {
plugins.register("com.example.dependencies") {
id = "com.example.dependencies"
implementationClass = "com.example.dependencies.DependenciesPlugin"
}
}
Run Code Online (Sandbox Code Playgroud)
部门对象:
object Deps {
const val buildTools = "com.android.tools.build:gradle:4.1.1"
// more dependencies
}
Run Code Online (Sandbox Code Playgroud)
我想Deps通过在文件中应用插件来在所有模块中使用这个对象build.gradle.kts。
plugins {
id("com.example.dependencies")
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,我可以导入和使用Deps对象。
但是当我想在根项目中使用它时build.gradle.kts,更准确地说在构建脚本中使用它时,会出现一个问题,如下所示:
buildscript {
repositories {
google() …Run Code Online (Sandbox Code Playgroud) 默认情况下,当我从 Android Studio 创建撰写应用程序时,它会在我的根项目中提供ext如下信息build.gradle。
buildscript {
ext {
compose_version = '1.0.0'
}
repositories {
google()
mavenCentral()
}
}
Run Code Online (Sandbox Code Playgroud)
当我将其更改为 时build.gradle.kts,它会抱怨
Unresolved reference: ext
Unresolved reference: compose_version
Too many characters in a character literal ''1.0.0''
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我正在使用这篇博文来配置Spring Boot项目的集成测试,但我非常坚持声明源集.我也在StackOverflow上发现了这篇文章,但我想我已经进一步了.
我的项目结构是
project
|_ src
|_ main
| |_ kotlin
| |_ resources
|_ testIntegration
| |_ kotlin
| |_ resources
|_ test
| |_ kotlin
| |_ resources
|_ build.gradle.kts
|_ ... other files
Run Code Online (Sandbox Code Playgroud)
和build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
idea
kotlin("jvm")
id("org.springframework.boot") version "2.0.5.RELEASE"
id("org.jetbrains.kotlin.plugin.spring") version "1.2.71"
}
fun DependencyHandlerScope.springBoot(module: String) = this.compile("org.springframework.boot:spring-boot-$module:2.0.5.RELEASE")
fun DependencyHandlerScope.springBootStarter(module: String) = this.springBoot("starter-$module")
dependencies {
springBoot("devtools")
springBootStarter("batch")
springBootStarter("... spring boot dependencies")
compile("... more dependencies")
testCompile("... more …Run Code Online (Sandbox Code Playgroud)