How to use native C library in flutter package


I have a library written in C which I have previously used from a java android app via JNI.

I am now moving to use flutter instead and the plan was to make a flutter package that wraps the C library.

The flutter project for the package has been created and the plan is to use Platform Channels (invokeMethod to call some simple Java methods that invokes the JNI for the library. I think I have most of this implemented.

Problem is, however that I would normally add the info about the existence of the library and JNI to the build.gradle of my android java project. I can however not find these files when I am working in the structure of a flutter package. If I instead build a standard flutter project (an app with UI) i suddenly have the build.gradle files.

How can I add these gradle files or should I add the info about the lib/JNI to some other flutter specific files instead? Do the multilayered dart->platformChannel -> java -> jni -> C library aproach sound like the right way of doing it? or is there a better way?


Are you sure you created a Flutter plugin project (not a package)? A plugin is a type of package with Dart/Kotlin (or Java)/Swift (or Obj-C) code implementing the Flutter-native layer, plus an example app that exercises and demonstrates use of the plugin.

A Flutter plugin has 3 build.gradle files:

  1. ./android/build.grade where you set the plugin’s min SDK, dependencies, etc
  2. ./example/android/build.grade which is generally left unchanged
  3. ./example/android/app/build.gradle which is where you set things for the example app

Any changes you make to (1) bubble up to the example app (which is just a sample app that uses your plugin). Likewise, when you use the plugin in a different app, that app just picks up (1) from your plugin.

Add your dependencies to (1).

Answered By – Richard Heap

Leave a Comment