java-api-examples/gradle-examples/README.md
This directory demonstrates how to use sherpa-onnx via Gradle.
The project uses JitPack to fetch sherpa-onnx artifacts. There are two ways to declare the dependencies.
This is the easiest way — one dependency pulls in everything:
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.k2-fsa:sherpa-onnx:1.13.5'
}
This is the recommended way for production use. It splits the JVM API and native libs so you only ship the platform you need, keeping the final jar lightweight:
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
// 1. JVM core API
implementation 'com.github.k2-fsa.sherpa-onnx:sherpa-onnx-jvm:1.13.5'
// 2. Platform native lib — pick ONE for your target platform
implementation 'com.github.k2-fsa.sherpa-onnx:sherpa-onnx-native-lib-osx-aarch64:1.13.5'
}
Available native lib artifacts for each platform:
| Platform | Artifact |
|---|---|
| macOS ARM64 | sherpa-onnx-native-lib-osx-aarch64 |
| macOS x64 | 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 |
Note: The
build.gradlein this directory uses Approach 2 by default. Approach 1 is included as a comment for reference.
cd java-api-examples/gradle-examples
# Using Gradle wrapper (recommended)
./gradlew build
# Or using system Gradle
gradle build
# 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: ...
The two approaches produce very different fat jars:
| Approach 1 (simple) | Approach 2 (multi-module) | |
|---|---|---|
| Jar size (compressed) | ~95 MB | ~9 MB |
| Uncompressed size | ~286 MB | ~33 MB |
| Total files | ~295 | ~253 |
| JVM class files | ~236 | ~236 |
| Native libs included | ALL platforms | macOS ARM64 only |
Approach 2 is ~10x smaller (~9 MB vs ~95 MB) because it only ships the native libs for your target platform. This matters for:
# Build and check the jar
./gradlew build
ls -lh build/libs/*.jar
# List native libs in the jar
unzip -l build/libs/sherpa-onnx-gradle-example.jar | grep -E "\.so$|\.dylib$|\.dll$"