flutter-intellij icon indicating copy to clipboard operation
flutter-intellij copied to clipboard

Native Android vs. Flutter-Android Emulator Launch Inconsistency

Open salmankhilji opened this issue 3 years ago • 12 comments

There is an inconsistency between a native Android project vs a Flutter-Android project in how the emulator is launched from within Android Studio.

  • In a Native Android app, clicking the Run button first launches the emulator and then starts the app.

  • In a Flutter-Android app, the user must manually launch the emulator and then start the app (because automatic launching of the emulator fails for some reason.)

Native Android Project

0

After clicking on the Run button, which is to the right of the device dropdown, Android Studio automatically launches the emulator and then starts the app. So far so good.

Flutter-Android Project

1

Notice how the device dropdown to the left of the Run button is disabled. Moreover, we have additional dropdown to the left showing <no devices>. Inconsistency No. 1: why have a dropdown that does not work?

After clicking on the Run button, I get:

3

Not sure why a device was not found as it shows up in the new dropdown, like this:

4

Inconsistency No. 2: This new dropdown does not respect the Launch in a tool window option.

So, as a workaround, I must use the Device Manager to launch the emulator as follows:

5

After waiting a few more seconds, the launched emulator finally shows up within the toolbar, replacing the <no devices>, like this:

6

Finally, I can click on the Run button and start the Flutter-Android app.

Expected Behavior

  • Clicking on the Run button within a Flutter-Android app shall first launch the emulator and the start the app, just like it does within a Native Android project.

  • We should not have the additional dropdown as it confuses a developer coming from the native Android land.

  • If we we want to keep this new dropdown, at least, it should respect the Launch in a tool window option.

Version info

/home/precor/.local/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel unknown, 2.8.2-0.0.pre.1, on Ubuntu 20.04.3 LTS 5.13.0-27-generic, locale en_US.UTF-8)
    • Flutter version 2.8.2-0.0.pre.1 at /home/precor/.local/flutter
    • Upstream repository unknown
    • Framework revision 91318852b3 (13 hours ago), 2022-01-27 06:42:52 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /home/precor/Android/Sdk
    • Platform android-31, build-tools 30.0.3
    • ANDROID_SDK_ROOT = /home/precor/Android/Sdk
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Android Studio (version 2021.1)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 63.2.2
    • Dart plugin version 211.7798
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] VS Code (version 1.63.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.32.0

[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
Process finished with exit code 0

Note: I have Flutter SDK 2.8.1 installed with minor mods to the flutter create -t app template. Here is my patch: flutter.zip

$ flutter emulators
1 available emulator:

Pixel_3a_XL_API_30 • Pixel 3a XL API 30 • Google • android

To run an emulator, run 'flutter emulators --launch <emulator id>'.
To create a new emulator, run 'flutter emulators --create [--name xyz]'.

You can find more information on managing emulators at the links below:
  https://developer.android.com/studio/run/managing-avds
  https://developer.android.com/studio/command-line/avdmanager
Android Studio Bumblebee | 2021.1.1
Build #AI-211.7628.21.2111.8092744, built on January 18, 2022
Runtime version: 11.0.11+0-b60-7590822 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.13.0-27-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: Dart (211.7798), org.jetbrains.kotlin (211-1.6.10-release-923-AS7442.40), io.flutter (63.2.2), org.intellij.plugins.markdown (211.7142.37)
Current Desktop: ubuntu:GNOME

salmankhilji avatar Jan 28 '22 04:01 salmankhilji

I just wanted to add a few notes on why things are the way they are.

Flutter cannot use the standard device selector because it does not support any platforms other than Android. Flutter also has iOS, web, MacOS, Linux, and Windows.

We tried to hide it but couldn't find a way to do so. IIRC a project that does not have an Android module does hide it, but then you cannot use some Android tools, like the Device Manager. If anyone knows how to hide it I would be glad to review a PR. :)

When we created the device selector we didn't think about auto-launching an emulator. At that time, Android Studio 3.0 beta did not have a device selector. They did a better job with theirs and we have not had time to go back and catch up.

The embedded emulator probably has a better chance of getting implemented than auto-launch. We'll consider it in a future planning session. We'd probably need more up-votes, though, so click that thumbs-up in the OP.

Technically, #5910 is a dup of this issue. Usually we keep the oldest issue open, but this one is asking for more work to be done (and probably should be split into two issues, but let's not bother).

stevemessick avatar Feb 09 '22 18:02 stevemessick

The embedded emulator probably has a better chance of getting implemented than auto-launch.

I agree that auto launch should not be implemented. Flutter supports multiple platforms, which one would be the default? The answer is different for each one of us.

pedromassango avatar Aug 05 '22 20:08 pedromassango

@stevemessick I decided to give it a go, and managed to make it to work. Here is a preview of the Flutter IntelliJ plugin fork starting the emulator as a embedded Android Emulator

https://user-images.githubusercontent.com/33294549/183224618-8bea105b-b773-449e-abac-87aced639001.mp4

pedromassango avatar Aug 05 '22 23:08 pedromassango

That's great! I'll be happy to review a PR if you want.

On Fri, Aug 5, 2022 at 4:55 PM Pedro Massango @.***> wrote:

@stevemessick https://github.com/stevemessick I decided to give it a go, and managed to make it to work. Here is a preview of the Flutter IntelliJ plugin fork starting the emulator as a embedded Android Emulator

https://user-images.githubusercontent.com/33294549/183224618-8bea105b-b773-449e-abac-87aced639001.mp4

— Reply to this email directly, view it on GitHub https://github.com/flutter/flutter-intellij/issues/5917#issuecomment-1207093736, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACA7VDKZLZDKGP3AJDQP4QTVXWSWBANCNFSM5M7ZEPWQ . You are receiving this because you were mentioned.Message ID: @.***>

stevemessick avatar Aug 06 '22 15:08 stevemessick

That's great! I'll be happy to review a PR if you want.

I haven't figured out how to access Android plugin's settings to check whether the emulator has to be embedded or not. Do you have any idea how to do that?

pedromassango avatar Aug 06 '22 19:08 pedromassango

I don't, and I had to delete my copy of the Android Studio sources so I'm not sure when I'll be able to look. If you are interested in downloading the source, here's the instructions. Note that it may take hours; just let it run overnight, if you can. https://android.googlesource.com/platform/tools/base/+/studio-master-dev/source.md

If that sounds like too much trouble then we might be able to add another setting to the Flutter settings page to control it. What do you think, @jacob314 ?

stevemessick avatar Aug 08 '22 20:08 stevemessick

I don't, and I had to delete my copy of the Android Studio sources so I'm not sure when I'll be able to look. If you are interested in downloading the source, here's the instructions. Note that it may take hours; just let it run overnight, if you can. https://android.googlesource.com/platform/tools/base/+/studio-master-dev/source.md

If that sounds like too much trouble then we might be able to add another setting to the Flutter settings page to control it. What do you think, @jacob314 ?

I spent few hours and was able to have a good understanding about how the android plugins is handling it, however I was not able to find a way to call the Android plugin code through the Flutter plugin to check when the embedded checkbox is checked.

There maybe be a way of accessing it: setting the android plugin as dependency. But this is not ideal since not everyone uses the Android plugin.

So far, I think the best solution would be to write a custom option in the Flutter settings page. I would be happy to come up with a PR for this

pedromassango avatar Aug 08 '22 20:08 pedromassango

The Android plugin already is a dependency for code in the flutter-studio tree. That tree defines a module that contains all the Android Studio specific code, and that module is only loaded when the IDE is Android Studio (i.e. not IntelliJ).

If you create an IntelliJ Platform Plugin SDK based on Android Studio (instead of IntelliJ) then it will have all the Android Studio modules available. It wouldn't have source code, though. I have unable to build a source-based Android Studio IDE since Kotlin 1.6 came out. (https://youtrack.jetbrains.com/issue/KTIJ-22141/Kotlin-internal-error-during-IR-lowering) You'd need to load flutter-studio as a Gradle module into the project. I think everything is set up to do that already, since that's how I've been working for months. You'd probably also need to add dependencies in the Gradle build script to make more Android modules visible to the flutter-studio module.

Let me know if that helps. We might be able to do a Flutter option but it would need to be visible only when Android Studio was in use.

stevemessick avatar Aug 08 '22 22:08 stevemessick

Wait a sec, so the idea is to "only run in embedded mode on Android Studio"? I thought this issue is about the IntelliJ plugin for IntelliJ IDEA (not Android Studio), dam... I was looking at the wrong direction then.

@stevemessick that said, the fix lies in the flutter-studio directory?!

I will review the code again and maybe put a PR so you can have an idea in what I'm doing

pedromassango avatar Aug 09 '22 22:08 pedromassango

Yes, I don't think embedded works for IntelliJ. It was something Android Studio added about a year ago, I think.

And yes, new code for this would go into flutter-studio.

I'll be happy to look at a PR, even if is WIP.

stevemessick avatar Aug 09 '22 22:08 stevemessick

Yes, I don't think embedded works for IntelliJ. It was something Android Studio added about a year ago, I think.

It works, the video I shared above is from the IntelliJ IDEA Community (running the flutter-intellij/flutter-idea project).

pedromassango avatar Aug 09 '22 22:08 pedromassango

Ha! I didn't notice. That's great, it means we don't need to worry much about Android Studio.

In that case, you probably just need to add additional dependencies to the build.gradle.kts in flutter-idea in order to get access to the setting that controls whether the emulator is embedded or not. And, the sources are available on github: https://github.com/JetBrains/intellij-community

On Tue, Aug 9, 2022 at 3:51 PM Pedro Massango @.***> wrote:

Yes, I don't think embedded works for IntelliJ. It was something Android Studio added about a year ago, I think.

It works, the video I shared above https://github.com/flutter/flutter-intellij/issues/5917#issuecomment-1207093736 is from the IntelliJ IDEA Community (running the flutter-intellij/flutter-idea project).

— Reply to this email directly, view it on GitHub https://github.com/flutter/flutter-intellij/issues/5917#issuecomment-1209969103, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACA7VDINZEDBATDHKKC4XBLVYLOGPANCNFSM5M7ZEPWQ . You are receiving this because you were mentioned.Message ID: @.***>

stevemessick avatar Aug 09 '22 23:08 stevemessick