Skip to content

Commit 9731110

Browse files
authored
android : decouple example into a library and app module (ggml-org#1445)
1 parent 11b5030 commit 9731110

File tree

10 files changed

+74
-29
lines changed

10 files changed

+74
-29
lines changed

examples/whisper.android/app/build.gradle

+2-14
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ android {
1818
vectorDrawables {
1919
useSupportLibrary true
2020
}
21-
ndk {
22-
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
23-
}
21+
2422
}
2523

2624
buildTypes {
@@ -43,20 +41,10 @@ android {
4341
composeOptions {
4442
kotlinCompilerExtensionVersion '1.5.0'
4543
}
46-
ndkVersion "25.2.9519653"
47-
externalNativeBuild {
48-
cmake {
49-
path = file("src/main/jni/whisper/CMakeLists.txt")
50-
}
51-
}
52-
packagingOptions {
53-
resources {
54-
excludes += '/META-INF/{AL2.0,LGPL2.1}'
55-
}
56-
}
5744
}
5845

5946
dependencies {
47+
implementation project(':lib')
6048
implementation 'androidx.activity:activity-compose:1.7.2'
6149
implementation 'androidx.compose.material:material-icons-core:1.5.0'
6250
implementation 'androidx.compose.material3:material3:1.1.1'

examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import androidx.lifecycle.viewmodel.initializer
1515
import androidx.lifecycle.viewmodel.viewModelFactory
1616
import com.whispercppdemo.media.decodeWaveFile
1717
import com.whispercppdemo.recorder.Recorder
18-
import com.whispercppdemo.whisper.WhisperContext
18+
import com.whispercpp.whisper.WhisperContext
1919
import kotlinx.coroutines.Dispatchers
2020
import kotlinx.coroutines.launch
2121
import kotlinx.coroutines.runBlocking
@@ -35,7 +35,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
3535
private val modelsPath = File(application.filesDir, "models")
3636
private val samplesPath = File(application.filesDir, "samples")
3737
private var recorder: Recorder = Recorder()
38-
private var whisperContext: WhisperContext? = null
38+
private var whisperContext: com.whispercpp.whisper.WhisperContext? = null
3939
private var mediaPlayer: MediaPlayer? = null
4040
private var recordedFile: File? = null
4141

@@ -47,7 +47,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
4747
}
4848

4949
private suspend fun printSystemInfo() {
50-
printMessage(String.format("System Info: %s\n", WhisperContext.getSystemInfo()))
50+
printMessage(String.format("System Info: %s\n", com.whispercpp.whisper.WhisperContext.getSystemInfo()))
5151
}
5252

5353
private suspend fun loadData() {
@@ -78,7 +78,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
7878
printMessage("Loading model...\n")
7979
val models = application.assets.list("models/")
8080
if (models != null) {
81-
whisperContext = WhisperContext.createContextFromAsset(application.assets, "models/" + models[0])
81+
whisperContext = com.whispercpp.whisper.WhisperContext.createContextFromAsset(application.assets, "models/" + models[0])
8282
printMessage("Loaded model ${models[0]}.\n")
8383
}
8484

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
plugins {
2+
id 'com.android.library'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
namespace 'com.whispercpp'
8+
compileSdk 34
9+
10+
defaultConfig {
11+
minSdk 26
12+
targetSdk 34
13+
versionCode 1
14+
versionName "1.0"
15+
16+
ndk {
17+
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
18+
}
19+
}
20+
21+
buildTypes {
22+
release {
23+
minifyEnabled false
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
}
33+
34+
ndkVersion "25.2.9519653"
35+
externalNativeBuild {
36+
cmake {
37+
path = file("src/main/jni/whisper/CMakeLists.txt")
38+
}
39+
}
40+
packagingOptions {
41+
resources {
42+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
43+
}
44+
}
45+
}
46+
47+
dependencies {
48+
implementation 'androidx.core:core-ktx:1.9.0'
49+
implementation 'androidx.appcompat:appcompat:1.6.1'
50+
implementation 'com.google.android.material:material:1.8.0'
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
</manifest>

examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt renamed to examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whispercppdemo.whisper
1+
package com.whispercpp.whisper
22

33
import android.content.res.AssetManager
44
import android.os.Build

examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.kt renamed to examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/WhisperCpuConfig.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.whispercppdemo.whisper
1+
package com.whispercpp.whisper
22

33
import android.util.Log
44
import java.io.BufferedReader

examples/whisper.android/app/src/main/jni/whisper/jni.c renamed to examples/whisper.android/lib/src/main/jni/whisper/jni.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static struct whisper_context *whisper_init_from_asset(
131131
}
132132

133133
JNIEXPORT jlong JNICALL
134-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset(
134+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContextFromAsset(
135135
JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) {
136136
UNUSED(thiz);
137137
struct whisper_context *context = NULL;
@@ -142,7 +142,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset(
142142
}
143143

144144
JNIEXPORT jlong JNICALL
145-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
145+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContext(
146146
JNIEnv *env, jobject thiz, jstring model_path_str) {
147147
UNUSED(thiz);
148148
struct whisper_context *context = NULL;
@@ -153,7 +153,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
153153
}
154154

155155
JNIEXPORT void JNICALL
156-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext(
156+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_freeContext(
157157
JNIEnv *env, jobject thiz, jlong context_ptr) {
158158
UNUSED(env);
159159
UNUSED(thiz);
@@ -162,7 +162,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext(
162162
}
163163

164164
JNIEXPORT void JNICALL
165-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe(
165+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_fullTranscribe(
166166
JNIEnv *env, jobject thiz, jlong context_ptr, jint num_threads, jfloatArray audio_data) {
167167
UNUSED(thiz);
168168
struct whisper_context *context = (struct whisper_context *) context_ptr;
@@ -194,7 +194,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe(
194194
}
195195

196196
JNIEXPORT jint JNICALL
197-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount(
197+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentCount(
198198
JNIEnv *env, jobject thiz, jlong context_ptr) {
199199
UNUSED(env);
200200
UNUSED(thiz);
@@ -203,7 +203,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount(
203203
}
204204

205205
JNIEXPORT jstring JNICALL
206-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment(
206+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment(
207207
JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
208208
UNUSED(thiz);
209209
struct whisper_context *context = (struct whisper_context *) context_ptr;
@@ -213,7 +213,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment(
213213
}
214214

215215
JNIEXPORT jstring JNICALL
216-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo(
216+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
217217
JNIEnv *env, jobject thiz
218218
) {
219219
UNUSED(thiz);
@@ -223,15 +223,15 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo(
223223
}
224224

225225
JNIEXPORT jstring JNICALL
226-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz,
226+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz,
227227
jint n_threads) {
228228
UNUSED(thiz);
229229
const char *bench_ggml_memcpy = whisper_bench_memcpy_str(n_threads);
230230
jstring string = (*env)->NewStringUTF(env, bench_ggml_memcpy);
231231
}
232232

233233
JNIEXPORT jstring JNICALL
234-
Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz,
234+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz,
235235
jint n_threads) {
236236
UNUSED(thiz);
237237
const char *bench_ggml_mul_mat = whisper_bench_ggml_mul_mat_str(n_threads);

examples/whisper.android/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ dependencyResolutionManagement {
1414
}
1515
rootProject.name = "WhisperCppDemo"
1616
include ':app'
17+
include ':lib'

0 commit comments

Comments
 (0)