标签: gradle-kotlin-dsl

如何使用gradle脚本Kotlin构建文件构建可运行的ShadowJar?

最简单的Kotlin hello world for gradle script Kotlin:

thufir@dur:~/github/gradleScriptKotlin$ 
thufir@dur:~/github/gradleScriptKotlin$ gradle clean shadowJar;java -jar build/libs/gradleScriptKotlin.jar 

> Task :compileKotlin 
Using Kotlin incremental compilation

> Task :shadowJar 
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
 - No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle …
Run Code Online (Sandbox Code Playgroud)

build gradle kotlin build.gradle gradle-kotlin-dsl

9
推荐指数
1
解决办法
2471
查看次数

使Gradle任务可以访问环境变量

我需要在shell环境中运行Gradle任务,该环境必须在启动任务之前创建.使用commandLineexecutable不合适,因为我需要在与shell脚本相同的进程中运行任务.最初,我直接在里面调用了脚本gradlew,但后来我决定从中获取源代码build.gradle.kts并通过gradlew以下方式调用后续任务:

val setupRosEnv by tasks.creating(Exec::class) {
    executable = "bash"

    args("-c", "source $rosPath/setup.sh && source gradlew myTask")
}
Run Code Online (Sandbox Code Playgroud)

我可以通过./gradlew setupRosEnvCLI 运行来构建所有内容.除了运行脚本然后运行gradlew,有没有办法使用Gradle API实现这一点?当前的解决方案看起来有点笨拙,而且依赖于其他任务很笨拙setupRosEnv,因为这将导致无限循环或必须明确处理以防止任务多次运行.

由于shell脚本本身是由ROS生成的,因此无法将其转换为Gradle或轻松解析.

bash gradle kotlin ros gradle-kotlin-dsl

9
推荐指数
1
解决办法
987
查看次数

无法使用 gradle 将 jar 发布到 Gitlab 包注册表

我正在尝试在 gitlab 包注册表上发布一些 jar 人工制品,但我从服务器收到此错误:

Received status code 415 from server: Unsupported Media Type
Run Code Online (Sandbox Code Playgroud)

这是我的 build.gradle.kts 的发布部分:

publishing {
    publications {
        create<MavenPublication>("maven"){
            artifact(tasks["bootJar"])
        }
    }
    repositories {
        maven {
            url =  uri("https://gitlab.com/api/v4/groups/my-group/-/packages/maven")
            name = "Gitlab"
            credentials(HttpHeaderCredentials::class) {
                name = "Token"
                value = System.getenv("CI_JOB_TOKEN")
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的 gitlab-ci 中,我添加了一个发布人工制品的任务:

deploy:
  stage: deploy
  script: gradle publish
  only:
    - master
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

gradle gitlab gitlab-ci gradle-kotlin-dsl

9
推荐指数
1
解决办法
2222
查看次数

在 KMM 中使用 firebase-bom 依赖项时,为什么会出现“未解析的引用:平台”

当我尝试在 Kotlin Multiplatform Mobile (KMM) 项目的共享模块中使用以下块添加 Firebase-bom 依赖项时,该词platform出现在红色错误文本中,并且 Gradle 构建失败并显示“未解析的引用:平台”。我该如何解决这个问题以便正确构建?

        val androidMain by getting {
            dependencies {
                implementation(platform("com.google.firebase:firebase-bom:28.0.1"))
                implementation("com.google.firebase:firebase-analytics-ktx")
            }
        }
Run Code Online (Sandbox Code Playgroud)

kotlin firebase build.gradle gradle-kotlin-dsl kmm

9
推荐指数
1
解决办法
1613
查看次数

如何配置 KMM 项目以支持不同的构建类型

当我切换构建变体时,出现编译错误,但问题仅适用于非默认构建变体(debug & release)。因此,如果我定义customBuild { }然后选择那个,它就会失败并显示以下错误日志:

    The consumer was configured to find a runtime of a component, 
    preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' 
    with value 'customBuild', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'.
    However we cannot choose between the following variants of project :shared-module:
- iosArm64RuntimeOnly
          - iosX64RuntimeOnly
        All of them match the consumer attributes:
          - Variant 'iosArm64RuntimeOnly' capability ExampleApp:shared-module:1.0:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'customBuild')
                  - Doesn't say anything about …
Run Code Online (Sandbox Code Playgroud)

android gradle-kotlin-dsl kotlin-multiplatform kotlin-multiplatform-mobile

9
推荐指数
0
解决办法
1509
查看次数

未解决的参考:库 - buildSrc

我正在尝试切换到使用 Kotlin DSL 从对象 Kotlin 文件而不是 gradle 文件获取依赖项版本。当我进行 gradle 同步时,它无法解析引用,但我可以从 build.gradle.kts 单击文件(库)。我没有对 build.gradle 或 settings.gradle 进行任何其他更改。这个问题并不是杰克逊特有的,我在多个项目中尝试过这个问题,并在发布问题之前阅读文档并搜索答案。

我得到一个 build.gradle.kts:15:20: Unresolved reference: Libraries

这是我的项目的结构

build.gradle
settings.gradle
+buildSrc
    build.gradle.kts
    +src
        +main
            +kotlin
                Libraries.kt
Run Code Online (Sandbox Code Playgroud)

这是我的 build.gradle.kts 文件

import org.gradle.kotlin.dsl.`kotlin-dsl`

plugins {
    `kotlin-dsl`
    java
    `java-gradle-plugin`
}


repositories {
    mavenCentral()
}

dependencies {
    implementation(Libraries.Jackson.core)
//    implementation(Libraries.anotherJackson)
}
Run Code Online (Sandbox Code Playgroud)

这是 kotlin Libraries.kt 文件

object Libraries {
    val Jackson = JacksonLibraries
    const val anotherJackson = "com.fasterxml.jackson.core:jackson-core:2.11.1"

}

object JacksonLibraries {
    const val core = "com.fasterxml.jackson.core:jackson-core:${Versions.Jackson.core}"
}

object Versions { …
Run Code Online (Sandbox Code Playgroud)

android dependency-management kotlin build.gradle gradle-kotlin-dsl

9
推荐指数
1
解决办法
9999
查看次数

KMM 项目:预期的类在 JVM 模块中没有实际声明

expect我有一个运行完美的 KMM 项目,除了 Android Studio在我的项目中的每个函数/值上给出错误,抱怨actualJVM 不存在该版本。

在此输入图像描述

旁边的黄色菱形中的 A 显示了 iOS 和 Android 的实际版本,并且该项目构建/运行得很好。

在此输入图像描述

我已经仔细检查了包名称,expect无论包如何,每个包都会发生这种情况。

我检查了我的 gradle 构建文件,将它们与新的 KMM 示例项目进行比较时找不到任何奇怪的东西。

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("com.rickclephas.kmp.nativecoroutines") version "0.12.2-new-mm"
}

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    //iosSimulatorArm64() sure all ios dependencies support this target

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "15.0"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
        }
    } …
Run Code Online (Sandbox Code Playgroud)

intellij-idea android-studio gradle-kotlin-dsl kotlin-multiplatform kmm

9
推荐指数
1
解决办法
2238
查看次数

将“androidResources”无压缩选项添加到 Android Library Gradle 时出错

我正在尝试将以下选项添加到Android 库的gradle.kts文件中

android{
        androidResources {
        noCompress += "tflite"
    }
}
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,每当我尝试同步项目时,我都会收到此错误

  • 出了什么问题:类 com.android.build.gradle.internal.dsl.AaptOptions$AgpDecolated_Decolated 无法转换为类 com.android.build.api.dsl.LibraryAndroidResources (com.android.build.gradle.internal.dsl.AaptOptions $AgpDecolated_Decolated 和 com.android.build.api.dsl.LibraryAndroidResources 位于加载器 org.gradle.internal.classloader.VisitableURLClassLoader @481a697b 的未命名模块中)

有人知道我该如何解决这个问题吗?互联网在这里似乎没有用。

像这样使用已弃用的旧版本不会产生任何错误

aaptOptions{
    noCompress += "tflite"
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Android Gradle 插件 8.1.0-beta05 和 gradle 包装器 8.0,但与 AGP 8.1.0-beta04 的行为相同

这是 gradle.kts 文件:

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
    alias(libs.plugins.com.android.library)
    alias(libs.plugins.org.jetbrains.kotlin.android)
}

android {
    namespace = "com.example.mylibrary"
    compileSdk = 33

    defaultConfig {
        minSdk = 26

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {
        release …
Run Code Online (Sandbox Code Playgroud)

android android-gradle-plugin gradle-kotlin-dsl

9
推荐指数
1
解决办法
1363
查看次数

尝试访问其他文件时切换到 Kotlin DSL 未解析的参考

我在尝试对我的 gradle 文件使用 Kotlin DSL 时遇到错误。

build.gradle(app)我有一个函数来检索存储在文件中的 api 密钥中keys.properties,Groovy 中的函数如下:

// Retrieve key api
def getApiKey() {
    def keysFile = file("keys.properties")
    def keysProperties = new Properties()
    keysProperties.load(new FileInputStream(keysFile))
    def apiKey = keysProperties['API_KEY']
    return apiKey
}
Run Code Online (Sandbox Code Playgroud)

当切换到 Kotlin DSL 时,我天真地将函数更改如下:

// Retrieve key for TMDB api
fun getApiKey() {
    val keysFile = file("keys.properties")
    val keysProperties = Properties()
    keysProperties.load(FileInputStream(keysFile))
    val apiKey = keysProperties["API_KEY"]
    return apiKey
}
Run Code Online (Sandbox Code Playgroud)

然后构建返回以下错误:

.../app/build.gradle.kts:13:26: Unresolved reference: Properties
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

编辑

按照 #bam bam 的建议,添加导入import …

android kotlin gradle-kotlin-dsl

8
推荐指数
1
解决办法
3176
查看次数

2 个子模块是否可以具有不同的路径但名称相同?

示例项目(使用 kotlin DSL 在 Gradle 8.1.1 中编写):

https://github.com/tribbloid/scaffold-gradle-kts/tree/16bf3acffca7b83a7e64dbb248a9512caba87e71

该项目有 3 个以上子模块:

include(":lightweight-dependency:core")
include(":lightweight-dependency:extra") // depends on :lightweight-dependency:core
include(":core") // depends on lightweight-dependency:extra
...
Run Code Online (Sandbox Code Playgroud)

编译的时候,出现了如下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Circular dependency between the following tasks:
:core:compileJava
+--- :core:compileJava (*)
+--- :core:compileKotlin
|    +--- :core:compileJava (*)
|    +--- :core:compileKotlin (*)
|    \--- :core:compileScala
|         +--- :core:compileJava (*)
|         +--- :core:compileKotlin (*)
|         \--- :core:compileScala (*)
\--- :core:compileScala (*)

(*) - details omitted (listed previously)
Run Code Online (Sandbox Code Playgroud)

这是有问题的,因为:lightweight-dependency:core …

gradle gradle-kotlin-dsl

8
推荐指数
1
解决办法
108
查看次数