Search code examples
androidkotlindependency-managementpocketsphinx

In Android Studio mavenCentral PocketSphinx library is not working


I created an app like AI assistance for my Android version 13 phone in Android Studio and I am having trouble importing the "PocketSphinx" library to use it for offline custom wake word detection that works in the background and if the custom word is being said so my app would open.

I am using Kotlin language, "SDK 33" and the template "Empty Views Activity" (I don't want to use Porcupine library).

I tried to search on Google and stack overflow to check how can I fix this problem by myself but couldn't find an answer that is updated and works for 2023 and version 13 of Android.

I tried the following:

  1. FileProject StructureDependenciesAll Dependencies → "+" button → Library DependencyPocketSphinx

    It says: "Nothing to show" so it could not find the library even that I have the "google" and "mavenCentral" repositories in settings.grade.kts root file

  2. Gradle Scripts → build.gradle.kts (Module:app) → dependencies → implementation("edu.cmu.pocketsphinx:pocketsphinx-android:5prealpha")

  3. I also tried using "Vosk" Gradle Scripts → build.gradle.kts (Module:app) → dependencies → implementation("ai.kitt.android:vosk-android:0.6.3")

  4. And also tried to restart Android Studio and it still did not work.

  5. And also tried reinstalling Android Studio and it still did not work.

  6. Of course, I synced every time I added a dependency and used every time BuildClean Project and could not rebuild the project if I added the imports that I tried which you can see at the bottom. However, I was able to use rebuild when I only added the dependencies and did not use any imports.

These are my project dependencies:

dependencies {
    implementation("androidx.core:core-ktx:1.7.0") // Not Updated version
    implementation("com.google.android.material:material:1.5.0") // Not Updated version
    implementation("androidx.appcompat:appcompat:1.6.1") // Updated version
    implementation("androidx.constraintlayout:constraintlayout:2.1.4") // Updated version
    implementation("androidx.media:media:1.6.0") // Updated version
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") // Updated version
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.compose.ui:ui-text-android:1.5.4") // Updated version
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5") // Updated version
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") // Updated version
}

I know that the first 2 dependencies are not the updated versions and this is because If I switch one of them or 2 of them to their updated versions, my program is not working at all and I get errors, so I prefer staying in those versions so my project will work.

These are my project android settings:

android {
    namespace = "com.example.jarvis"
    compileSdk = 33

    defaultConfig {
        applicationId = "com.example.jarvis"
        minSdk = 33
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

And these are all the imports that I tried of "PocketSphinx" and of "Vosk" (The ones that starts with ai, edu gave me errors that they are not imported. "Unresolved reference: ai", "Unresolved reference: edu"):

import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import android.util.Log import android.util.Log import ai.kitt.android.vosk.VoskModel import ai.kitt.android.vosk.VoskRecognizer import ai.kitt.android.vosk.VoskResult import ai.kitt.android.vosk.VoskRecognizer.Listener import edu.cmu.sphinx.api.Configuration import edu.cmu.sphinx.api.LiveSpeechRecognizer import edu.cmu.sphinx.api.RecognitionResult import edu.cmu.sphinx.api.SpeechRecognizer

How can I solve this problem and use this library, and what imports do I need to use in the Kotlin code?


Solution

  • Seems like dependency didn't added properly. Instead of using AndroidStudio settings for adding dependency you can add dependency definition directly in build file.

    According to this instruction, firstly add mavenCenter() in settings.gradle.kts

    // ...
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    

    It is added by default so probably in new project is should be already configured.

    Next edit file app/src/build.gradle.kts and in dependencies add new line

    plugins {
      // ...
    }
    
    android {
      // ...
    }
    
    dependencies {
      // ...
      implementation("com.alphacephei:vosk-android:0.3.32")
    }
    

    There is also sample application source code which can be useful https://github.com/alphacep/vosk-android-demo