Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FlutterとAndroidの 連携を変えるかもしれないjni
Search
Mikami Hiroki
May 22, 2023
Programming
620
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
FlutterとAndroidの 連携を変えるかもしれないjni
Mikami Hiroki
May 22, 2023
More Decks by Mikami Hiroki
See All by Mikami Hiroki
KMPの今と未来
monaapk
0
590
Accompanistから考えるAndroidの権限許諾
monaapk
0
460
AndroidにおけるWebの進化
monaapk
1
310
DroidKaigi 2022のカンファレンスアプリを眺める
monaapk
2
1.1k
Circuit⚡
monaapk
0
410
Jetpack Compose と Accompanist
monaapk
0
340
Other Decks in Programming
See All in Programming
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
8
450
音楽のための関数型プログラミング言語mimiumにおける多段階計算の活用
tomoyanonymous
1
220
スマートグラスで並列バイブコーディング
hyshu
0
280
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
670
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
230
関数型プログラミングのメリットって何だろう?
wanko_it
0
120
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
280
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.6k
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
190
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
250
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.7k
Featured
See All Featured
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
Raft: Consensus for Rubyists
vanstee
141
7.6k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.9k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
GitHub's CSS Performance
jonrohan
1033
470k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
610
Rails Girls Zürich Keynote
gr2m
96
14k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Transcript
<< FlutterとAndroidの 連携を変えるかもしれないjni DroidKaigi.collect{ #3@Tokyo }
• 株式会社ゆめみ • AndroidとFlutterをぼちぼち • Twitter: @mono33__ みかみ
[1] Rethinking Dart interoperability with Android, https://youtu.be/ZWp2FJ2TuJs
アジェンダ 1. 従来のネイティブ連携 2. jni パッケージについて 3. まとめ
アジェンダ 1. 従来のネイティブ連携 2. jni パッケージについて 3. まとめ
Flutter標準のネイティブ連携 • プラットフォーム固有の機能やAPIは直接扱えない • MethodChannelなど、メッセージによってFlutterと ネイティブ間の連携を可能にする仕組み[2] [2] Writing custom platform-specific
code, https://docs.flutter.dev/platform-integration/platform-channels Flutter Android MethodChannel
MethodChannelの問題点 • クラスの指定や引数、メソッド名を文字列で扱う必要があ る 🤔
Pigeonパッケージの登場 • Flutterとネイティブ間で型安全な連携を可能にする コード生成ツール[3] • Dartでスキーマを記述し、Flutterとプラットフォームの共通 のインターフェースとなるコードを自動生成 [3] Pigeon, https://pub.dev/packages/pigeon
Pigeonのイメージ Flutter Android インターフェース .dart Pigeon インターフェース 1. スキーマの定義 2.
自動生成 3. それぞれ実装
アジェンダ 1. 従来のネイティブ連携 2. jni パッケージについて 3. まとめ
jniパッケージの概要 • Dart / Flutter から Java Native Interface(JNI) への
アクセスをサポートするパッケージ[4] • Android側でDartと連携する処理が不要 🎉 ◦ DartだけでAndroid側の処理を呼び出せる ◦ ContextやActivityを取得するメソッドも用意 [4] jin, https://pub.dev/packages/jni
jniのイメージ [5] jnigen/README.md, https://github.com/dart-lang/jnigen/blob/main/README.md C bindings dart bindings Flutter Java
code JNI jnigen.yaml (設定ファイル) 1. 設定 & 自動生成 2. dart bindings を扱う
4ステップで導入できる
1. 依存関係の追加 • pubspec.yamlにjniとjnigen[6]を追加 [6] jnigen, https://pub.dev/packages/jnigen
2. 設定ファイル(jnigen.yaml)の記述 • 自動生成周りの情報を記述 ◦ 自作のコードやAndroidXのライブラリやJavaの標準ラ イブラリも呼び出せる ◦ もちろんKotlinのコードも
jnigen.yamlのイメージ
jnigen.yamlのイメージ 生成するコードの設 定
jnigen.yamlのイメージ 呼び出したい ものの設定
jnigen.yamlのイメージ example.dartに Exampleクラスを呼び出す dart bindingsが生成
3. コードの自動生成 • jnigen.yamlを元にコードを自動生成
4. build.gradleの変更 • jnigenによって生成されるCMakeLists.txtのパスを設定
呼び出し DartだけでAndroid側の 処理を呼び出せる
アジェンダ 1. 従来のネイティブ連携 2. jni パッケージについて 3. まとめ
まとめ • jniはPigeonとは別アプローチでネイティブ連携を行う • 癖はあるがDartだけでネイティブの実装を呼び出せる • まだまだ🚧開発中🚧
補足・参考 • Swift 向けにffi[7]/ffigen[8]が開発されている • 公式にpedometer[9]というjni・ffiを使った サンプルのリポジトリ [7] ffi, https://pub.dev/packages/ffi
[8] ffigen, https://pub.dev/packages/ffigen [9] pedometer, https://github.com/flutter/samples/tree/main/experimental/pedometer
References • [1] Rethinking Dart interoperability with Android, https://youtu.be/ZWp2FJ2TuJs •
[2] Writing custom platform-specific code, https://docs.flutter.dev/platform-integration/platform-channels • [3] Pigeon, https://pub.dev/packages/pigeon • [4] jin, https://pub.dev/packages/jni • [5] jnigen/README.md, https://github.com/dart-lang/jnigen/blob/main/README.md • [6] jnigen, https://pub.dev/packages/jnigen • [7] ffi, https://pub.dev/packages/ffi • [8] ffigen, https://pub.dev/packages/ffigen • [9] pedometer, https://github.com/flutter/samples/tree/main/experimental/pedometer
CREDITS: This presentation template was created by Slidesgo, including icons
by Flaticon, infographics & images by Freepik. Thanks!