java-api-examples/gradle-kts-examples/README.md
This directory demonstrates how to use sherpa-onnx via Gradle with Kotlin DSL (build.gradle.kts).
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:
./gradlew buildThe project uses JitPack to fetch sherpa-onnx artifacts:
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")
}
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
# 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: ...
| 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 |
| 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 |