Running native code on Android #OSDCfr 2012

Running native code on Android #OSDCfr 2012

* cross-compiling 3rd party libraries with the Android Standalone Toolchain,
* using prebuilt libraries,
* using NEON code while still targeting all devices,
* illustrating some Java Native Interface (JNI) tips and tricks

5d911b7a5d9158ad9fdcc6a493c7333c?s=128

Cédric Deltheil

October 13, 2012
Tweet

Transcript

  1. Running native code on Android OSDC.fr 2012-10-13 Cédric @deltheil C

    NDK ARM
  2. - Why using native code? -

  3. 1. Reuse some C code

  4. 2. Use 3rd party libraries

  5. 3. Create portable components iOS Android ...

  6. 4. Use hardware acceleration CPU SIMD algorithm

  7. 5. ... because it’s fun :)

  8. ...but How? Native Development Kit

  9. - Android NDK overview -

  10. Downloads http://developer.android.com/tools/sdk/ndk/index.html cross-platform

  11. Contents 1. Tools for Linux,OS X,Win to cross-compile native ARM,x86,MIPS

    binaries.
  12. Contents 2. System files and headers for Android native APIs.

    w/ Java Native Interface (a.k.a JNI) <jni.h>
  13. Contents 3. Easy-to-use build system: Android.mk & ndk-build (make wrapper).

  14. Contents 4. Documentation and code samples.

  15. - Steps to embed C code -

  16. 1/3 Wrap C Java JNI C code C Java libmisc.so

  17. 1/3 Wrap C /* 1. Wrap your C code with

    the JNI */ #include <jni.h> jstring Java_com_example_Foo_bar(JNIEnv* env, jobject thiz) { char buffer[512]; /* ... */ return (*env)->NewStringUTF(env, buffer); } namespace class method instance native interface
  18. 2/3 Build native lib Java JNI C code C Java

    libmisc.so
  19. # 2. Generate a lib with ndk-build $ ndk-build Compile

    thumb : misc <= misc.c StaticLibrary : libmisc.a SharedLibrary : libmisc.so Install : libmisc.so => libs/armeabi/libmisc.so ... 2/3 Build native lib
  20. 3/3 Java ext Java JNI C code C Java libmisc.so

  21. /* 3. Expose the logic via a native extension */

    package com.example; public class Foo extends /* ... */ { static { System.loadLibrary("misc"); } public native String bar(); } 3/3 Java ext
  22. Basic Java JNI C code C Java libmisc.so

  23. Advanced Java JNI libA.a C Java libmisc.so A sources C

    code libB.a B sources 3rd party libs
  24. - Building 3rd party libs -

  25. Pro-tip Use the Standalone Toolchain.

  26. What? A customized install for a given platform, arch. handy

  27. Why? No need to write specific Android.mk makefiles: reuse existing

    build systems. 0 LOC Makefile autotools
  28. How?

  29. Makefile [Ex. 1] jsmn JSON C parser

  30. [Ex. 1] jsmn 1/3 override Makefile variables

  31. [Ex. 1] jsmn 2/3

  32. [Ex. 1] jsmn 3/3 choose the proper arch... and that’s

    it! Full gist @ http://git.io/ndk-jsmn
  33. [Ex. 2] msgpack autotools binary C/C+ serialization

  34. [Ex. 2] msgpack 1/3

  35. [Ex. 2] msgpack 2/3

  36. [Ex. 2] msgpack 3/3 Full gist @ http://git.io/ndk-msgpack use the

    cross-toolchains... and that’s it!
  37. - Using a prebuilt library -

  38. Overview Android.mk Application.mk ndk-build armeabi/ libfoo.so libs/ single arch

  39. Overview Android.mk Application.mk armeabi-v7a/ libfoo.so ndk-build armeabi/ libfoo.so libs/ fat

    binary
  40. - Loading a native library -

  41. Basic Pick the right arch vs. target device

  42. Medium

  43. Custom frontal library native method (frontal lib.) Pick the right

    arch yourself!
  44. Custom: why? [2] Bypass a loadLibrary bug on ICS! [1]

    Properly target ARMv7 without NEON CPUs when getCpuFeatures() can’t be used at runtime (e.g. 3rd party libs) [2] see http://www.moodstocks.com/2012/03/20/ice-cream-sandwich-why-native-code-support- sucks [1] e.g. «the NVidia Tegra 2 generation SoC has a dual-core ARM Cortex-A9 CPU (lacking ARM's advanced SIMD extension—NEON)» - see http://en.wikipedia.org/wiki/Tegra
  45. - Quick JNI hints -

  46. C pointer = int field e.g. persist a DB handle

    and use it throughout the JNI extensions Don’t forget to destruct it (explicitly or at finalize() time)
  47. C error codes C error code & string

  48. Resources #AltDevBlogADay NDK Part 1 & Part 2 Linux Mag.,

    07/11 VLC for Android Tokyo Cabinet Java Pkg
  49. Thanks! Questions? Comments? cedric@moodstocks.com | @deltheil