In my flutter app, I use the flutter_tts 4.0.2 package. I tried to debug my app on a old phone, a Sony Xperia E4g E2033 with Android 4.4.4. Unfortunatly, the app crash when starting with the following error.

√  Built build\app\outputs\flutter-apk\app-dev-debug.apk.
W/FlutterEngineCxnRegstry(14000): Attempted to register plugin (io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin@4287be40) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@426d0500).
E/AndroidRuntime(14000): FATAL EXCEPTION: main
E/AndroidRuntime(14000): Process: meihouwang.chinese_decoder.dev.dev, PID: 14000
E/AndroidRuntime(14000): java.lang.NoSuchMethodError: android.speech.tts.TextToSpeech.getDefaultVoice
E/AndroidRuntime(14000):    at com.tundralabs.fluttertts.FlutterTtsPlugin.firstTimeOnInitListener$lambda$5(FlutterTtsPlugin.kt:234)
E/AndroidRuntime(14000):    at com.tundralabs.fluttertts.FlutterTtsPlugin.$r8$lambda$KxrDwWtOaRLTbUOlm0Ru9-56Ri8(FlutterTtsPlugin.kt)
E/AndroidRuntime(14000):    at com.tundralabs.fluttertts.FlutterTtsPlugin$$ExternalSyntheticLambda0.onInit(D8$$SyntheticClass)
E/AndroidRuntime(14000):    at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:701)
E/AndroidRuntime(14000):    at android.speech.tts.TextToSpeech.access$1400(TextToSpeech.java:58)
E/AndroidRuntime(14000):    at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:1509)
E/AndroidRuntime(14000):    at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:1471)
E/AndroidRuntime(14000):    at android.os.AsyncTask.finish(AsyncTask.java:632)
E/AndroidRuntime(14000):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
E/AndroidRuntime(14000):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
E/AndroidRuntime(14000):    at android.os.Handler.dispatchMessage(Handler.java:110)
E/AndroidRuntime(14000):    at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime(14000):    at android.app.ActivityThread.main(ActivityThread.java:5486)
E/AndroidRuntime(14000):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(14000):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(14000):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
E/AndroidRuntime(14000):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
E/AndroidRuntime(14000):    at dalvik.system.NativeStart.main(Native Method)

I know there is no tts availble on android 4.4.4 so before initialysing FlutterTts, I use the following code to prevent the initilisation on icompatible OS

      var androidInfo = await
      DeviceInfoPlugin().androidInfo;
      var sdkInt = androidInfo.version.sdkInt;
      // Text to speech only available in Lolipop
      if(sdkInt < 21){
        _available = false;
        return;
      }
    }

But when I put breakpoint, I don't reach this code before the crash. So it's probably the flutter framework loading the library even if it has not yet be called from the app code.

So how can I prevent this crash ?

1

There are 1 best solutions below

0
Gael On

while debugging this I got the following message

C:\dev\perso\chinesedecoder\chinese_decoder\android\app\src\debug\AndroidManifest.xml Error:
    uses-sdk:minSdkVersion 19 cannot be smaller than version 21 declared in library 
[:flutter_tts] C:\dev\perso\chinesedecoder\chinese_decoder\build\flutter_tts\intermediates\merged_manifest\debug\AndroidManifest.xml
as the library might be using APIs not available in 19
    Suggestion: use a compatible library with a minSdk of at most 19,
    or increase this project's minSdk version to at least 21,
    or use tools:overrideLibrary="com.tundralabs.fluttertts" 
    to force usage (may lead to runtime failures)

So it was the use of tools:overrideLibrary="com.tundralabs.fluttertts" in the manifest file which leaded to an uncatchable exception.
removing it prevent the application to be compiled if the minSdk is smaller than 21.

So I guess there is nothing I can do except dropping support for android 4.4.4. Anyway it's already 10 year old and Google stopped supporting this version last year.