Slide 1

Slide 1 text

Atsushi Eno (Xamarin) Embeddinator-4000: using .NET libraries in Android Studio C A C h b

Slide 2

Slide 2 text

What is e4k? Embeddinator-4000とは Embed .NET libraries in native platform environment. almost anywhere 2 .NETライブラリを ネイティブプラットフォームで組み 込み再利用する だいたいどこでも動く https://github.com/mono/Embeddinator-4000/

Slide 3

Slide 3 text

Aren't you Xamarin? Xamarinと何が違うの? Xamarin: ecosystem to build mobile apps in C# e4k: tool to bind .NET libraries in native languages Both are from Mono/Xam team 3 Xamarinはアプリケーションを全部 C#/F#で作る生態系 e4kは.NETライブラリを他言語で利用で きるバインディングを作るツール どちらもMono/Xamarinチーム発

Slide 4

Slide 4 text

Aren't you Xamarin? Xamarinと何が違うの? 4

Slide 5

Slide 5 text

Goals このセッションの目的 ● Introduce e4k ● discuss how interop layer works 5 ● e4kがどんなものか紹介する ● e4kを肴に、プラットフォーム相互 運用のなんたるかを話す Xamarin Kotlin/Native JS, swift, ruby, python... etc.

Slide 6

Slide 6 text

Background いくつかの前提知識 6 Mono Android NDK JNI

Slide 7

Slide 7 text

Mono 7 Mono NDK JNI ● C#/.NET dev./exec. environment for Linux ● OSS since 2001 ● bytecode with VM ● C#/.NETの開発実行環境 ● 2001年からLinuxのOSS ● 仮想マシンコード

Slide 8

Slide 8 text

Dev. Fx Components 開発フレームワークの構成要素 8 Mono NDK JNI ● Exec. Environment ○ Runtime, Standard libs ● Dev. Environment ○ Compilers, build tools, IDE ● 実行環境 ○ ランタイム、標準ライブラリ ● 開発環境 ○ コンパイラ、ビルドツール、 IDE Android - ART, android.jar, javac, gradle, AS Mono - mono, System*.dll, Roslyn csc, MSBuild, VS

Slide 9

Slide 9 text

Runtimes ランタイム 9 Mono NDK JNI ● Usually executables ● Can be used as a library ● with C embedded API ● 通常は実行可能ファイル ● 大抵はネイティブライブラリと ● Cの組み込みAPIがある "embedded mono" in Xamarin, e4k, Unity... $ gcc `pkg-config --cflags mono-2` `pkg-config --libs mono-2` ...

Slide 10

Slide 10 text

Native code in Android 10 Mono NDK JNI ● Android NDK: C/C++ toolchain for Android ● Android is Linux ○ has Kernel and libc (mono uses it for I/O impl. etc.) ● Android NDK: Android用C/C++ ツール群 ● AndroidはLinux ○ kernel APIやlibcが使える(monoが I/Oの実装などで使う) mono builds with Android NDK (LLVM/Clang or GCC)

Slide 11

Slide 11 text

JNI: C-Java interop Javaとの相互運用 11 Mono NDK JNI ● C/C++ API to manipulate JVM ● load bytecode as classes to VM, run methods etc. ● exists in Dalvik/ART too. ● JVMを操作するC/C++ API ● .classをVMにロードしたり、 メソッドを実行したりできる ● Androidでも実装されている Java: `native` / Kotlin: `external` .NET: `extern` (P/Invoke)

Slide 12

Slide 12 text

Summary ここまでのまとめ 12 ● e4k builds on top of Mono ● runtime like Mono usually written in C/C++, has external C API, and ● typically works with NDK ● Android uses Linux, Mono is built on Linux ● Java is accessible via JNI ● e4kはMonoで作られている ● Monoのようなランタイムはたいて いC/C++で作られ、C APIをもち、 ● たいがいNDKでビルド可能 ● AndroidはLinuxであり、Monoは Linux上で動く ● Java VMはJNIで操作可能

Slide 13

Slide 13 text

e4k in action Demos

Slide 14

Slide 14 text

Before showing demos... 事前説明 14 ● Basically e4k is C API generator ● Java support is just a wrapper around it ● the demos are twofolds: ○ 1: how C interop works ○ 2: how Java interop works ● e4kの基本はC APIの自動生成 ● Javaサポートはその応用例 ● デモは2部構成 ○ 1: C相互運用の例 ○ 2: Java相互運用の例

Slide 15

Slide 15 text

Demo 1 15 ● nuget install Embeddinator-4000 ● vi MyLibrary.cs ● csc MyLibrary.cs -t:library ● mono Embeddinator-4000/tools/Embeddinator-4000.exe -gen C -p (Linux) -o output -c -d MyLibrary.dll ● cd output ● cp ../MyLibrary.dll . ● vi myapp.c ● gcc myapp.c `pkg-config --cflags mono-2` `pkg-config --libs mono-2` -L . -l MyLibrary namespace KaigiDemo { public class Greeter { public string Greet(string name) { return "Hello, " + name; } KaigiDemo_Greeter *p = KaigiDemo_Greeter_new(); puts (KaigiDemo_Greeter_Greet(p, "Droiders"));

Slide 16

Slide 16 text

Demo 2 16 ● mono Embeddinator-4000/tools/Embeddinator-4000.exe -gen Java -p Android -o output -c -d MyLibrary.dll ● # launch Android Studio and create project Test1 ● cp output/MyLibrary.aar ~/AndroidStudioProjects/Test1/app/libs app/kotlin/var msg = mylibrary.kaigidemo.Greeter ().greet("Droiders") this.findViewById(R.id.the_text_view).setText(msg) MainActivity.kt android { aaptOptions { noCompress 'dll' } } implementation fileTree(dir: 'libs', include: ['*.aar']) activity_main.xml app/build.gradle

Slide 17

Slide 17 text

e4k current status from users' perspective

Slide 18

Slide 18 text

How is e4k mature? e4kの完成度は? 18 Not useful yet usable native runtime C code generator is okay immature Java generator tooling not featureful yet no real-world libraries まだ一般向けではない  ランタイムは使えそう  Cジェネレータはまあまあ使えそう  Javaジェネレータは未熟  ツールは基本機能のみ  ライブラリの生態系の不在

Slide 19

Slide 19 text

e4k major issues 主な問題点 19 unable to bind core libraries difficult target to achieve huge, unsafe, too special Only one e4k-ed aar can be used embedded runtime conflicts lack of library ecosystem コアライブラリがバインドできない  難易度高め  巨大、unsafeなど特別なものが多い e4kのaarは1つしか使えない  組み込みランタイムが衝突  ライブラリ体系の不在問題

Slide 20

Slide 20 text

e4k detailed issues 細かい問題点 20

Slide 21

Slide 21 text

is e4k useless? what's good? 使えないの? 21 e4k Runtime is powerful enough to run Xamarin.Forms Xamarin.Plugins are plenty of potentially reusable x-plat code e4kランタイムはXamarin.Formsを呼び 出せる程度には実用的 Xamarin.Pluginsに再利用可能になりそ うなライブラリがたくさんある

Slide 22

Slide 22 text

is e4k useless? what's good? 使えないの? 22 API: use methods with simple parameters and simple returns (such as strings) Use Assembly Linker to filter out blockers and then bind it 文字列など簡単な型のみを使ったメソッ ドを使うAPIを定義する アセンブリリンカーを使って邪魔なコード を削除してからバインドする

Slide 23

Slide 23 text

binding architectures e4kなどから学べること

Slide 24

Slide 24 text

Binding System: Quality Index バインディング生態系の評価軸 24 ● Design ○ Well Documented? ○ Well Tested? ● Exec. Environment ○ How interoperable? ○ Performance? ● 設計 ○ きちんと文書化されているか? ○ 多様な状況を想定して吟味/テスト されているか? ● 実行環境 ○ どれくらい相互運用可能か? ○ パフォーマンスは?

Slide 25

Slide 25 text

Binding System: Quality Index バインディング生態系の評価軸 25 ● Binding Capabilities ○ Bindings automated? ○ Easy to import platform native libs? ● Dev. Experiences ○ Distributable outcome? ○ IDE supported? ● バインディング生成能力 ○ バインディング自動生成機構は? ○ プラットフォーム言語のライブラリは 利用可? ● 開発環境 ○ ライブラリ利用/配布は容易か? ○ IDEサポートは?

Slide 26

Slide 26 text

Implementation Strategy 実装戦略 26 ● transpiler or interop runtime ● for interop: ○ C as the connector ■ no C++ ○ Clang: binder helper ○ LLVM: runtime helper? ● Common traps: pointers, value types, union, sizeof int ● 事前コード変換と実行時相互運用 ● 相互運用の場合 ○ Cを相互運用の基盤にする ■ C++は難しいので避ける ○ Clang ASTが便利 ○ LLVMランタイムも便利? ● C呼び出しの一般的な問題: ポインタ、 値型、union、sizeof int

Slide 27

Slide 27 text

Implementation Strategy 実装戦略 27 ● Java interop difficulties ○ variants (co-/contra-) ○ non-public inheritance ○ erased generics ● C# interop difficulties ○ conflicts in interfaces ○ reified generics ○ operator overloads ● Java相互運用の課題 ○ 共変と反変 ○ non-publicからの継承 ○ ジェネリクス (erased) ● C#相互運用の課題 ○ interfaceメンバーの衝突 ○ ジェネリクス (reified) ○ 演算子オーバーロード

Slide 28

Slide 28 text

References 参考資料 28 ● https://github.com/mono/embeddi nator-4000/ - github repo ● https://mono.github.io/Embeddinat or-4000/ - docs on github.io ● https://www.youtube.com/watch?v =Oj4q93ddrGs - Xamarin.Forms and Embeddinator-4000 for Android Studio ● https://speakerdeck.com/atsushieno/embeddin ator-4000karaxue-buxamarinfalseji-chu - Embeddinator-4000から学ぶXamarinの基礎 ● https://xamaritans.booth.pm/items/669614 - Extensive Xamarin