I am developing a flutter web app in Anroid Studio.
When testing the app and run it from Android Studio, I would like to it to run in Chrome using my current user profile, as this user profile has all necessary passwords stored.
Whatever I try, when runnung the app from Android Studio, Anroid Studio would start a new Chrome instance with an empty Profile. This makes sense, as Android Studio starts a new virtual device which don’t have any information on user profiles stored.
This makes my developement-test cycles slow because I need to login with my google account credentials each time.
I learned the Anroid Studio Flutter plugin starts Flutter applications using the flutter run command from the Flutter SDK.
What I tried so far, is change the Settings in Anroid Studio: Settings > Tools > Web Browsers > Chrome
both to use a custom user data directory:
"%LOCALAPPDATA%GoogleChromeUser Data"
which I get from typing chrome://version/
in my current chrome browser.
and/or to use command line options:
--profile-directory=Default
This didn’t help, and also the JetBrain Support says,
Thanks in advance for your help.
3
Answers
You can run the app using
flutter run -d web-server
. It will start a local web server and provide you with a URL that you can open in any browser.First of all not the Android Studio is responsible for launching Chrome but a flutter tool.
This tool creates every time a
new Profile
into a temporary directory as a result all histories/cookies/passwords
/etc. are lost whenever a new debugsession is started.Here we can mess around with Chrome flags, unfortunately I couldn’t connect it to internal Chrome profiles (remote debugger couldn’t attach to the newly opened Chrome process) however I could connect it to a
minimalist Profile
which flutter tool uses as a skeleton when it is creates a new Profile.The trick is to copy this
minimalist Profile
to a new path (if possible avoid pathes which contains spaces) so it can be reused as many times as you want and shared between multiple flutter web projects.Go to
PROJECT_ROOT.dart_toolchrome-device
folder if.dart_tool
and/or
chrome-device
folder not exists then run you flutterproject with
Chrome (web) target
so.dart_toolchrome-device
will be generated
Inside
PROJECT_ROOT.dart_toolchrome-device
there should be a folder calledDefault
. Create a new Profile container on your system (mine is at:D:flutter_chrome_profiles
) and copy theDefault
folder (including the Default folder not just it’s content) to the newly created Profile containerThis part of answer is based on this answer.
Close
Android Studio
Go to
flutterbincache
and remove a file named:flutter_tools.stamp
Go to
flutterpackagesflutter_toolslibsrcweb
and open the filechrome.dart
Change this
'--user-data-dir=${userDataDir.path}',
to your new Profile container’s path (mine is:'--user-data-dir=D:\flutter_chrome_profiles',
)Now every time whenever you lauch any flutter web project your
histories/cookies/passwords/etc.
will be remained.If you have a lot of passwords then you should export them and import them into this newly created Profile, you can easily do this if this new Profile is opened by Android Studio.
I also needed to keep my Google authentication everytime I wanted to debug the app.
The way I did it was to set up persistency by doing the following:
I also had to make this change using
authStateChanges()
, as for some reason firebase user came as null:This was recently fixed by FlutterFire, so make sure all the dependencies are updated. I also removed all the scripts from index.html that were previously required.
So, this solves the "lose auth after refresh" problem. Then, every time you close and open Chrome (ie everytime you start debugging from scratch) a fresh new log-in was required. To solve this, you can force the code to run on the same web-port everytime, so it keeps local data in between sessions. Don’t forget the
-d chrome
as it wasn’t working previouslyflutter run -d chrome --web-port 7357
Since I didn’t want to run it from command, I’ve added a new Run/Debug configuration like in the image below.
Good luck!