首页 > 解决方案 > 如何在 Flutter 包中使用原生 C 库

问题描述

我有一个用 C 编写的库,我以前通过 JNI 从 java android 应用程序中使用过它。

我现在转而使用颤振,计划是制作一个包装 C 库的颤振包。

包的flutter项目已经创建,计划使用平台通道(invokeMethod来调用一些简单的Java方法来调用库的JNI。我想我已经实现了大部分。

但是问题是,我通常会将有关库和 JNI 存在的信息添加到我的 android java 项目的 build.gradle 中。但是,当我在颤振包的结构中工作时,我找不到这些文件。如果我改为构建一个标准的颤振项目(一个带有 UI 的应用程序),我突然有了 build.gradle 文件。

如何添加这些 gradle 文件,或者我应该将有关 lib/JNI 的信息添加到其他一些特定于颤振的文件中?多层 dart->platformChannel -> java -> jni -> C library 方法听起来是正确的做法吗?或者,还有更好的方法?

标签: androidcflutterandroid-ndkjava-native-interface

解决方案


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).


推荐阅读