Skip to content

Commit 12a7e55

Browse files
authored
[jnigen] Create FFI plugin (#8)
Stub from `flutter create --template plugin_ffi` with GitHub workflow support.
1 parent b2f7def commit 12a7e55

File tree

102 files changed

+4141
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+4141
-0
lines changed

.github/workflows/test-package.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,68 @@ jobs:
7373
with:
7474
github-token: ${{ secrets.GITHUB_TOKEN }}
7575
path-to-lcov: ./jni_gen/coverage/lcov.info
76+
77+
build_jni_example_linux:
78+
runs-on: ubuntu-latest
79+
defaults:
80+
run:
81+
working-directory: pkg/jni/example
82+
steps:
83+
- uses: actions/checkout@v3
84+
- uses: subosito/flutter-action@v2
85+
with:
86+
channel: 'stable'
87+
- run: |
88+
sudo apt-get update -y
89+
sudo apt-get install -y ninja-build libgtk-3-dev
90+
- run: flutter config --enable-linux-desktop
91+
- run: flutter pub get
92+
- run: flutter build linux
93+
94+
build_jni_example_windows:
95+
runs-on: windows-latest
96+
defaults:
97+
run:
98+
working-directory: pkg/jni/example
99+
steps:
100+
- uses: actions/checkout@v3
101+
- uses: subosito/flutter-action@v2
102+
with:
103+
channel: 'stable'
104+
- run: flutter config --enable-windows-desktop
105+
- run: flutter pub get
106+
- run: flutter build windows
107+
108+
build_jni_example_macos:
109+
runs-on: macos-latest
110+
defaults:
111+
run:
112+
working-directory: pkg/jni/example
113+
steps:
114+
- uses: actions/checkout@v3
115+
- uses: subosito/flutter-action@v2
116+
with:
117+
channel: 'stable'
118+
architecture: x64
119+
- run: flutter config --enable-macos-desktop
120+
- run: flutter pub get
121+
- run: flutter build macos
122+
123+
build_jni_example_android:
124+
runs-on: ubuntu-latest
125+
defaults:
126+
run:
127+
working-directory: pkg/jni/example
128+
steps:
129+
- uses: actions/checkout@v3
130+
- uses: actions/setup-java@v2
131+
with:
132+
distribution: 'zulu'
133+
java-version: '11'
134+
- uses: subosito/flutter-action@v2
135+
with:
136+
channel: 'stable'
137+
- run: flutter pub get
138+
- run: flutter build apk
139+
- run: flutter build appbundle
140+

pkgs/jni/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
26+
/pubspec.lock
27+
**/doc/api/
28+
.dart_tool/
29+
.packages
30+
build/

pkgs/jni/.metadata

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled.
5+
6+
version:
7+
revision: 676cefaaff197f27424942307668886253e1ec35
8+
channel: stable
9+
10+
project_type: plugin_ffi
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: 676cefaaff197f27424942307668886253e1ec35
17+
base_revision: 676cefaaff197f27424942307668886253e1ec35
18+
- platform: android
19+
create_revision: 676cefaaff197f27424942307668886253e1ec35
20+
base_revision: 676cefaaff197f27424942307668886253e1ec35
21+
- platform: linux
22+
create_revision: 676cefaaff197f27424942307668886253e1ec35
23+
base_revision: 676cefaaff197f27424942307668886253e1ec35
24+
- platform: macos
25+
create_revision: 676cefaaff197f27424942307668886253e1ec35
26+
base_revision: 676cefaaff197f27424942307668886253e1ec35
27+
- platform: windows
28+
create_revision: 676cefaaff197f27424942307668886253e1ec35
29+
base_revision: 676cefaaff197f27424942307668886253e1ec35
30+
31+
# User provided section
32+
33+
# List of Local paths (relative to this file) that should be
34+
# ignored by the migrate tool.
35+
#
36+
# Files that are not part of the templates will be ignored by default.
37+
unmanaged_files:
38+
- 'lib/main.dart'
39+
- 'ios/Runner.xcodeproj/project.pbxproj'

pkgs/jni/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* TODO: Describe initial release.

pkgs/jni/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2022, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/jni/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# jni
2+
3+
A new Flutter FFI plugin project.
4+
5+
## Getting Started
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.
10+
11+
## Project stucture
12+
13+
This template uses the following structure:
14+
15+
* `src`: Contains the native source code, and a CmakeFile.txt file for building
16+
that source code into a dynamic library.
17+
18+
* `lib`: Contains the Dart code that defines the API of the plugin, and which
19+
calls into the native code using `dart:ffi`.
20+
21+
* platform folders (`android`, `ios`, `windows`, etc.): Contains the build files
22+
for building and bundling the native code library with the platform application.
23+
24+
## Buidling and bundling native code
25+
26+
The `pubspec.yaml` specifies FFI plugins as follows:
27+
28+
```yaml
29+
plugin:
30+
platforms:
31+
some_platform:
32+
ffiPlugin: true
33+
```
34+
35+
This configuration invokes the native build for the various target platforms
36+
and bundles the binaries in Flutter applications using these FFI plugins.
37+
38+
This can be combined with dartPluginClass, such as when FFI is used for the
39+
implementation of one platform in a federated plugin:
40+
41+
```yaml
42+
plugin:
43+
implements: some_other_plugin
44+
platforms:
45+
some_platform:
46+
dartPluginClass: SomeClass
47+
ffiPlugin: true
48+
```
49+
50+
A plugin can have both FFI and method channels:
51+
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.
92+

pkgs/jni/analysis_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options

pkgs/jni/android/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.cxx

pkgs/jni/android/build.gradle

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// The Android Gradle Plugin builds the native code with the Android NDK.
2+
3+
group 'dev.dart.jni'
4+
version '1.0'
5+
6+
buildscript {
7+
repositories {
8+
google()
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
// The Android Gradle Plugin knows how to build native code with the NDK.
14+
classpath 'com.android.tools.build:gradle:7.1.2'
15+
}
16+
}
17+
18+
rootProject.allprojects {
19+
repositories {
20+
google()
21+
mavenCentral()
22+
}
23+
}
24+
25+
apply plugin: 'com.android.library'
26+
27+
android {
28+
// Bumping the plugin compileSdkVersion requires all clients of this plugin
29+
// to bump the version in their app.
30+
compileSdkVersion 31
31+
32+
// Bumping the plugin ndkVersion requires all clients of this plugin to bump
33+
// the version in their app and to download a newer version of the NDK.
34+
35+
// Note(MaheshH): Seems 22 is lowest one can get through SDKManager now?
36+
//
37+
// It's more of a logistic issue, I can't download NDK 21, so keeping it
38+
// 22. You might get a warning to bump some versions.
39+
ndkVersion "22.1.7171670"
40+
// ndkVersion "21.1.6352462"
41+
42+
// Invoke the shared CMake build with the Android Gradle Plugin.
43+
externalNativeBuild {
44+
cmake {
45+
path "../src/CMakeLists.txt"
46+
47+
// The default CMake version for the Android Gradle Plugin is 3.10.2.
48+
// https://developer.android.com/studio/projects/install-ndk#vanilla_cmake
49+
//
50+
// The Flutter tooling requires that developers have CMake 3.10 or later
51+
// installed. You should not increase this version, as doing so will cause
52+
// the plugin to fail to compile for some customers of the plugin.
53+
// version "3.10.2"
54+
}
55+
}
56+
57+
compileOptions {
58+
sourceCompatibility JavaVersion.VERSION_1_8
59+
targetCompatibility JavaVersion.VERSION_1_8
60+
}
61+
62+
defaultConfig {
63+
minSdkVersion 16
64+
}
65+
}

pkgs/jni/android/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'jni'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="dev.dart.jni">
3+
</manifest>

pkgs/jni/example/.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.packages
31+
.pub-cache/
32+
.pub/
33+
/build/
34+
35+
# Web related
36+
lib/generated_plugin_registrant.dart
37+
38+
# Symbolication related
39+
app.*.symbols
40+
41+
# Obfuscation related
42+
app.*.map.json
43+
44+
# Android Studio will place build artifacts here
45+
/android/app/debug
46+
/android/app/profile
47+
/android/app/release

0 commit comments

Comments
 (0)