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

Taming the NDK dragon for cross-platform develo...

Taming the NDK dragon for cross-platform development

Talk at Droidcon ZG about how to approach cross-platform development with NDK.

Jernej Virag

April 28, 2016
Tweet

More Decks by Jernej Virag

Other Decks in Technology

Transcript

  1. Jernej Virag / PSPDFKit Taming the NDK dragon NDK DRAGON

    Taming the for cross-platform development Jernej Virag / PSPDFKit
  2. Jernej Virag / PSPDFKit Taming the NDK dragon So much

    choice… j2objc Xamarin RoboVM LUA ObjC Apportable Swift JavaScript Appcelerator Titanium C / C++
  3. Jernej Virag / PSPDFKit Taming the NDK dragon NDK r11c

    • LLVM / Clang 3.8 or GCC 4.9 • C++ STL libraries: stdc++, gabi++, stlport, gnustl and libc++ • Access to: • OpenGL ES 1.0 - 3.1 • OpenSL ES • OpenMAX AL • zlib • logcat • sensors • configuration • APK assets • OBB files
  4. Jernej Virag / PSPDFKit Taming the NDK dragon Compiling •

    ndk-build with custom .mk configuration • Gradle Experimental Plugin • Standalone toolchain with standard tools 
 (CMake, etc.) } lib/<arch>/libname.so
  5. Jernej Virag / PSPDFKit Taming the NDK dragon NDK C++

    STL libraries: libstdc++, gabi++, stlport, gnustl, libc++ None of them fully featured. ¯\_(ϑ)_/¯
  6. Jernej Virag / PSPDFKit Taming the NDK dragon Architectures CPU

    TYPE ARM5/6** ARM7 ARM8 X86 X86_64 MIPS*** armeabi ✓ ✓ ✓ ✓ ✓ armeabi-v7a ✓ ✓ ✓* ✓ arm64-v8a ✓ x86 ✓ ✓ x86_64 ✓ mips ✓ * via libhoudini, slower and buggy ** obsolete architecture, not used anymore
 *** no known consumer devices
  7. Jernej Virag / PSPDFKit Taming the NDK dragon JNI JVM

    DalvikVM ART libmyndklib.so Java Native Interface
  8. Jernej Virag / PSPDFKit Taming the NDK dragon Djinni 


    
 https://github.com/dropbox/djinni
  9. Jernej Virag / PSPDFKit Taming the NDK dragon Djinni Page

    = interface +c { const get_text(): string; const get_page_info(): PageInfo; get_text_rect(coord: Point, tolerance: f32): optional<Rect>; close(); }
  10. Jernej Virag / PSPDFKit Taming the NDK dragon Java public

    class Page { public String getText() { .. }; public PageInfo getPageInfo() { .. }; public RectF getTextRect(Point coord, float tolerance) { .. }; public void close() { .. }; } C++ class Page { virtual std::string get_text() = 0; virtual PageInfo get_page_info() = 0; virtual std::optional<Rect> get_text_rect(Point coord, float tolerance) = 0; virtual void close() = 0; }
  11. Jernej Virag / PSPDFKit Taming the NDK dragon Djinni page.djinni

    NativePage.java NativePage.cpp Page.cpp PSPDFPage.mm Java API JNI glue Objective-C API C++ implementation
  12. Jernej Virag / PSPDFKit Taming the NDK dragon Djinni Page

    = interface +j +o { const get_text(): string; const get_page_info(): PageInfo; get_text_rect(coord: Point, tolerance: f32): optional<Rect>; close(); }
  13. Jernej Virag / PSPDFKit Taming the NDK dragon Djinni -

    tips • Minimize JNI calls (remember, slow!) • Keep strings in UTF-8. • Keep threading in Android / avoid native threads - keep calls synchronized. • APIs can be exposed both ways (Java -> C++ and C++ -> Java), use it!
  14. Jernej Virag / PSPDFKit Taming the NDK dragon Debugging and

    testing NDK debugging is kind of a mess.
  15. Jernej Virag / PSPDFKit Taming the NDK dragon Debugging and

    testing • Compile binary in debug mode (-g !) to show stack trace in logcat. • Use ndk-stack to resolve addresses to line numbers. • Path is the path to JNI compilation ‘obj’ directory with unstripped binaries. adb logcat | ndk-stack obj/local
  16. Jernej Virag / PSPDFKit Taming the NDK dragon Debugging and

    testing GTest https://github.com/google/googletest
  17. Jernej Virag / PSPDFKit Taming the NDK dragon GTest on

    Android device Android.mk, build binary Jenkins / TC JUnit output compatible script
  18. Jernej Virag / PSPDFKit Taming the NDK dragon Profiling •

    No good support at all - XCode Instruments still the best tool. • nVidia Tegra Profiler is the only working one - needs a (rooted) Tegra device. https://developer.nvidia.com/tegra-system-profiler
  19. Jernej Virag / PSPDFKit Taming the NDK dragon Cross-platform tips

    • Minimize cross-language calls. • Use modern C++14 and use it wisely. • Never link against system libraries. • Offload heavy dependencies (regex, charsets, crypto) to platform libraries.
  20. Jernej Virag / PSPDFKit Taming the NDK dragon Cross-platform tips

    • Don’t rely on C++ UTF implementation - use miniUTF or platform APIs. • Be conservative with dependencies - “smart” libraries will break. • Link everyting in a single static .so library. Dynamic linking is full of issues on Android.