running native code on android #osdcfr 2012

Post on 18-Nov-2014

692 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Running native code on Android

OSDC.fr 2012-10-13 Cédric @deltheil

C NDK ARM

- Why using native code? -

1. Reuse some C code

2. Use 3rd party libraries

3. Create portable components

iOSAndroid ...

4. Use hardware acceleration

CPUSIMD algorithm

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

...but How?

Native Development Kit

- Android NDK overview -

Downloadshttp://developer.android.com/tools/sdk/ndk/index.html

cross-platform

Contents1. Tools for Linux,OS X,Win to cross-compile native ARM,x86,MIPS binaries.

Contents2. System files and headers for Android native APIs.

w/ Java Native Interface (a.k.a JNI) <jni.h>

Contents3. Easy-to-use build system: Android.mk & ndk-build (make wrapper).

Contents4. Documentation andcode samples.

- Steps to embed C code -

1/3 Wrap CJava

JNI

C code

C

Java

libmisc.so

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

2/3 Build native libJava

JNI

C code

C

Java

libmisc.so

# 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

3/3 Java extJava

JNI

C code

C

Java

libmisc.so

/* 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

BasicJava

JNI

C code

C

Java

libmisc.so

AdvancedJava

JNI

libA.a

C

Java

libmisc.so

A sources

C code

libB.a B sources

3rd party libs

- Building 3rd party libs -

Pro-tip

Use the Standalone Toolchain.

What?

A customized install for a given platform, arch.

handy

Why?

No need to write specific Android.mk makefiles: reuse existing build systems.

0 LOC Makefile autotools

How?

Makefile

[Ex. 1] jsmn

JSONC parser

[Ex. 1] jsmn 1/3

override Makefile variables

[Ex. 1] jsmn 2/3

[Ex. 1] jsmn 3/3

choose the proper arch... and that’s it!

Full gist @ http://git.io/ndk-jsmn

[Ex. 2] msgpack

autotools binaryC/C+ serialization

[Ex. 2] msgpack 1/3

[Ex. 2] msgpack 2/3

[Ex. 2] msgpack 3/3

Full gist @ http://git.io/ndk-msgpack

use the cross-toolchains... and that’s it!

- Using a prebuilt library -

OverviewAndroid.mk

Application.mk

ndk-buildarmeabi/libfoo.so

libs/

single arch

OverviewAndroid.mk

Application.mk

armeabi-v7a/libfoo.so

ndk-buildarmeabi/libfoo.so

libs/

fat binary

- Loading a native library -

Basic

Pick the right arch vs.

target device

Medium

Custom

frontal library

native method (frontal lib.)

Pick the right arch yourself!

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

- Quick JNI hints -

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)

C error codes

C error code & string

Thanks!

Questions? Comments? cedric@moodstocks.com | @deltheil

top related