Repository: flutter
Issue: App crashes on hot restart with custom FlutterApplication
Labels: c: crash,tool,t: hot reload,P2,team-tool,triaged-tool

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill our the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

Hot restart crashes when defining a custom `FlutterApplication` class. A custom class is needed for some plugins, such as the `android_alarm_manager`.

## Steps to Reproduce
1. Clone the `android_alarm_manager` example app
1. Run with `flutter run --verbose`
1. Hot restart multiple times  (it never crashed on the first hot restart for me)


## Logs

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```
[+4193 ms] Performing hot restart...
[  +54 ms] Scanned through 443 files in 50ms
[   +3 ms] Syncing files to device Wileyfox Swift...
[   +1 ms] Scanning asset files
[   +7 ms] <- reset
[   +1 ms] Compiling dart to kernel with 0 updated files
[   +8 ms] <- recompile package:android_alarm_manager_example/main.dart b0d7dbd5-3aad-445b-81c5-3109bbb64628
[   +1 ms] <- b0d7dbd5-3aad-445b-81c5-3109bbb64628
[   +9 ms] -> result c3961f4c-514f-4f6d-960b-769bb2476be9
[ +361 ms] -> c3961f4c-514f-4f6d-960b-769bb2476be9
[   +2 ms] -> c3961f4c-514f-4f6d-960b-769bb2476be9 build\app.dill 0
[   +6 ms] Updating files
[+1123 ms] DevFS: Sync finished
[   +2 ms] Syncing files to device Wileyfox Swift... (completed in 1,525ms)
[   +6 ms] Synced 17.3MB.
[   +2 ms] <- accept
[        ] Sending to VM service: getIsolate({isolateId: isolates/3272278510078987})
[   +7 ms] Sending to VM service: getIsolate({isolateId: isolates/843250359330915})
[   +2 ms] Sending to VM service: getIsolate({isolateId: isolates/2521375301490347})
[  +26 ms] Result: {type: Isolate, id: isolates/3272278510078987, name: main, number: 3272278510078987, _originNumber: 3272278510078987, startTime:
1569259838857, _heaps: {new: {type: HeapSpace, name: new, vmName: Scavenger, collections: 0, avgCollectionPeriodMillis...
[   +2 ms] Sending to VM service: resume({isolateId: isolates/3272278510078987})
[  +15 ms] Result: {type: Isolate, id: isolates/843250359330915, name: main, number: 843250359330915, _originNumber: 843250359330915, startTime:
1569259838699, _heaps: {new: {type: HeapSpace, name: new, vmName: Scavenger, collections: 0, avgCollectionPeriodMillis: 0...
[  +17 ms] Result: {type: Isolate, id: isolates/2521375301490347, name: main, number: 2521375301490347, _originNumber: 2521375301490347, startTime:
1569259838100, _heaps: {new: {type: HeapSpace, name: new, vmName: Scavenger, collections: 0, avgCollectionPeriodMillis...
[   +6 ms] Error 105 received from application: Isolate must be runnable
[   +7 ms] {request: {method: resume, params: {isolateId: isolates/3272278510078987}}, details: Isolate must be runnable before this request is made.}
[   +2 ms] Performing hot restart... (completed in 1,680ms)
[   +4 ms] Restarted application in 1,688ms.
[   +9 ms] Sending to VM service: _flutter.runInView({viewId: _flutterView/0x7f6c4f3f20, mainScript:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/lib/main.dart.dill, packagesFile:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/.packages, assetDirectory:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/build/flutter_assets})
[   +3 ms] Sending to VM service: _flutter.runInView({viewId: _flutterView/0x7f6c504f20, mainScript:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/lib/main.dart.dill, packagesFile:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/.packages, assetDirectory:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/build/flutter_assets})
[   +6 ms] Sending to VM service: _flutter.runInView({viewId: _flutterView/0x7f8904c120, mainScript:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/lib/main.dart.dill, packagesFile:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/.packages, assetDirectory:
file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/build/flutter_assets})
[  +10 ms] Application finished.
hot restart failed to complete

#0      throwToolExit (package:flutter_tools/src/base/common.dart:28:3)
#1      TerminalHandler._commonTerminalInputHandler (package:flutter_tools/src/resident_runner.dart:1067:11)
<asynchronous suspension>
#2      TerminalHandler.processTerminalInput (package:flutter_tools/src/resident_runner.dart:1117:13)
<asynchronous suspension>
#3      _rootRunUnary (dart:async/zone.dart:1132:38)
#4      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#5      _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#7      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#8      _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:375:20)
#9      _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:250:5)
#10     _AsBroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:474:11)
#11     _rootRunUnary (dart:async/zone.dart:1132:38)
#12     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#13     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#14     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#15     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#16     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#17     _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#18     _StringAdapterSink.add (dart:convert/string_conversion.dart:236:11)
#19     _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:241:7)
#20     _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:312:20)
#21     _ErrorHandlingAsciiDecoderSink.addSlice (dart:convert/ascii.dart:252:17)
#22     _ErrorHandlingAsciiDecoderSink.add (dart:convert/ascii.dart:238:5)
#23     _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:72:18)
#24     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#25     _rootRunUnary (dart:async/zone.dart:1132:38)
#26     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#27     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#28     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#29     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#30     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:764:19)
#31     _StreamController._add (dart:async/stream_controller.dart:640:7)
#32     _StreamController.add (dart:async/stream_controller.dart:586:5)
#33     _Socket._onData (dart:io-patch/socket_patch.dart:1791:41)
#34     _rootRunUnary (dart:async/zone.dart:1136:13)
#35     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#36     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#37     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#38     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#39     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:764:19)
#40     _StreamController._add (dart:async/stream_controller.dart:640:7)
#41     _StreamController.add (dart:async/stream_controller.dart:586:5)
#42     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1339:33)
#43     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:860:14)
#44     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#45     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#46     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:116:13)
#47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:173:5)



[  +31 ms] DevFS: Deleting filesystem on the device (file:///data/user/0/io.flutter.plugins.androidalarmmanagerexample/code_cache/exampleMNGMMD/example/)
[   +1 ms] Sending to VM service: _deleteDevFS({fsName: example})
```


## Flutter analyze
```
Analyzing example...
No issues found! (ran in 13.7s)
```

## VSCode Output
Crash on second hot reload
```
Restarted application in 2,628ms.
I/flutter (29380): [2019-09-23 10:28:42.220606] main run
I/flutter (29380): [2019-09-23 10:28:42.349451] main run
Error -32601 received from application: Method not found
method not available: ext.flutter.platformOverride
Error -32601 received from application: Method not found
Error -32601 received from application: Method not found
method not available: ext.flutter.inspector.setPubRootDirectories
Error -32601 received from application: Method not found
method not available: ext.flutter.inspector.isWidgetCreationTracked
Error -32601 received from application: Method not found
method not available: ext.flutter.platformOverride
Error -32601 received from application: Method not found
Error -32601 received from application: Method not found
method not available: ext.flutter.inspector.setPubRootDirectories
Error -32601 received from application: Method not found
method not available: ext.flutter.inspector.isWidgetCreationTracked
Application finished.
Exited (sigterm)
```

## Flutter Doctor
```
[√] Flutter (Channel stable, v1.9.1+hotfix.2, on Microsoft Windows [Version 10.0.18362.356], locale en-US)
    • Flutter version 1.9.1+hotfix.2 at C:\app\dev\sdk\flutter
    • Framework revision 2d2a1ffec9 (2 weeks ago), 2019-09-06 18:39:49 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\app\dev\sdk\android-sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\app\editor\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\app\editor\android-studio
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.38.1)
    • VS Code at C:\Users\<user>\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.4.1

[√] Connected device (1 available)
    • Wileyfox Swift • b6d5f2fa • android-arm64 • Android 7.1.2 (API 25)

• No issues found!
```