1

In Android Studio after ending the debug/run session and/or unplugging my phone from the computer and restarting the app, the locally installed app seems to not have saved the last hot reload's changes.

I noticed this a few times and decided to run an experiment by having a simple page and introducing a couple simple Text widgets in separate hot reloads (triggered by my pressing Ctrl+S), and indeed after restarting the app (after having unplugged) only the first of the two Text widgets appeared, indicating that indeed the last hot reload's changes have been lost.

I'm not sure if this is by design or if something in my development environment is not working.

Either way, I am hoping to be able to easily unplug and have the last changes persist so that when I am discussing my latest changes with other developers away from my computer, we can look at the latest changes.

P.S. I am aware that I can just introduce a dummy change at the end of each development session and unplug so that all the actual changes stick around, but I'm wondering if there is another more elegant solution than adding garbage code.

KGEM
  • 79
  • 8
  • Changes from a hot restart/reload persist only as long as the application doesn't close/restart. When you restart the application on the phone (not run via Android Studio), it opens up the last version that was installed. If you want to make sure you keep your changes, then you need a new install of the application. Uninstall the current version on your phone, then run the application again in Android Studio with your updates. – Leviticus Dec 06 '21 at 18:46

2 Answers2

1

Hot reload works by uploading your latest changes into a debuggable process. The source code is compiled into kernel files and sent to the mobile device’s Dart VM. But they are loaded in-memory. When you disconnect the debug process and kill your app, after you open again it will load the libraries from the last real compilation which is in your disk.

Applying changes and saving them both in memory and then disk would actually make the whole process a lot less efficient and is currently unavailable, unfortunately.

If you want to run your application without recompiling it, you can:

  1. Connect your device
  2. Open the old installed version of your app
  3. On the IDE you click on the little bug icon with an arrow (See the screenshot) (or pressing shift twice and then typing "Attach Debugger to Android Process")
  4. Click OK

Please note: this approach won't work properly if you have native code changed, for instance installing a third party library that adds functionality through platform-channels would require you to recompile the application.

Image pointing to the debug icon with an arrow

Hope this helps. There's also the same thing on VSCode.

See more at: https://docs.flutter.dev/development/tools/hot-reload

  • I'm not sure if the four steps you described would address my goal of unplugging and having all the latest changes persist (will try it soon, I just mean I didn't understand you completely!). Please clarify if that is the case. – KGEM Dec 07 '21 at 10:31
  • Otherwise, how can I compile to disk fully before unplugging? I'm happy to run a full compile prior to ending work for the day for example. I just couldn't find such a button frankly. The closest thing I could find is creating an APK and transferring it over and installing manually from the phone which seems more arduous than need be. – KGEM Dec 07 '21 at 10:32
  • 1
    The behavior you are trying to achieve is impossible. I emphasized that if you are worrying about speeding up your development process, you could do the steps above, which will allow you to apply the local changes again to an old apk file. But only in memory too. If you want to have the latest version of the app on your device, all you have to do is compile again. – Anderson Freitas Dec 07 '21 at 11:45
  • So it seems like my best bet is to use the dummy change approach prior to unplugging in terms of speed. Otherwise if it's not too much trouble, can you elaborate on where the compile button is? I couldn't find it (other than the one which creates an APK that I then have to manually move to the device). Thanks again and if you can edit your answer to clarify that my goal is impossible it would be great. Either way I'll accept your answer! – KGEM Dec 07 '21 at 12:23
  • Sure, i'll sort this out as soon as I can – Anderson Freitas Dec 08 '21 at 13:47
  • 1
    And I must apologize, reading it again now, it really seems a bit confusing, but I wrote that in a sort of a rush so... As I said, I'll adjust ASAP – Anderson Freitas Dec 08 '21 at 13:48
0

try that ;

  1. run the app
  2. insert flutter install on the console

Tell me if that work for you