Do flutter plugins for desktop have to be written in platform specific languages (like windows we use C++ and mac we use Swift)

1,195

Solution 1

It does not seem possible to use Java code in macos.

In order to add the support for instance for Mac OSX you should create a directory called macos and write your plugin in swift for that case.

You can take a look here for more information about this: https://flutter.dev/desktop#plugin-support

(29/02/2020) Window & Linux support are currently in technical preview

Solution 2

There are likely two reasons the specific thing you want to do won't work:

  • Flutter applications on the desktop platforms don't have a Java VM, so you can't use Java.
  • Flutter plugins usually exist to call platform-specific functionality (or third-party SDKs that are written for a specific platform, but the effect is the same). If you consider a plugin like camera for instance, even if you ignore the language problem, you can't call Android camera APIs on macOS or Windows, because they aren't Android; they have completely different OS camera APIs. (There are exceptions like sqflite that wrap an already-cross-platform library, but that's not the norm).

However, for your general question of whether you have to use the specific languages described in the documentation, the answer is technically no:

  • On Windows and Linux, plugins are (currently) just shared libraries with a specific C interface. You could in theory write the whole plugin in any language that can compile to a C-compatible shared library, such as Go or Rust (although you would have to do your own method channel encoding/decoding). Alternately you could use the C++ API as a thin glue layer to handle the platform channel, and call into underlying code compiled to a C-compatible form.
  • On macOS a plugin is a module, so the outer layer must be Swift or Objective-C. However, Objective-C is C compatible, so the same option of having only the method channel glue in Objective-C and the rest of the code in anything with a C interface applies to macOS as well.

However, unless you are dealing with an existing cross-platform library, that's likely not a good idea. If you are writing new code that's not platform-specific for use in Flutter you're almost always going to be better off just writing it in Dart as a regular package, where it will work on all current and future Flutter platforms.

Share:
1,195
Black Cupid
Author by

Black Cupid

Updated on December 17, 2022

Comments

  • Black Cupid
    Black Cupid over 1 year

    Good day people.

    I'm new to flutter and dart so as much help will be appreciated

    I have a plug in for flutter that's currently written in Java in the android directory. I want the same plugin to be able to run on other platforms like windows and mac. Do I have to rewrite this plugin in C++ for windows and Swift for macos or can I use the java files in those directories.

    If I can use the same Java files can someone please let me know how to move these files across to the the other directories and what I need to do for my flutter app to pick up these plugins for the other platforms.

    • IInspectable
      IInspectable over 4 years
      Neither C++ nor Swift are platform-specific.
  • Black Cupid
    Black Cupid over 4 years
    so there's no way just to move the Java files into the macos directory
  • Stefano Saitta
    Stefano Saitta over 4 years
    Is not mention anywhere in the official documentation, so I expect that Flutter Engine will not compile Java code inside the macos directory.
  • Stefano Saitta
    Stefano Saitta over 4 years
    probably something you can do if you want something to be used from everywhere is this ffi flutter.dev/docs/development/platform-integration/c-interop
  • Black Cupid
    Black Cupid over 4 years
    that was gonna be my next option if I could move the java files over