Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embeddinator-4000: using .NET libraries in Android Studio

Atsushi Eno
February 08, 2018

Embeddinator-4000: using .NET libraries in Android Studio

DroidKaigi 2018

Atsushi Eno

February 08, 2018
Tweet

More Decks by Atsushi Eno

Other Decks in Programming

Transcript

  1. What is e4k? Embeddinator-4000とは Embed .NET libraries in native platform

    environment. almost anywhere 2 .NETライブラリを ネイティブプラットフォームで組み 込み再利用する だいたいどこでも動く https://github.com/mono/Embeddinator-4000/
  2. 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チーム発
  3. Goals このセッションの目的 • Introduce e4k • discuss how interop layer works

    5 • e4kがどんなものか紹介する • e4kを肴に、プラットフォーム相互 運用のなんたるかを話す Xamarin Kotlin/Native JS, swift, ruby, python... etc.
  4. Mono 7 Mono NDK JNI • C#/.NET dev./exec. environment for

    Linux • OSS since 2001 • bytecode with VM • C#/.NETの開発実行環境 • 2001年からLinuxのOSS • 仮想マシンコード
  5. 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
  6. 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` ...
  7. 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)
  8. 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)
  9. 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で操作可能
  10. 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相互運用の例
  11. 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"));
  12. 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<TextView>(R.id.the_text_view).setText(msg) MainActivity.kt android { aaptOptions { noCompress 'dll' } } implementation fileTree(dir: 'libs', include: ['*.aar']) <TextView android:id="@+id/the_text_view" ... /> activity_main.xml app/build.gradle
  13. 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ジェネレータは未熟  ツールは基本機能のみ  ライブラリの生態系の不在
  14. 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つしか使えない  組み込みランタイムが衝突  ライブラリ体系の不在問題
  15. 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に再利用可能になりそ うなライブラリがたくさんある
  16. 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を定義する アセンブリリンカーを使って邪魔なコード を削除してからバインドする
  17. Binding System: Quality Index バインディング生態系の評価軸 24 • Design ◦ Well

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

    Bindings automated? ◦ Easy to import platform native libs? • Dev. Experiences ◦ Distributable outcome? ◦ IDE supported? • バインディング生成能力 ◦ バインディング自動生成機構は? ◦ プラットフォーム言語のライブラリは 利用可? • 開発環境 ◦ ライブラリ利用/配布は容易か? ◦ IDEサポートは?
  19. 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
  20. 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) ◦ 演算子オーバーロード
  21. 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