Kotlin JVM Builds and Tests
Build, run, test, and cache Kotlin and mixed Java libraries and applications on the JVM with Struo, and produce Kotlin/Native frameworks for Apple apps.
Kotlin targets are declared in struo.toml. Android Kotlin sources use the
Android target kinds.
Prerequisites#
Install the pinned Kotlin compiler and Java Development Kit through mise:
mise install
mise exec -- kotlinc -version
mise exec -- java -versionDeclare targets#
Declare a library and binary#
[[target]]
name = "Greeting"
kind = "kotlin_jvm_library"
srcs = ["src/main/kotlin/dev/struo/greeting/**/*.kt"]
[target.attrs]
module_name = "greeting"
jvm_target = "17"
[[target]]
name = "Hello"
kind = "kotlin_jvm_binary"
srcs = ["src/main/kotlin/dev/struo/hello/**/*.kt"]
deps = ["./Greeting"]
[target.attrs]
main_class = "dev.struo.hello.MainKt"
args = ["Struo"]Query, build, and run#
Inspect the graph, then build and run the binary:
struo query schema kotlin_jvm_library
struo query target ./Hello
struo build ./Hello
struo run ./HelloThe library and binary compile to Java archives. struo run builds the archive
first, then starts the main class with the complete runtime classpath.
Run host-side tests#
Declare kotlin_jvm_test with normal compile dependencies and optional
runtime-only dependencies:
[[target]]
name = "GreetingTests"
kind = "kotlin_jvm_test"
srcs = ["src/test/kotlin/**/*.kt"]
deps = ["./Greeting"]
[target.dependencies]
runtime_deps = ["./TestRuntime"]
[target.attrs]
labels = ["unit"]Run it:
struo test GreetingTestsThe runner discovers zero-argument methods whose names begin with test, and
also recognizes JUnit and Kotlin test annotations
when those libraries are on the classpath. Results use the same test record as
other ecosystems, so affected-test queries and MCP tools work unchanged.
Mix Java sources#
List .java files in srcs next to the Kotlin sources. Kotlin and Java can
reference each other in either direction, and Java test classes in a
kotlin_jvm_test are discovered like Kotlin ones:
[[target]]
name = "Greeting"
kind = "kotlin_jvm_library"
srcs = ["src/main/kotlin/**/*.kt", "src/main/java/**/*.java"]
[target.attrs]
jvm_target = "1.8"
javac_opts = ["-Xlint:none"]Manage dependencies#
Import prebuilt Java archives#
Use java_import for archives built
elsewhere, such as Maven artifacts resolved by another tool:
[[target]]
name = "TestLibraries"
kind = "java_import"
[target.attrs]
jars = ["third_party/junit-4.13.2.jar", "third_party/hamcrest-core-1.3.jar"]Any Kotlin or Android role that accepts java_library accepts the import.
Model classpath roles#
Use deps for normal compile and runtime libraries. More precise relationships
belong under [target.dependencies]:
[target.dependencies]
associates = ["./FriendModule"]
exported_deps = ["./PublicTypes"]
provided_deps = ["./CompileApi"]
runtime_deps = ["./RuntimeSupport"]associatespasses Kotlin friend paths so internal declarations are visible.exported_depsalso propagates to downstream compile and runtime classpaths.provided_depsappears only while compiling.runtime_depsappears only while running.
Each role is validated before Kotlin runs, so a runtime-only artifact in a compile-only role fails early.
Use compiler plug-ins#
Pass package-relative compiler plug-in Java archives and their options through attributes:
[target.attrs]
compiler_plugin_jars = ["tools/serialization-plugin.jar"]
compiler_plugin_options = [
"plugin:org.example.serialization:enabled=true",
]Changing the archive contents or options invalidates the cached compile.
Use Kotlin from Android and Apple#
Use android_library or
android_binary for Android Kotlin
sources, because those target kinds also own resources and application
packaging. Use
kotlin_apple_framework when an
Apple target should consume Kotlin/Native code as a framework.
Limitations#
- Generated source directories and Kotlin symbol processing are not supported. Keep those pipelines in a script target.
- Resource packaging, annotation processing, incremental compilation,
interface archive generation, and test framework lifecycle integration are
not part of the Kotlin JVM target kinds. Android local tests are available
through
android_local_test.
Target kinds#
Next steps#
- Android: package Kotlin code into an Android application.
- Apple: link a Kotlin/Native framework into an Apple application.
- Testing and Scheduling: select and report test runs.