STT icon indicating copy to clipboard operation
STT copied to clipboard

Bug: Android couldn`t find libstt-jni.so

Open Himly1 opened this issue 1 year ago • 0 comments

I am using the libstt-1.4.0-alpha.6-release.aar in my android project I put the libstt-1.4.0-alpha.6-release.aar file to the libs folder And in my build.gradle file I imported the libstt-1.4.0-alpha.6-release.aar to my project Here is the content of my build.gradle

buildscript {
  repositories {
      google()
      mavenCentral()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.5.3'
  }
}

def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") &&  rootProject.getProperty("newArchEnabled") == "true"
}

apply plugin: 'com.android.library'

if (isNewArchitectureEnabled()) {
  apply plugin: 'com.facebook.react'
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['SttReactNative_' + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['SttReactNative_' + name]).toInteger()
}

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault('minSdkVersion')
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  }
  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable 'GradleCompatible'
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  sourceSets {
    main {
      jniLibs.srcDirs = ['libs']
    }
  }
}

repositories {
  mavenCentral()
  google()

  def found = false
  def defaultDir = null
  def androidSourcesName = 'React Native sources'

  if (rootProject.ext.has('reactNativeAndroidRoot')) {
    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
  } else {
    defaultDir = new File(
      projectDir,
      '/../../../node_modules/react-native/android'
    )
  }

  if (defaultDir.exists()) {
    maven {
      url defaultDir.toString()
      name androidSourcesName
    }

    logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
    found = true
  } else {
    def parentDir = rootProject.projectDir

    1.upto(5, {
      if (found) return true
      parentDir = parentDir.parentFile

      def androidSourcesDir = new File(
        parentDir,
        'node_modules/react-native'
      )

      def androidPrebuiltBinaryDir = new File(
        parentDir,
        'node_modules/react-native/android'
      )

      if (androidPrebuiltBinaryDir.exists()) {
        maven {
          url androidPrebuiltBinaryDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
        found = true
      } else if (androidSourcesDir.exists()) {
        maven {
          url androidSourcesDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
        found = true
      }
    })
  }

  if (!found) {
    throw new GradleException(
      "${project.name}: unable to locate React Native android sources. " +
      "Ensure you have you installed React Native as a dependency in your project and try again."
    )
  }
}


dependencies {
    //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation (files("libs/libstt-1.4.0-alpha.6-release.aar"))
// From node_modules
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "SttReactNative"
    codegenJavaPackageName = "com.sttreactnative"
  }
}

At the first its working fine untill I import thte SttModel to my java file here is the content

package com.sttreactnative;

import android.os.Environment;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import ai.coqui.libstt.STTModel;

@ReactModule(name = SttReactNativeModule.NAME)
public class SttReactNativeModule extends ReactContextBaseJavaModule {
  public static final String NAME = "SttReactNative";
  private STTModel model;

  public SttReactNativeModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  @NonNull
  public String getName() {
    return NAME;
  }


  // Example method
  // See https://reactnative.dev/docs/native-modules-android
  @ReactMethod
  public void multiply(double a, double b, Promise promise) {
    promise.resolve(a * b);
  }

  @ReactMethod
  public void loadModel(String path, Promise promise) {
    try {
      model = new STTModel("file:/storage/emulated/0/Download/deepspeech-0.9.3-models.tflite");
      promise.resolve(Environment.getExternalStorageDirectory().toURI().toString());
    }catch (Exception ex) {
      promise.reject("Failed to load the model.", ex);
    }
  }
}

If I remove the line model = new STTModel("file:/storage/emulated/0/Download/deepspeech-0.9.3-models.tflite"); there is no error throwed but if I add the line of the code it will throw an exception says:

image

I dont know what`s going on, any ideas ? Thanks.

Himly1 avatar Nov 29 '22 06:11 Himly1