Back to Sherpa Onnx

sherpa-onnx Gradle Kotlin DSL (KTS) Example

java-api-examples/gradle-kts-examples/README.md

1.13.53.0 KB
Original Source

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:

kotlin
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:

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

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

Build

bash
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:

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

Run

bash
# Using Gradle wrapper (recommended)
./gradlew run

# Or using system Gradle
gradle run

Expected output:

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

Supported Platforms

PlatformArchitectureArtifact
macOSARM64 (Apple Silicon)sherpa-onnx-native-lib-osx-aarch64
macOSx64 (Intel)sherpa-onnx-native-lib-osx-x64
Linuxx64sherpa-onnx-native-lib-linux-x64
LinuxARM64sherpa-onnx-native-lib-linux-aarch64
Windowsx64sherpa-onnx-native-lib-win-x64
WindowsARM64sherpa-onnx-native-lib-win-arm64

Appendix: Groovy vs Kotlin DSL comparison

FeatureGroovy (build.gradle)Kotlin DSL (build.gradle.kts)
SyntaxDynamic, conciseStatic, type-safe
IDE supportGoodExcellent (auto-completion, refactoring)
Strings'single' or "double""double" only
Function callsimplementation '...'implementation("...")
PropertiesmainClass = '...'mainClass.set("...")
Recommended forLegacy projectsNew projects