From 973111088b1847687ff48dc5b9e3907d5eb44d4b Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 7 Nov 2023 13:27:33 +0100 Subject: [PATCH] android : decouple example into a library and app module (#1445) --- examples/whisper.android/app/build.gradle | 16 +----- .../ui/main/MainScreenViewModel.kt | 8 +-- examples/whisper.android/lib/.gitignore | 1 + examples/whisper.android/lib/build.gradle | 51 +++++++++++++++++++ .../lib/src/main/AndroidManifest.xml | 4 ++ .../com/whispercpp}/whisper/LibWhisper.kt | 2 +- .../whispercpp}/whisper/WhisperCpuConfig.kt | 2 +- .../src/main/jni/whisper/CMakeLists.txt | 0 .../{app => lib}/src/main/jni/whisper/jni.c | 18 +++---- examples/whisper.android/settings.gradle | 1 + 10 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 examples/whisper.android/lib/.gitignore create mode 100644 examples/whisper.android/lib/build.gradle create mode 100644 examples/whisper.android/lib/src/main/AndroidManifest.xml rename examples/whisper.android/{app/src/main/java/com/whispercppdemo => lib/src/main/java/com/whispercpp}/whisper/LibWhisper.kt (99%) rename examples/whisper.android/{app/src/main/java/com/whispercppdemo => lib/src/main/java/com/whispercpp}/whisper/WhisperCpuConfig.kt (98%) rename examples/whisper.android/{app => lib}/src/main/jni/whisper/CMakeLists.txt (100%) rename examples/whisper.android/{app => lib}/src/main/jni/whisper/jni.c (91%) diff --git a/examples/whisper.android/app/build.gradle b/examples/whisper.android/app/build.gradle index 7134d9f1..9f407998 100644 --- a/examples/whisper.android/app/build.gradle +++ b/examples/whisper.android/app/build.gradle @@ -18,9 +18,7 @@ android { vectorDrawables { useSupportLibrary true } - ndk { - abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64' - } + } buildTypes { @@ -43,20 +41,10 @@ android { composeOptions { kotlinCompilerExtensionVersion '1.5.0' } - ndkVersion "25.2.9519653" - externalNativeBuild { - cmake { - path = file("src/main/jni/whisper/CMakeLists.txt") - } - } - packagingOptions { - resources { - excludes += '/META-INF/{AL2.0,LGPL2.1}' - } - } } dependencies { + implementation project(':lib') implementation 'androidx.activity:activity-compose:1.7.2' implementation 'androidx.compose.material:material-icons-core:1.5.0' implementation 'androidx.compose.material3:material3:1.1.1' diff --git a/examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt b/examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt index bd477932..d614ce33 100644 --- a/examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt +++ b/examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt @@ -15,7 +15,7 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import com.whispercppdemo.media.decodeWaveFile import com.whispercppdemo.recorder.Recorder -import com.whispercppdemo.whisper.WhisperContext +import com.whispercpp.whisper.WhisperContext import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -35,7 +35,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() { private val modelsPath = File(application.filesDir, "models") private val samplesPath = File(application.filesDir, "samples") private var recorder: Recorder = Recorder() - private var whisperContext: WhisperContext? = null + private var whisperContext: com.whispercpp.whisper.WhisperContext? = null private var mediaPlayer: MediaPlayer? = null private var recordedFile: File? = null @@ -47,7 +47,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() { } private suspend fun printSystemInfo() { - printMessage(String.format("System Info: %s\n", WhisperContext.getSystemInfo())) + printMessage(String.format("System Info: %s\n", com.whispercpp.whisper.WhisperContext.getSystemInfo())) } private suspend fun loadData() { @@ -78,7 +78,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() { printMessage("Loading model...\n") val models = application.assets.list("models/") if (models != null) { - whisperContext = WhisperContext.createContextFromAsset(application.assets, "models/" + models[0]) + whisperContext = com.whispercpp.whisper.WhisperContext.createContextFromAsset(application.assets, "models/" + models[0]) printMessage("Loaded model ${models[0]}.\n") } diff --git a/examples/whisper.android/lib/.gitignore b/examples/whisper.android/lib/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/examples/whisper.android/lib/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/examples/whisper.android/lib/build.gradle b/examples/whisper.android/lib/build.gradle new file mode 100644 index 00000000..c32a6899 --- /dev/null +++ b/examples/whisper.android/lib/build.gradle @@ -0,0 +1,51 @@ +plugins { + id 'com.android.library' + id 'org.jetbrains.kotlin.android' +} + +android { + namespace 'com.whispercpp' + compileSdk 34 + + defaultConfig { + minSdk 26 + targetSdk 34 + versionCode 1 + versionName "1.0" + + ndk { + abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64' + } + } + + buildTypes { + release { + minifyEnabled false + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } + + ndkVersion "25.2.9519653" + externalNativeBuild { + cmake { + path = file("src/main/jni/whisper/CMakeLists.txt") + } + } + packagingOptions { + resources { + excludes += '/META-INF/{AL2.0,LGPL2.1}' + } + } +} + +dependencies { + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'com.google.android.material:material:1.8.0' +} \ No newline at end of file diff --git a/examples/whisper.android/lib/src/main/AndroidManifest.xml b/examples/whisper.android/lib/src/main/AndroidManifest.xml new file mode 100644 index 00000000..a5918e68 --- /dev/null +++ b/examples/whisper.android/lib/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt b/examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt similarity index 99% rename from examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt rename to examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt index b0d67037..513202fa 100644 --- a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt +++ b/examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper +package com.whispercpp.whisper import android.content.res.AssetManager import android.os.Build diff --git a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.kt b/examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/WhisperCpuConfig.kt similarity index 98% rename from examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.kt rename to examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/WhisperCpuConfig.kt index 5fa9a4e4..edfb415f 100644 --- a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.kt +++ b/examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/WhisperCpuConfig.kt @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper +package com.whispercpp.whisper import android.util.Log import java.io.BufferedReader diff --git a/examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt b/examples/whisper.android/lib/src/main/jni/whisper/CMakeLists.txt similarity index 100% rename from examples/whisper.android/app/src/main/jni/whisper/CMakeLists.txt rename to examples/whisper.android/lib/src/main/jni/whisper/CMakeLists.txt diff --git a/examples/whisper.android/app/src/main/jni/whisper/jni.c b/examples/whisper.android/lib/src/main/jni/whisper/jni.c similarity index 91% rename from examples/whisper.android/app/src/main/jni/whisper/jni.c rename to examples/whisper.android/lib/src/main/jni/whisper/jni.c index a8b3ded4..08825ed9 100644 --- a/examples/whisper.android/app/src/main/jni/whisper/jni.c +++ b/examples/whisper.android/lib/src/main/jni/whisper/jni.c @@ -131,7 +131,7 @@ static struct whisper_context *whisper_init_from_asset( } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContextFromAsset( JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) { UNUSED(thiz); struct whisper_context *context = NULL; @@ -142,7 +142,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset( } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContext( JNIEnv *env, jobject thiz, jstring model_path_str) { UNUSED(thiz); struct whisper_context *context = NULL; @@ -153,7 +153,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext( } JNIEXPORT void JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_freeContext( JNIEnv *env, jobject thiz, jlong context_ptr) { UNUSED(env); UNUSED(thiz); @@ -162,7 +162,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext( } JNIEXPORT void JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_fullTranscribe( JNIEnv *env, jobject thiz, jlong context_ptr, jint num_threads, jfloatArray audio_data) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; @@ -194,7 +194,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe( } JNIEXPORT jint JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentCount( JNIEnv *env, jobject thiz, jlong context_ptr) { UNUSED(env); UNUSED(thiz); @@ -203,7 +203,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount( } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment( JNIEnv *env, jobject thiz, jlong context_ptr, jint index) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; @@ -213,7 +213,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment( } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo( +Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo( JNIEnv *env, jobject thiz ) { UNUSED(thiz); @@ -223,7 +223,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo( } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz, +Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz, jint n_threads) { UNUSED(thiz); const char *bench_ggml_memcpy = whisper_bench_memcpy_str(n_threads); @@ -231,7 +231,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *en } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz, +Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz, jint n_threads) { UNUSED(thiz); const char *bench_ggml_mul_mat = whisper_bench_ggml_mul_mat_str(n_threads); diff --git a/examples/whisper.android/settings.gradle b/examples/whisper.android/settings.gradle index 3deecee6..dbd8508e 100644 --- a/examples/whisper.android/settings.gradle +++ b/examples/whisper.android/settings.gradle @@ -14,3 +14,4 @@ dependencyResolutionManagement { } rootProject.name = "WhisperCppDemo" include ':app' +include ':lib'