|
1 | | -# jni |
| 1 | +# jni (experimental module) |
2 | 2 |
|
3 | | -A new Flutter FFI plugin project. |
| 3 | +This is a utility library to access JNI from Dart / Flutter code, intended as a supplement for `jnigen` code generator, as well as provide the common base components (such as managing the JVM instance) to the code generated by `jni_gen`. |
4 | 4 |
|
5 | | -## Getting Started |
| 5 | +This library contains: |
6 | 6 |
|
7 | | -This project is a starting point for a Flutter |
8 | | -[FFI plugin](https://docs.flutter.dev/development/platform-integration/c-interop), |
9 | | -a specialized package that includes native code directly invoked with Dart FFI. |
| 7 | +* functions to access the JNIEnv and JavaVM variables from JNI, and wrapper functions to those provided by JNI. (`Jni.getEnv`, `Jni.getJavaVM`). |
10 | 8 |
|
11 | | -## Project stucture |
| 9 | +* Functions to spawn a JVM on desktop platforms (`Jni.spawn`). |
12 | 10 |
|
13 | | -This template uses the following structure: |
| 11 | +* Some utility functions to make it easier to work with JNI in Dart; eg: To convert a java string object to Dart string (mostly as extension methods on `Pointer<JniEnv>`). |
14 | 12 |
|
15 | | -* `src`: Contains the native source code, and a CmakeFile.txt file for building |
16 | | - that source code into a dynamic library. |
| 13 | +* Some Android-specific helpers (get application context and current activity references). |
17 | 14 |
|
18 | | -* `lib`: Contains the Dart code that defines the API of the plugin, and which |
19 | | - calls into the native code using `dart:ffi`. |
| 15 | +* Some helper classes and functions to simplify one-off uses (`JniObject` and `JniClass` intended for calling functions by specifying the name and arguments. It will reduce some boilerplate when you're debugging. Note: this API is slightly incomplete). |
20 | 16 |
|
21 | | -* platform folders (`android`, `ios`, `windows`, etc.): Contains the build files |
22 | | - for building and bundling the native code library with the platform application. |
| 17 | +This is intended for one-off / debugging uses of JNI, as well as providing a base library for code generated by jni_gen. |
23 | 18 |
|
24 | | -## Buidling and bundling native code |
| 19 | +__To interface a complete java library, look forward for `jni_gen`.__ |
25 | 20 |
|
26 | | -The `pubspec.yaml` specifies FFI plugins as follows: |
| 21 | +## Platform support |
| 22 | +The focus of this project is Flutter Android, since Flutter Android apps already have a JVM, and JNI enables interop with existing Java code and Android Platform APIs. This project also (partially) supports Linux desktop by spawning a JVM through JNI. |
27 | 23 |
|
28 | | -```yaml |
29 | | - plugin: |
30 | | - platforms: |
31 | | - some_platform: |
32 | | - ffiPlugin: true |
33 | | -``` |
| 24 | +## Version note |
| 25 | +This library is at an early stage of development and we do not provide backwards compatibility of the API at this point. |
34 | 26 |
|
35 | | -This configuration invokes the native build for the various target platforms |
36 | | -and bundles the binaries in Flutter applications using these FFI plugins. |
| 27 | +## Documentation |
| 28 | +The test/ directory contains files with comments explaining the basics of this module, and the example/ directory contains a flutter example which also touches some Android-specifics. |
37 | 29 |
|
38 | | -This can be combined with dartPluginClass, such as when FFI is used for the |
39 | | -implementation of one platform in a federated plugin: |
| 30 | +Using this library assumes some familiarity with JNI - it's threading model and object references, among other things. |
40 | 31 |
|
41 | | -```yaml |
42 | | - plugin: |
43 | | - implements: some_other_plugin |
44 | | - platforms: |
45 | | - some_platform: |
46 | | - dartPluginClass: SomeClass |
47 | | - ffiPlugin: true |
48 | | -``` |
| 32 | +## jni_gen |
49 | 33 |
|
50 | | -A plugin can have both FFI and method channels: |
| 34 | +This library is a part of `jni_gen` - a 2022 GSoC project. |
51 | 35 |
|
52 | | -```yaml |
53 | | - plugin: |
54 | | - platforms: |
55 | | - some_platform: |
56 | | - pluginClass: SomeName |
57 | | - ffiPlugin: true |
58 | | -``` |
59 | | -
|
60 | | -The native build systems that are invoked by FFI (and method channel) plugins are: |
61 | | -
|
62 | | -* For Android: Gradle, which invokes the Android NDK for native builds. |
63 | | - * See the documentation in android/build.gradle. |
64 | | -* For iOS and MacOS: Xcode, via CocoaPods. |
65 | | - * See the documentation in ios/jni.podspec. |
66 | | - * See the documentation in macos/jni.podspec. |
67 | | -* For Linux and Windows: CMake. |
68 | | - * See the documentation in linux/CMakeLists.txt. |
69 | | - * See the documentation in windows/CMakeLists.txt. |
70 | | -
|
71 | | -## Binding to native code |
72 | | -
|
73 | | -To use the native code, bindings in Dart are needed. |
74 | | -To avoid writing these by hand, they are generated from the header file |
75 | | -(`src/jni.h`) by `package:ffigen`. |
76 | | -Regenerate the bindings by running `flutter pub run ffigen --config ffigen.yaml`. |
77 | | - |
78 | | -## Invoking native code |
79 | | - |
80 | | -Very short-running native functions can be directly invoked from any isolate. |
81 | | -For example, see `sum` in `lib/jni.dart`. |
82 | | - |
83 | | -Longer-running functions should be invoked on a helper isolate to avoid |
84 | | -dropping frames in Flutter applications. |
85 | | -For example, see `sumAsync` in `lib/jni.dart`. |
86 | | - |
87 | | -## Flutter help |
88 | | - |
89 | | -For help getting started with Flutter, view our |
90 | | -[online documentation](https://flutter.dev/docs), which offers tutorials, |
91 | | -samples, guidance on mobile development, and a full API reference. |
| 36 | +The broader aim of jni_gen is making Java APIs accessible from dart in an idiomatic way. |
92 | 37 |
|
0 commit comments