Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use c++ code in flutter (android) application?

I am developing an application in Flutter (for Android) whose core is based on a library developed only in C++ and python. Therefore, I would like to know how I can use this library in my dart code (either the C++ version or the Python version).

like image 817
Endy Bermúdez R Avatar asked Mar 01 '19 21:03

Endy Bermúdez R


1 Answers

Additional information (2021-05-05)

From @FacundoFarall comment to the original question:

Try this video by Richard Heap, it helped me quiet a lot. He explains with an example how to interact between the Dart code of your Flutter app, and the C++ library you're using, all through dart:ffi. I'd also recommend checking out this post for a way to debug the C++ code while running it through a Flutter app.

Update (2019-09-10)

Today has been released Dart 2.5 (and Flutter 1.9). One of the additions to Dart 2.5 is "the dart:ffi foreign function interface for calling C code directly from Dart." I still haven't checked it, but it should provide a more direct path to interface with C (and C++) code.

See also the announcement video (confirms both old and new methods).

Old answer (still valid)

I would take a look at Platform Channels, which are a way to communicate Flutter code to platform native code (e.g. Java/Kotlin on Android, Objective C/Swift in iOS). From there, you can use your C++ library using the regular NDK mechanisms.

You can see an introduction to Flutter Platform Channels and Getting Started with the NDK.

Maybe there's a more direct route, but that's the one I know could work.

like image 95
Xavier Rubio Jansana Avatar answered Sep 28 '22 10:09

Xavier Rubio Jansana