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

feat: respect embedded emulator settings

Open pedromassango opened this issue 3 years ago • 6 comments
trafficstars

Fix #5917

pedromassango avatar Aug 10 '22 18:08 pedromassango

@stevemessick the flutter-idea project has no acess to EmulatorSettings even though I have code completion. Running the plugin works but you try to launch the emulator you will get a crash sayng "EmulatorSettings not found":

Caused by: java.lang.ClassNotFoundException: com.android.tools.idea.emulator.EmulatorSettings PluginClassLoader(plugin=PluginDescriptor(name=Flutter, id=io.flutter, descriptorPath=plugin.xml, path=~/dev/projects/flutter-intellij/flutter-idea/build/idea-sandbox/plugins/flutter-idea, version=70, package=null, isBundled=false), packagePrefix=null, instanceId=100, state=active)

I'm struggling to find the correct dependency for that class

pedromassango avatar Aug 10 '22 18:08 pedromassango

@pedromassango Add this to line 55 of flutter-idea/build.gradle.kts: "android",

I'm currently reviewing this PR. After that change, it may be ready for review, but I'm having some trouble launching the IDE.

stevemessick avatar Aug 10 '22 18:08 stevemessick

Got the same result: class not found

pedromassango avatar Aug 10 '22 19:08 pedromassango

Yeah, the change I suggested does nothing. The Android plugin is already a dependency. Your code does work when I build a binary plugin and install it. I'm having trouble getting it to work in the debugger, though. I verified that EmulatorSettings is in the Android plugin in lib/android.jar, and the plugin list include org.jetbrains.android, which is its plugin-id in the plugin.xml file. I don't understand why the class is not found when running under the debugger.

One thing I noticed is that the tool window starts collapsed. We will need some code to make it visible. See FlutterPerformanceView.activateToolWindow() for an example of doing that.

stevemessick avatar Aug 10 '22 23:08 stevemessick

Could it be that the plugin is not loaded/stripped in Debug mode?

pedromassango avatar Aug 11 '22 12:08 pedromassango

No, I checked the plugins page. It is listed as "bundled".

stevemessick avatar Aug 11 '22 16:08 stevemessick

We will need some code to make it visible. See FlutterPerformanceView.activateToolWindow() for an example of doing that.

I was able to open the tool window, however it closes itself right after it opens. Probably some internal code is closing it.

pedromassango avatar Aug 12 '22 17:08 pedromassango

I was able to open the tool window, however it closes itself right after it opens. Probably some internal code is closing it.

It could be a timing issue. I'd make sure it was opened after everything else had been done, and possibly wrap that code in a call to Application.invokeLater(). Sorry but I didn't have a chance to look into the class loading problem today.

stevemessick avatar Aug 12 '22 23:08 stevemessick

It could be a timing issue.

Hmm, I'm getting the same behavior after implementing invokeLater. I will test again by generating a binary of the plugin

pedromassango avatar Aug 13 '22 10:08 pedromassango

It looks like your code is missing a few import statements. Does the behavior change after the code is fixed?

stevemessick avatar Aug 15 '22 18:08 stevemessick

Does the behavior change after the code is fixed?

I get the same behavior. I was unable to test it as a stand-alone plugin, faced some challenges generating the plugin.

pedromassango avatar Aug 15 '22 18:08 pedromassango

Are you no longer getting this error when running in the debugger?

java.lang.NoClassDefFoundError: com/android/tools/idea/emulator/EmulatorSettings

I still do. Very puzzling.

I patched in your changes and built the plugin. I only checked AS 2021.1 but it worked fine. The tool window opened, and after a short delay, the emulator was visible. I'm using macos. What platform are you testing on?

faced some challenges generating the plugin

Is that related to downloads? Once everything is downloaded it will be re-used, but it uses a lot of bandwidth initially. The GitHub actions build the plugin for each supported platform, so it should "just work".

stevemessick avatar Aug 15 '22 23:08 stevemessick

Are you no longer getting this error when running in the debugger?

I'm still getting it as well

What platform are you testing on?

Mac OS - m1 with the latest intellij IDEA. This is crazy 🐒

Is that related to downloads? Once everything is downloaded it will be re-used, but it uses a lot of bandwidth initially. The GitHub actions build the plugin for each supported platform, so it should "just work".

Maybe I'm missing something here. How are you generating a new build? I tried to build it mannually following this Jetbrains tutorial.

pedromassango avatar Aug 16 '22 16:08 pedromassango

bin/plugin make

stevemessick avatar Aug 16 '22 17:08 stevemessick

bin/plugin make

Thanks. Got the same error after installing the generated plugin from flutter-intelliJ/releases/release_master/2022.2/

pedromassango avatar Aug 16 '22 20:08 pedromassango

I can reproduce that problem. I also tried another IntelliJ version, and got the same result. Now, I think I understand what the problem is. We have a conditional dependency in the Android Studio version on the Android plugin. We don't have that for IntelliJ.

Try adding this to plugin_template.xml: <depends>org.jetbrains.android</depends>

Then run bin/plugin generate and bin/plugin make (or just launch the debugger).

stevemessick avatar Aug 16 '22 21:08 stevemessick

Adding that dependency works. I can run the IDE in the debugger, launch an emulator, and the emulator tool window appears but is immediately closed. After opening it, the embedded emulator is running. I tried moving the call to openEmulatorToolWindow() into the ProcessAdaptor but that didn't help. It looks like IntelliJ is closing the window when the emulator tries to use it. You might try adding a delay, but that can be tricky, depending on how fast/slow a user's compute is (and yours is fast).

Everything is fine for Android Studio. The tool window stays open and shows the emulator after it is initialized. I don't have time to investigate the difference. If you want to, I suggest looking into the jetbrains/intellij-community repo sources. If you don't have time, then I think it would be appropriate to check that the user is not running Android Studio (FlutterUtils.isAndroidStudio()) then showing a blue ballon with some text explaining that the tool window has to be re-opened. The tool window will be in the side-bar.

Launching with the debugger can be flakey. I've seen a number of weird issues. Once, it tried to load Android Studio-specific classes. Sometimes it cannot find classes in the Dart plugin. Once the window never opened. And, always the NST library cannot be loaded (ignore that one). If weird things happen, just kill it and launch again.

This is looking good! Thanks for driving it.

stevemessick avatar Aug 16 '22 22:08 stevemessick

Yes, the class-not-found issue is now fixed, thanks ✅ but: I had to change /resources/META-INF/plugin.xml for it to work.

It looks like IntelliJ is closing the window when the emulator tries to use it.

I was able to make it to work as follow: the emulator starts, the tool window opens and closes itself, after a short period of time (~2sec) the tool windows opens itself again and the emulator is visible and ready. Would that be ok to merge this? not for me (I'm still not happy with that, but open for your PoV).

I will look into it again and see what I can do. otherwise we can go with the ballon approach.

pedromassango avatar Aug 20 '22 12:08 pedromassango

I think your current behavior is fine since this only happens in IntelliJ and 90%+ of our users use Android Studio. One person suggested adding a one-time balloon pointing out the open-close-open behavior does not happen with Android Studio. I'll leave that up to you. If you'll commit your latest and change it to ready for review I'll merge it. It would be nice to have this in the next release, which is Sept 1. Again, thanks!

stevemessick avatar Aug 22 '22 17:08 stevemessick

Alright. since I'm still not able to fix the issue, it is fair to merge it like that and prove it on another PR. I will commit the changes asap.

pedromassango avatar Aug 24 '22 19:08 pedromassango

Do you need me to commit this?

I can merge it. Do I need to what for the kokoro-mac task to be completed?

pedromassango avatar Aug 24 '22 23:08 pedromassango

No need to wait. It will fail.

stevemessick avatar Aug 24 '22 23:08 stevemessick

@pedromassango Oops! I forgot to ask you to add your "name" to our AUTHORS file. Could you create a new PR to do that? It's nice to get credit for one's work, but if you don't want to, it isn't required.

stevemessick avatar Aug 29 '22 22:08 stevemessick

Oh, nice. Here is the PR

pedromassango avatar Aug 30 '22 17:08 pedromassango

Nope! It is going to fail anyway.

On Wed, Aug 24, 2022 at 4:40 PM Pedro Massango @.***> wrote:

Do you need me to commit this?

I can merge it. Do I need to what for the kokoro-mac task to be completed?

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

stevemessick avatar Oct 11 '22 08:10 stevemessick