Giter Site home page Giter Site logo

leehack / flutter_golang_ffi_example Goto Github PK

View Code? Open in Web Editor NEW
35.0 4.0 5.0 11.6 MB

Example for Flutter and Golang binding

License: Other

C 15.99% Kotlin 0.18% Ruby 6.59% Swift 2.44% Objective-C 0.05% Dart 10.36% CMake 28.72% C++ 30.95% Makefile 3.67% Shell 0.84% Go 0.21%

flutter_golang_ffi_example's Introduction

native_add

Golang FFI plugin project for Flutter

Getting Started

This project is a starting point for a Flutter FFI plugin, a specialized package that includes native code directly invoked with Dart FFI.

Project structure

This template uses the following structure:

  • src: Contains the golang source code with Makefile that compiles the native code library for the various target platforms.

  • lib: Contains the Dart code that defines the API of the plugin, and which calls into the native code using dart:ffi.

  • platform folders (android, ios, windows, etc.): Contains the build files for building and bundling the native code library with the platform application.

Building and bundling native code

The pubspec.yaml specifies FFI plugins as follows:

  plugin:
    platforms:
      some_platform:
        ffiPlugin: true

This configuration invokes the native build for the various target platforms and bundles the binaries in Flutter applications using these FFI plugins.

This can be combined with dartPluginClass, such as when FFI is used for the implementation of one platform in a federated plugin:

  plugin:
    implements: some_other_plugin
    platforms:
      some_platform:
        dartPluginClass: SomeClass
        ffiPlugin: true

A plugin can have both FFI and method channels:

  plugin:
    platforms:
      some_platform:
        pluginClass: SomeName
        ffiPlugin: true

The native build systems that are invoked by FFI (and method channel) plugins are:

  • For Android: Gradle, which invokes the Android NDK for native builds.
    • See the documentation in android/build.gradle.
  • For iOS and MacOS: Xcode, via CocoaPods.
    • See the documentation in ios/native_add.podspec.
    • See the documentation in macos/native_add.podspec.
  • For Linux and Windows: CMake.
    • See the documentation in linux/CMakeLists.txt.
    • See the documentation in windows/CMakeLists.txt.

Binding to native code

To use the native code, bindings in Dart are needed. To avoid writing these by hand, they are generated from the header file (src/libsum.h) by package:ffigen. Regenerate the bindings by running flutter pub run ffigen --config ffigen.yaml.

Invoking native code

Very short-running native functions can be directly invoked from any isolate. For example, see sum in lib/native_add.dart.

Flutter help

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

flutter_golang_ffi_example's People

Contributors

leehack avatar

Stargazers

Buck avatar prongbang avatar Braden Preston avatar heramerom avatar Rody Davis avatar Brad Pillow avatar Ma3X avatar Steve Phillips avatar  avatar  avatar yujie avatar  avatar  avatar  avatar Felicity Yin avatar bhalle avatar linguohua avatar brave723 avatar Burak Karahan avatar Roman Cherkasov avatar DengBo avatar Vinicius Fortuna avatar  avatar Akmal avatar Jente Jan de Waart avatar James Mills avatar Andy.Zhao avatar Yan avatar LI YUYANG avatar Lenz Paul avatar  avatar Perry Birch avatar agung susanto avatar Aleksandr Efremenkov avatar Yash Khattar avatar

Watchers

 avatar James Mills avatar Pratyush Harsh avatar  avatar

flutter_golang_ffi_example's Issues

Compilation on macos m1

First of all, thank you very much for the wonderful article.

I downloaded the repository and ran the example on macos and it worked!
The problem was when I tried to rebuild the dylib and from then on the lib was no longer found nor its reference.
Did you compile it in any different way?
I'm trying to cross over from macos with Apple Silicom M1

CC="clang -arch x86_64"
CGO_ENABLED=1
GOARCH=amd64
GOOS=darwin
go build -trimpath -buildmode=c-shared -o libsum.dylib ../src/sum.go

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.