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

I have (often) no auto completion in Android Studio when developing a plugin

Open rekire opened this issue 1 year ago • 16 comments

Steps to reproduce

  1. Create a new plugin like this: flutter create --org com.example --template=plugin --platforms=android -a kotlin hello
  2. Open the project
  3. Edit e.g. android/src/main/kotlin/com/example/hello/HelloPlugin.kt

Actual results

I have code high lighting (but also not always related bug), in that fresh project I cannot jump into the definition of the classes. There is no checking active so I can write code which won't compile without getting the normal linting or error hints.

In my real project I can fix that by opening a new Android Studio Window with the path example/android (in this sample project this even does not work on my computer). However when I open accidentally android directly the io.flutter dependencies cannot be resolved and "everything is red".

The bar "Open for editing in Android Studio" sometimes does not work (nothing happens) or it opens android instead of example/android.

Logs

Logs
Not relevant I guess otherwise I'll update this section

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.0, on macOS 14.4.1 23E224 darwin-arm64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Notes

This is a follow up to discord discussion. Also related is this issue: flutter/flutter#19830

rekire avatar May 21 '24 19:05 rekire

However when I open accidentally android directly the io.flutter dependencies cannot be resolved and "everything is red".

Is this the bug, or is the bug that something Flutter tool is opening android when it shouldn't? Not working unless you open the example is expected behavior due to the way plugins work.

stuartmorgan-g avatar May 21 '24 20:05 stuartmorgan-g

IMHO it should not matter which of those three path (/, /android or /example/android) are opened. Can you share some details why it matters? AFIK you can add modules on the fly in a plugin even if not why not adding code to detect this senario and add a conditional import or dependency to the example module and flutter dependencies.

The ultimate question is why a mixed mode (full dart and Kotlin support) is not possible. If you know a related issue feel free to link it.

rekire avatar May 22 '24 05:05 rekire

Thanks for the report @rekire

I have code high lighting (but also not always related bug), in that fresh project I cannot jump into the definition of the classes.

I seem to get below upon following the details:

Screenshot 2024-05-22 at 1 13 52 PM Screenshot 2024-05-22 at 1 13 59 PM

The bar "Open for editing in Android Studio" sometimes does not work (nothing happens) or it opens android instead of example/android.

This seem to worked at my end when it opened and did its thing to sync the gradle changes and open up example:

Screenshot 2024-05-22 at 1 17 45 PM

why a mixed mode (full dart and Kotlin support) is not possible

By this do you mean Gradle Kotlin DSL support ?

darshankawar avatar May 22 '24 07:05 darshankawar

With mixed mode I mean that I can edit in the same AS window dart and the Kotlin code with full auto completion and open class defintion, since the IDE has the capabilities to support both languages. Just code highlighting works (almost) every time.

Regarding the second screen on my try I was not able to open the class definition by holding Ctrl (or Command on MacOS) and clicking on the class name or using middle click (of cause just for Windows and MacOS).

rekire avatar May 22 '24 08:05 rekire

IMHO it should not matter which of those three path (/, /android or /example/android) are opened. Can you share some details why it matters?

Because the Flutter dependency is injected into native projects by flutter build, and flutter build only operates in the context of an application.

why not adding code to detect this senario

I'm not sure what you are proposing adding code to.

stuartmorgan-g avatar May 22 '24 09:05 stuartmorgan-g

why not adding code to detect this senario

I'm not sure what you are proposing adding code to.

If I remember correctly then the flutter dependencies are injected with the plugin 'dev.flutter.flutter-gradle-plugin' which is set in /example/android/app/build.gradle. I would like to add the same plugin to /android/build.gradle which detects the native flutter dependencies and injects the same packages as in the example/app/build.gradle. If this already works (didn't check it yet) why is that not the default?

rekire avatar May 22 '24 10:05 rekire

Keeping the issue open for team's tracking.

darshankawar avatar May 23 '24 05:05 darshankawar

I updated my IDE now to:

Android Studio Jellyfish | 2023.3.1 Patch 1 Build #AI-233.14808.21.2331.11842104, built on May 15, 2024 Runtime version: 17.0.10+0-17.0.10b1087.21-11572160 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 14.5 GC: G1 Young Generation, G1 Old Generation Memory: 4096M Cores: 10 Metal Rendering is ON Registry: ide.experimental.ui=true Non-Bundled Plugins: Dart (233.15271) io.flutter (79.0.2)

That reset all my settings. Then I click for a code file like android/src/main/kotlin/com/example/Example.kt on Open in Android Studio a window pops up and closes directly. (I am aware that I need to open the example app)

Here are the logs:

2024-05-28 08:25:06,838 [ 767968]   INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files
2024-05-28 08:25:12,184 [ 773314]   INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files
2024-05-28 08:25:51,952 [ 813082]   INFO - #c.i.j.JdkEx - === TabbingMode: on ===
2024-05-28 08:25:51,960 [ 813090]   INFO - #c.i.w.i.i.l.m.ModuleBridgeLoaderService - Workspace model loaded without cache. Loading real project state into workspace model. Thread[DefaultDispatcher-worker-14,5,main]
2024-05-28 08:25:51,961 [ 813091]   INFO - #c.i.w.i.i.GlobalWorkspaceModel - Sync global entities with mutable entity storage
2024-05-28 08:25:51,995 [ 813125]   INFO - #c.i.w.i.i.EntitiesOrphanageImpl - Update orphanage. 0 modules added
2024-05-28 08:25:51,996 [ 813126]   INFO - #c.i.w.i.i.WorkspaceModelImpl - Project model updated to version 1 in 2 ms: Apply JPS storage (iml files)
2024-05-28 08:25:51,996 [ 813126]   INFO - #c.i.w.i.i.EntitiesOrphanageImpl - Update orphanage. 0 modules added
2024-05-28 08:25:51,999 [ 813129]   INFO - #c.i.p.c.ProjectCodeStyleSettingsManager - Initialized from default code style settings.
2024-05-28 08:25:51,999 [ 813129]   INFO - #com.android.tools.idea.projectsystem.ProjectSystemService - GradleProjectSystem project system has been detected
2024-05-28 08:25:52,007 [ 813137]   INFO - #c.i.c.b.CompilerReferenceServiceBase - backward reference index reader is closed (didn't exist)
2024-05-28 08:25:52,007 [ 813137]   INFO - #o.j.k.i.s.r.KotlinCompilerReferenceIndexStorage - KCRI storage is closed (didn't exist)
2024-05-28 08:25:52,332 [ 813462]   INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files
2024-05-28 08:25:54,127 [ 815257]   INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files

rekire avatar May 28 '24 07:05 rekire

Is this bug actually a feature request that plugins have syntax highlighting when opened independently of an application or something else?

reidbaker avatar May 28 '24 14:05 reidbaker

This is one aspect of the problem. For my workflows it is important that I can edit my dart and flutter code in a single IDE instance. In Android Studio I see sometimes no syntax errors and sometimes everything is red for unresolved dependencies. I cannot work productive that way. My workaround is as pointed out that I open the project from "example/android".

When I use the play button in the Android project mostly the project cannot been build of cause I need to switch the window again. If there would be just one window this would be no issue.

It even become worse when you depend on multiple own packages which you depend on for development as path dependency (via pubspec_overrides.yaml and dependency_overrides:. Then I have to handle often 3 Android Studio instances for a single project: The flutter app which actually used my package, the package (which contains the dart code) and the native Android code.

rekire avatar May 28 '24 20:05 rekire

Temporarily assigning myself to better understand this and triage on behalf of the Tools team.

andrewkolos avatar May 28 '24 21:05 andrewkolos

For my workflows it is important that I can edit my dart and flutter code in a single IDE instance.

@jwren am I right to assume that editing Dart & android native code in the same Android Studio session and getting intellisense support for both languages is not officially supported.

christopherfujino avatar May 28 '24 23:05 christopherfujino

@christopherfujino thanks for tagging me here. This has to do with Module configurations in the IJ platform that AS sits on top of.

There was/is a recent AS regression for us with the AS surfaced APIs that will make this worse, see https://github.com/flutter/flutter-intellij/issues/7103 and the removal of our usage of the API here, https://github.com/flutter/flutter-intellij/commit/dd5a521382bf20f6cfb1121851f38c398fc9dbaa.

@andrewkolos thanks for taking a look.

jwren avatar May 29 '24 19:05 jwren

This seems to be a unfunny connection of multiple issues. So the open in Android Studio feature was dropped from Android Studio right? Strange that it still works sometimes but for this part are different issues.

What is about to support Kotlin and Dart or Flutter modules at once? This is currently not possible as I have to notice painfully. But is that something which can be done in general?

rekire avatar May 29 '24 20:05 rekire

[At a high level], with IntelliJ & Android Studio the answer is that yes, we should be able to do this. Certain sub-directories can be marked as a module of some other language, i.e. the top level directory might be a Dart module, and sub directories could be marked as Java or Android modules.

jwren avatar May 29 '24 20:05 jwren

Might be I found my personal Holy Grail for mixed mode development with Dart and Android code in a single Android Studio instance. I just need to create in the project root a settings.gradle and reference there my android modules.

<project-root>/settings.gradle:

pluginManagement {
  def flutterSdkPath = {
    def properties = new Properties()
    file("example/android/local.properties").withInputStream { properties.load(it) }
    def flutterSdkPath = properties.getProperty("flutter.sdk")
    assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
    return flutterSdkPath
  }
  settings.ext.flutterSdkPath = flutterSdkPath()

  includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

  repositories {
    google()
    mavenCentral()
    gradlePluginPortal()
  }
}

plugins {
  id "dev.flutter.flutter-plugin-loader" version "1.0.0"
  id "com.android.application" version '8.5.0' apply false
  id "org.jetbrains.kotlin.android" version "2.0.0" apply false
}

include ':android'
include ':example'
project(':android').projectDir = new File(rootDir, 'android/')
project(':example').projectDir = new File(rootDir, 'example/android/app/')

The second step is just to copy from the <project-root>/example/android/local.properties to the project root. After a gradle sync everything the java and kotlin code was possible to use with auto completion even for the pubspec dependencies. I found in a repo a gradle script which also makes it possible to resolve the engine dependencies (of cause I adapted it a bit since it does too much IMHO). With those two parts I can now develop easily in one IDE dart and native (Kotlin/Java) code.

rekire avatar Jul 25 '24 08:07 rekire

The correct solution (not only a workaround) is to open the example rather than the plugin project so that dependency relationship can be built, similar to opening the Runner.xcworkspace when developing a Darwin plugin.

AlexV525 avatar Mar 02 '25 13:03 AlexV525

IMHO this is a wrong decision. When you open the same Kotlin files in two Android Studio instances the Kotlin cache become corrupted and you need to "repair" the IDE. You end up in the same problem that the auto complication brakes.

Why is not my suggested way considered as a possible idea to fix this issue? I have not the time and knowledge to fix the issue myself.

rekire avatar Mar 02 '25 22:03 rekire

I just want to mention that this will be fixed by me via https://github.com/flutter/flutter/pull/167332

rekire avatar May 30 '25 05:05 rekire