27

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!
Shahzad Akram
  • 4,586
  • 6
  • 32
  • 65

9 Answers9

32

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).
Shahzad Akram
  • 4,586
  • 6
  • 32
  • 65
7

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

M. Massula
  • 3,730
  • 2
  • 11
  • 14
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

wamfous
  • 1,849
  • 1
  • 12
  • 11
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

1

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

enter image description here

rvng
  • 156
  • 1
  • 7
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.

User Rebo
  • 3,056
  • 25
  • 30
1

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.

Alex Rintt
  • 1,618
  • 1
  • 11
  • 18
0
  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

RusArtM
  • 1,116
  • 3
  • 15
  • 22
0

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