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

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

Cédric Deltheil

October 13, 2012
Tweet

More Decks by Cédric Deltheil

Other Decks in Programming

Transcript

  1. Contents 2. System files and headers for Android native APIs.

    w/ Java Native Interface (a.k.a JNI) <jni.h>
  2. 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
  3. # 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
  4. /* 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
  5. Advanced Java JNI libA.a C Java libmisc.so A sources C

    code libB.a B sources 3rd party libs
  6. [Ex. 1] jsmn 3/3 choose the proper arch... and that’s

    it! Full gist @ http://git.io/ndk-jsmn
  7. 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
  8. 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)
  9. Resources #AltDevBlogADay NDK Part 1 & Part 2 Linux Mag.,

    07/11 VLC for Android Tokyo Cabinet Java Pkg