fluttercommunity / flutter_downloader

Flutter Downloader - A plugin for creating and managing download tasks.
https://pub.dev/packages/flutter_downloader
BSD 3-Clause "New" or "Revised" License
913 stars 514 forks source link

java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization #228

Closed gleesonger closed 4 years ago

gleesonger commented 4 years ago

When I terminate my Android app while FlutterDownloader is downloading the background service keeps crashing, causing a popup on the screen each time. The logs point to the DownloadWorker class as the source of the error however I can not replicate the issue using the Example app included. I am not sure what is unique about my app that is causing this however implementing the fix implied by the error message does fix the issue.

FlutterDownloader version: 1.4.0 Flutter versoin: v1.13.6

Fix: call FlutterMain.startInitialization just before ensureInitializationComplete

DownloadWorker.java:

private void startBackgroundIsolate(Context context) {
  synchronized (isolateStarted) {
    if (backgroundFlutterView == null) {

      SharedPreferences pref = context.getSharedPreferences(FlutterDownloaderPlugin.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
      long callbackHandle = pref.getLong(FlutterDownloaderPlugin.CALLBACK_DISPATCHER_HANDLE_KEY, 0);

      Log.d(TAG,"Initializing Flutter");                       <---- line added
      FlutterMain.startInitialization(context);                <---- line added
      FlutterMain.ensureInitializationComplete(context, null);

      [code removed]
    }
  }

  backgroundChannel = new MethodChannel(backgroundFlutterView, "vn.hunghd/downloader_background");
  backgroundChannel.setMethodCallHandler(this);
}

From the documentation and the source code mutliple calls of startInitialization does nothing https://github.com/flutter/engine/blob/9acec4102a3536e41287704d33640dc8ff54e67f/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Logs

01-27 22:13:33.099  1635  1635 I ActivityManager: Start proc 4939:saicpd.sai_cpd/u0a80 for service saicpd.sai_cpd/androidx.work.impl.background.systemjob.SystemJobService
01-27 22:13:33.189  4939  4939 W zzd     : Application name is not set. Call Builder#setApplicationName.
01-27 22:13:33.196  4939  4961 W zzd     : Application name is not set. Call Builder#setApplicationName.
01-27 22:13:33.205  4939  4939 D isEnabled: No perf enable meta data found in manifest.
01-27 22:13:33.207  4939  4939 I FirebaseInitProvider: FirebaseApp initialization successful
01-27 22:13:33.259  4939  4939 I CrashlyticsCore: Initializing Crashlytics 2.6.8.32
01-27 22:13:33.282  4939  4957 I FA      : App measurement is starting up, version: 18202
01-27 22:13:33.282  4939  4957 I FA      : To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-27 22:13:33.282  4939  4957 I FA      : To enable faster debug mode event logging run:
01-27 22:13:33.282  4939  4957 I FA      :   adb shell setprop debug.firebase.analytics.app saicpd.sai_cpd
01-27 22:13:33.284  4939  4939 I CrashlyticsInitProvider: CrashlyticsInitProvider initialization successful
01-27 22:13:33.366  4939  4971 D NetworkSecurityConfig: No Network Security Config specified, using platform default
01-27 22:13:38.470  4939  4939 D AndroidRuntime: Shutting down VM
01-27 22:13:38.477  4939  4995 D DownloadWorker: DownloadWorker{url=https://web.actuaries.ie/sites/default/files/Podcast-IFRS17-15-Nov-19-080101-003.mp3,filename=8b25d718-2597-4123-995f-b44511ca7327,savedDir=/storage/emulated/0/Android/data/saicpd.sai_cpd/files,header=,isResume=false
01-27 22:13:38.482  4939  4995 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:13:38.482  4939  4995 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:13:38.507  4939  4995 D DownloadWorker: Open connection to https://web.actuaries.ie/sites/default/files/Podcast-IFRS17-15-Nov-19-080101-003.mp3
01-27 22:13:38.542  4939  4957 I FA      : Tag Manager is not found and thus will not be used
--------- beginning of crash
01-27 22:13:38.547  4939  4939 E AndroidRuntime: FATAL EXCEPTION: main
01-27 22:13:38.547  4939  4939 E AndroidRuntime: Process: saicpd.sai_cpd, PID: 4939
01-27 22:13:38.547  4939  4939 E AndroidRuntime: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete(FlutterLoader.java:153)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at io.flutter.view.FlutterMain.ensureInitializationComplete(FlutterMain.java:80)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at vn.hunghd.flutterdownloader.DownloadWorker.startBackgroundIsolate(DownloadWorker.java:112)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at vn.hunghd.flutterdownloader.DownloadWorker.access$000(DownloadWorker.java:59)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at vn.hunghd.flutterdownloader.DownloadWorker$1.run(DownloadWorker.java:97)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:751)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6077)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-27 22:13:38.547  4939  4939 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
01-27 22:13:38.628  4939  4995 D DownloadWorker: Content-Type = audio/mpeg
01-27 22:13:38.628  4939  4995 D DownloadWorker: Content-Length = 118119654
01-27 22:13:38.628  4939  4995 D DownloadWorker: Charset = null

Logs after code update

01-27 22:20:46.974  1635  1635 I ActivityManager: Start proc 5378:saicpd.sai_cpd/u0a80 for service saicpd.sai_cpd/androidx.work.impl.background.systemjob.SystemJobService
01-27 22:20:46.984  5378  5384 E art     : Failed sending reply to debugger: Broken pipe
01-27 22:20:46.984  5378  5384 I art     : Debugger is no longer active
01-27 22:20:46.984  5378  5384 I art     : Starting a blocking GC Instrumentation
01-27 22:20:47.045  5378  5378 W zzd     : Application name is not set. Call Builder#setApplicationName.
01-27 22:20:47.054  5378  5400 W zzd     : Application name is not set. Call Builder#setApplicationName.
01-27 22:20:47.060  5378  5378 D isEnabled: No perf enable meta data found in manifest.
01-27 22:20:47.061  5378  5378 I FirebaseInitProvider: FirebaseApp initialization successful
01-27 22:20:47.124  5378  5378 I CrashlyticsCore: Initializing Crashlytics 2.6.8.32
01-27 22:20:47.147  5378  5378 I CrashlyticsInitProvider: CrashlyticsInitProvider initialization successful
01-27 22:20:47.158  5378  5396 I FA      : App measurement is starting up, version: 18202
01-27 22:20:47.158  5378  5396 I FA      : To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-27 22:20:47.158  5378  5396 I FA      : To enable faster debug mode event logging run:
01-27 22:20:47.158  5378  5396 I FA      :   adb shell setprop debug.firebase.analytics.app saicpd.sai_cpd
01-27 22:20:47.249  5378  5412 D NetworkSecurityConfig: No Network Security Config specified, using platform default
01-27 22:20:48.578  5378  5378 D DownloadWorker: Initializing Flutter
01-27 22:20:48.594  5378  5436 I ResourceExtractor: Found extracted resources res_timestamp-1-1580163525850
01-27 22:20:48.596  5378  5437 D DownloadWorker: DownloadWorker{url=https://web.actuaries.ie/sites/default/files/Podcast-IFRS17-15-Nov-19-080101-003.mp3,filename=7d0c9498-080c-40cf-b955-8cc1b4f726f7,savedDir=/storage/emulated/0/Android/data/saicpd.sai_cpd/files,header=,isResume=false
01-27 22:20:48.607  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:48.607  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:20:48.771  5378  5437 D DownloadWorker: Open connection to https://web.actuaries.ie/sites/default/files/Podcast-IFRS17-15-Nov-19-080101-003.mp3
01-27 22:20:48.845  5378  5437 D DownloadWorker: Content-Type = audio/mpeg
01-27 22:20:48.845  5378  5437 D DownloadWorker: Content-Length = 118119654
01-27 22:20:48.845  5378  5437 D DownloadWorker: Charset = null
01-27 22:20:48.845  5378  5437 D DownloadWorker: fileName = 7d0c9498-080c-40cf-b955-8cc1b4f726f7
01-27 22:20:48.908  5378  5446 I flutter : Observatory listening on http://127.0.0.1:54089/0fBFyB43gUQ=/
01-27 22:20:49.032  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:49.032  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:20:50.558  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:50.558  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:20:52.339  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:52.339  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:20:53.911  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:53.911  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
01-27 22:20:55.148  5378  5437 D skia    : ---- fAsset->read(8192) returned 0
01-27 22:20:55.148  5378  5437 D skia    : --- SkAndroidCodec::NewFromStream returned null
etc

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v1.13.6, on Microsoft Windows [Version 10.0.18362.535], locale en-IE)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Android Studio (version 3.5)
[√] VS Code (version 1.41.1)
[√] Connected device (1 available)

• No issues found!
Screenshot_1580164268
hnvn commented 4 years ago

fixed in v1.4.1