Running native code on Android #OSDCfr 2012
-
Upload
cedric-deltheil -
Category
Documents
-
view
693 -
download
4
description
Transcript of Running native code on Android #OSDCfr 2012
![Page 1: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/1.jpg)
Running native code on Android
OSDC.fr 2012-10-13 Cédric @deltheil
C NDK ARM
![Page 2: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/2.jpg)
- Why using native code? -
![Page 3: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/3.jpg)
1. Reuse some C code
![Page 4: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/4.jpg)
2. Use 3rd party libraries
![Page 5: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/5.jpg)
3. Create portable components
iOSAndroid ...
![Page 6: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/6.jpg)
4. Use hardware acceleration
CPUSIMD algorithm
![Page 7: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/7.jpg)
5. ... because it’s fun :)
![Page 8: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/8.jpg)
...but How?
Native Development Kit
![Page 9: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/9.jpg)
- Android NDK overview -
![Page 10: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/10.jpg)
Downloadshttp://developer.android.com/tools/sdk/ndk/index.html
cross-platform
![Page 11: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/11.jpg)
Contents1. Tools for Linux,OS X,Win to cross-compile native ARM,x86,MIPS binaries.
![Page 12: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/12.jpg)
Contents2. System files and headers for Android native APIs.
w/ Java Native Interface (a.k.a JNI) <jni.h>
![Page 13: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/13.jpg)
Contents3. Easy-to-use build system: Android.mk & ndk-build (make wrapper).
![Page 14: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/14.jpg)
Contents4. Documentation andcode samples.
![Page 15: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/15.jpg)
- Steps to embed C code -
![Page 16: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/16.jpg)
1/3 Wrap CJava
JNI
C code
C
Java
libmisc.so
![Page 17: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/17.jpg)
1/3 Wrap C
/* 1. Wrap your C code with the JNI */
#include <jni.h>
jstringJava_com_example_Foo_bar(JNIEnv* env, jobject thiz) { char buffer[512]; /* ... */ return (*env)->NewStringUTF(env, buffer);}
namespace class method instancenative interface
![Page 18: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/18.jpg)
2/3 Build native libJava
JNI
C code
C
Java
libmisc.so
![Page 19: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/19.jpg)
# 2. Generate a lib with ndk-build
$ ndk-build Compile thumb : misc <= misc.cStaticLibrary : libmisc.aSharedLibrary : libmisc.soInstall : libmisc.so => libs/armeabi/libmisc.so...
2/3 Build native lib
![Page 20: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/20.jpg)
3/3 Java extJava
JNI
C code
C
Java
libmisc.so
![Page 21: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/21.jpg)
/* 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
![Page 22: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/22.jpg)
BasicJava
JNI
C code
C
Java
libmisc.so
![Page 23: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/23.jpg)
AdvancedJava
JNI
libA.a
C
Java
libmisc.so
A sources
C code
libB.a B sources
3rd party libs
![Page 24: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/24.jpg)
- Building 3rd party libs -
![Page 25: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/25.jpg)
Pro-tip
Use the Standalone Toolchain.
![Page 26: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/26.jpg)
What?
A customized install for a given platform, arch.
handy
![Page 27: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/27.jpg)
Why?
No need to write specific Android.mk makefiles: reuse existing build systems.
0 LOC Makefile autotools
![Page 28: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/28.jpg)
How?
![Page 29: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/29.jpg)
Makefile
[Ex. 1] jsmn
JSONC parser
![Page 30: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/30.jpg)
[Ex. 1] jsmn 1/3
override Makefile variables
![Page 31: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/31.jpg)
[Ex. 1] jsmn 2/3
![Page 32: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/32.jpg)
[Ex. 1] jsmn 3/3
choose the proper arch... and that’s it!
Full gist @ http://git.io/ndk-jsmn
![Page 33: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/33.jpg)
[Ex. 2] msgpack
autotools binaryC/C+ serialization
![Page 34: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/34.jpg)
[Ex. 2] msgpack 1/3
![Page 35: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/35.jpg)
[Ex. 2] msgpack 2/3
![Page 36: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/36.jpg)
[Ex. 2] msgpack 3/3
Full gist @ http://git.io/ndk-msgpack
use the cross-toolchains... and that’s it!
![Page 37: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/37.jpg)
- Using a prebuilt library -
![Page 38: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/38.jpg)
OverviewAndroid.mk
Application.mk
ndk-buildarmeabi/libfoo.so
libs/
single arch
![Page 39: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/39.jpg)
OverviewAndroid.mk
Application.mk
armeabi-v7a/libfoo.so
ndk-buildarmeabi/libfoo.so
libs/
fat binary
![Page 40: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/40.jpg)
- Loading a native library -
![Page 41: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/41.jpg)
Basic
Pick the right arch vs.
target device
![Page 42: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/42.jpg)
Medium
![Page 43: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/43.jpg)
Custom
frontal library
native method (frontal lib.)
Pick the right arch yourself!
![Page 44: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/44.jpg)
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
![Page 45: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/45.jpg)
- Quick JNI hints -
![Page 46: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/46.jpg)
C pointer = int fielde.g. persist a DB handle and use it throughout the JNI extensions
Don’t forget to destruct it (explicitly or at finalize() time)
![Page 47: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/47.jpg)
C error codes
C error code & string
![Page 48: Running native code on Android #OSDCfr 2012](https://reader034.fdocuments.us/reader034/viewer/2022051108/546c9650af79596c298b5047/html5/thumbnails/48.jpg)
Resources#AltDevBlogADay NDK Part 1 & Part 2 Linux Mag., 07/11
VLC for AndroidTokyo Cabinet Java Pkg