How to use native C library in flutter package
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:
./android/build.grade
where you set the plugin's min SDK, dependencies, etc./example/android/build.grade
which is generally left unchanged./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).
kimusan
Software developer by daytime, keyboard hacker by night time. Always om the lookout for new fun projects to work on.
Updated on December 12, 2022Comments
-
kimusan 11 months
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?
-
kimusan over 4 yearsYou are right. The project was created as package and not plugin. Is there any way top change this (package -> plugin) or do I have to redo the changes I have already done to the project?
-
Richard Heap over 4 yearsIt's best to start with a new project and copy the implementation over.