Files
sherpa-onnx/java-api-examples/gradle-kts-examples

sherpa-onnx Gradle Kotlin DSL (KTS) Example

This directory demonstrates how to use sherpa-onnx via Gradle with Kotlin DSL (build.gradle.kts).

Prerequisites

  • JDK 8 or above
  • Gradle 8.x+ (or use the included Gradle wrapper)

How It Works

The build.gradle.kts automatically detects your OS and architecture at build time:

val osName = System.getProperty("os.name").lowercase()
val osArch = System.getProperty("os.arch").lowercase()

val targetNativeClassifier = when {
    osName.contains("mac") || osName.contains("darwin") -> {
        if (osArch == "aarch64" || osArch == "arm64") "osx-aarch64" else "osx-x64"
    }
    osName.contains("linux") -> {
        if (osArch == "aarch64" || osArch == "arm64") "linux-aarch64" else "linux-x64"
    }
    osName.contains("win") -> {
        if (osArch == "aarch64" || osArch == "arm64") "win-arm64" else "win-x64"
    }
    else -> throw GradleException("Unsupported OS: $osName, Arch: $osArch")
}

This means:

  • No manual configuration needed — just run ./gradlew build
  • Works on any platform — macOS, Linux, Windows x64, Windows ARM64
  • No CI scripts to modify — the build file handles everything

Dependencies

The project uses JitPack to fetch sherpa-onnx artifacts:

dependencies {
    // 1. JVM core API
    implementation("com.github.k2-fsa.sherpa-onnx:sherpa-onnx-jvm:v2.0.0")

    // 2. Platform native lib (auto-detected)
    implementation("com.github.k2-fsa.sherpa-onnx:sherpa-onnx-native-lib-$targetNativeClassifier:v2.0.0")
}

Build

cd java-api-examples/gradle-kts-examples

# Using Gradle wrapper (recommended)
./gradlew build

# Or using system Gradle
gradle build

The build output will show the auto-detected platform:

--> Auto-detected platform native lib: osx-aarch64

Run

# Using Gradle wrapper (recommended)
./gradlew run

# Or using system Gradle
gradle run

Expected output:

sherpa-onnx version: x.y.z
sherpa-onnx gitSha1: ...
sherpa-onnx gitDate: ...

Supported Platforms

Platform Architecture Artifact
macOS ARM64 (Apple Silicon) sherpa-onnx-native-lib-osx-aarch64
macOS x64 (Intel) sherpa-onnx-native-lib-osx-x64
Linux x64 sherpa-onnx-native-lib-linux-x64
Linux ARM64 sherpa-onnx-native-lib-linux-aarch64
Windows x64 sherpa-onnx-native-lib-win-x64
Windows ARM64 sherpa-onnx-native-lib-win-arm64

Appendix: Groovy vs Kotlin DSL comparison

Feature Groovy (build.gradle) Kotlin DSL (build.gradle.kts)
Syntax Dynamic, concise Static, type-safe
IDE support Good Excellent (auto-completion, refactoring)
Strings 'single' or "double" "double" only
Function calls implementation '...' implementation("...")
Properties mainClass = '...' mainClass.set("...")
Recommended for Legacy projects New projects