Flutter plugin development Unresolved reference: io
Asked Answered
F

14

30

Its my first attempt to create a flutter plugin, I created flutter plugin project from android studio and its running fine, when I tried to add platform specific code for android I stuck in some issues, in the same project platform specific code editing is almost dead (no linting). I imported MyPluginProject/android in android studio and there are also some issues,

Unresolved reference: io

enter image description here

Here is output of flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.7.8+hotfix.2, on Linux, locale en_US.UTF-8)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
[✓] Android Studio (version 3.4)
[✓] Connected device (1 available)

• No issues found!
Flawed answered 10/7, 2019 at 7:45 Comment(1)
Latest Answer - After looking for various solutions like deleting .idea folder etc which did not work for me I stumbled upon this link FIX FOR THE ISSUE This solution I tried for Flutter Android pluginAllomerism
F
36

Solved the problem by following these steps from this issue on github.

  1. Delete the .idea folder in the project (do not know if is needed but just to be sure)
  2. In Android Studio click on Open an existing Android studio project and select the folder your_plugin/example/android (It's important to open your_plugin/example/android first and then your_plugin).
Flawed answered 10/7, 2019 at 15:45 Comment(5)
I don't think you need to delete the .idea folder. It works without it.Tyranny
Did not help me :(Sinapism
Actually it did help. I didn't realize I need to open two projects. First example/android and then library android.Sinapism
I think there is also a need of replacing this to FlutterEngine(this), as a solutionInglorious
Please not that you need to run the example app at least once using flutter run and then opening example app/android/build.gradle in android studio/intellijJ shows you your_plugin module as well as your example app moduleHydranth
I
12

In my case I had to:

  1. Open the Flutter Project on Android Studio
  2. Open the file located on android/src/main/kotlin/com/example/flutter_your_plugin/FlutterYourPlugin.kt
  3. Android Studio will suggest you to Open for Editing in Android Studio

PS: My Android Studio Version: Android Studio Arctic Fox | 2020.3.1 Patch 3 Android Studio Suggestion

Inglenook answered 29/1, 2022 at 2:42 Comment(0)
F
4

Shahzad Akram approach is almost correct:

It's important to open your_plugin/example/android first and then your_plugin.

In reality you never open your plugin, you open the example android project and there you can access your plugin, it is like opening an project that uses your plugin, then you can access your plugin with all dependencies.

If you open the plugin itself, AS will never load the Flutter jar dependency, neither androidx (sometimes it loads, but probably from cache).

This also applies for federated plugins! Or any other design that implies different Dart workspaces for the same package.


Full step

AS: Stands for Android Studio.

  1. Close all AS windows.
  2. Open AS initial window.
  3. Click to open project.
  4. Select your base Dart workspace (the one that contains the example folder).
    • If you are depeloping a federated plugin, click on the <plugin-name> folder, not <plugin-name>_platform_interface or <plugin-name>_android.
    • If you are depeloping a "normal" plugin, no secret, just open the root project.
  5. Then right click on your example folder -> Flutter -> Open Android module in Android Studio -> This window.
  6. Very important: select the sidebar "Android" view. NOT "Packages", "Projects", "Project Source Files", or anything else.
  7. The sidebar will show will two modules: app which is from your example/android project; and the <plugin-name> module, which is your actual plugin, use this to develop your plugin.

You may noticed that you will not be able to open your plugin directly with the intellisense features, you must open your plugin from the example project to be able to see the auto-completion for Android and Flutter java classes working.

Also: avoid using a custom gradle that will copy from the flutter.jar from the Flutter SDK files and copy into the standalone plugin module, it will crash when loading your plugin from an app because the classes will be duplicated (loaded from app gradle AND from your plugin gradle) -> This is not applicable if you know gradle enough to build a custom script for you to avoid this error.

Foret answered 18/2, 2023 at 1:27 Comment(1)
Worked for me :-)Guenzi
M
3

Shahzad Akram's answer didn't work for me but another tip from the given github link did:

Add the following to the module's build.gradle:

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

after the other apply declarations, and

flutter {
    source '..'
}

at the bottom.

Edit: It looks like the extra lines need to be commented out for the depending project to build; definitely not ideal

Meggy answered 1/9, 2020 at 13:16 Comment(0)
I
3

In my case, it's happen because I have 2 Kotlin extension on VS code, you should remove one.

enter image description here

Indeterminism answered 13/6, 2022 at 1:54 Comment(0)
A
2

Flutter Official Documentation Flutter Plugin

Follow the steps:

  • Open Android Studio
  • Check for option to open Existing Project button and press that
  • Now open your_plugin/example/android/build.gradle file and done

It should work fine 👍🏼

Armandarmanda answered 29/6, 2022 at 21:36 Comment(0)
P
1

If you develop a native plugin for flutter with IntelliJ, I propose the following approach given this structure:

├── my_plugin_root
    └── packages
        ├── my_plugin
        │   ├── example/android/
        │   └── lib/src/...
        └── my_plugin_android/android/src/main/...
  • open one of these paths in IntelliJ / AndroidStudio:
    • a) if test in example and with linting: my_plugin_root/packages/my_plugin/example/, then FileProject structureModules+Import module → navigate to my_plugin_root/packages/my_plugin/example/android/Import module from external modelGradleCreate
    • b) only with linting: my_plugin_root/packages/my_plugin/example/android/
  • open IntelliJ's file explorer aka Project tool window (Alt+1)
  • select view of the tool window and navigate to your implementation
    • a) Projectandroidandroid[my_plugin_android] → src → ...
    • b) Project Filesandroidmy_plugin_android → ...

The io.flutter dependencies should be resolved, once having made a gradle build.

See this comment on support for gradle plugin.

Pleasurable answered 15/8, 2022 at 13:30 Comment(0)
W
1
  1. Open the project in Android Studio, click File > Project Structure > Project
  • Check if the sdk is as <no_sdk>

  • If so, change it to "Android Platform api 31" or whatever platform your plugin supports.

  • click apply and then ok.

  1. just restart and kill the cache in your Android Studio (File > Invalidate Cache / Restart).

It fixed for me, I hope you too

Whizbang answered 18/12, 2022 at 12:57 Comment(0)
K
1

I did everything described here, the only thing that worked for me:

  1. After creating the project (plugin), open it in Visual Studio Code.
  2. Make sure that pub get is running.
  3. Right click on the example/android folder, open in Android Studio. enter image description here

and finally the modules of the plugin and the example are loaded. enter image description here

Pdta.- Android Studio Electric Eel | 2022.1.1 Patch 1

Kinna answered 7/3, 2023 at 16:6 Comment(0)
T
1

According to the documentation, you should open the plugin_test/example/android/build.gradle instead of plugin_test/android/build.gradle, it opens the example and the library

Troublemaker answered 4/10, 2023 at 0:36 Comment(0)
R
1

Lately the problem re-appeared and it seem to resolve around the Android Studio version, namely Hedgehog. So if someone is stumbling upon this looking for an answer, for me and others the way to fix this was to downgrade Android Studio to Giraffe, when everything else did not work. There are reports that jellyfish does also work, but I could not confirm that. See also here at GitHub

Rift answered 23/1 at 7:33 Comment(0)
W
0

What worked for me was running flutter create . --platforms=android in my_plugin/example before opening my_plugin/example/android/build.gradle in Android Studio

Watchful answered 19/1 at 0:35 Comment(0)
A
0

What worked for me was:

  1. Opening the my_plugin/example/android folder
  2. Right click on app -> Project Structure -> Dependencies
  3. Click on my module name and change the configuration value to debugImplementation.

The source code was the module was shown in the file structure and now I can work on my plugins using Android Studio features.

Argue answered 22/1 at 20:42 Comment(0)
E
0

Shahzad Akram's method works for me, the key is that you must first open the Flutter project, and then open the Android code from the right upper corner's Open for Editing in Android Studio option.
Never Never Never open the Android code by yourself from File - Open, otherwise you will encounter the problem you mentioned.

Exceeding answered 23/2 at 23:21 Comment(1)
This doesn't work on new Android Studio, the screen just blinks but it wont open. Android Studio IguanaDieterich

© 2022 - 2024 — McMap. All rights reserved.