Gradle compileKotlin includeRuntime not adding runtime to jar
Asked Answered
B

3

8

I have a Kotlin Gradle project, and I would like to include Kotlin's runtime and stdlib in the jar file. I'm currently using this, but it's not including the runtime or stdlib when I build the project using the build.gradle configuration.

compileKotlin {
    kotlinOptions {
        includeRuntime = true
        noStdlib = false
    }
}

This is the Gradle code I'm using to include the runtime/stdlib in the jar, but it isn't working like I expect it to. Here's the full build.gradle file for some context: https://github.com/BenWoodworth/CrossPlatformGreeter/blob/bd1da79f36e70e3d88ed871bc35502ecc3a852fb/build.gradle#L35-L43

Kotlin's Gradle documentation seems to indicate that setting kotlinOptions.includeRuntime to true should include the Kotlin runtime in the resulting .jar.
https://kotlinlang.org/docs/reference/using-gradle.html#attributes-specific-for-kotlin

Edit: This might be related. When I run compileKotlin, I'm getting a couple of warnings related to the runtime:

:compileKotlin
w: Classpath entry points to a non-existent location: <no_path>\lib\kotlin-runtime.jar

BUILD SUCCESSFUL
Bhang answered 24/2, 2017 at 1:37 Comment(0)
B
3

Here's an alternative I came up with. It'll add the Kotlin runtime and stdlib to the jar using the jar task.

jar {
    from {
        String[] include = [
            "kotlin-runtime-${version_kotlin}.jar",
            "kotlin-stdlib-${version_kotlin}.jar"
        ]

        configurations.compile
                .findAll { include.contains(it.name) }
                .collect { it.isDirectory() ? it : zipTree(it) }
    }
}
Bhang answered 25/2, 2017 at 3:48 Comment(0)
B
1

Gradle Kotlin DSL:

tasks.withType<Jar> {

    val include = setOf("kotlin-stdlib-1.4.0.jar")

    configurations.runtimeClasspath.get()
        .filter { it.name in include }
        .map { zipTree(it) }
        .also { from(it) }
}
Businesswoman answered 1/9, 2020 at 19:16 Comment(0)
E
0

Try this:

  1. Build script
  2. Unpack jar
  3. Add kotlin runtime and rapack it

type gradle packJar to create jar with kotlin runtime in it or type gradle runJar to create and run the jar file

build Script

Edison answered 24/2, 2022 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.