introduction to the android ndk

28
Introduction to the Android NDK Sebastian Mauer GDG Aachen CodeFest Karlsruhe February 20th, 2014

Upload: sebastian-mauer

Post on 18-Nov-2014

932 views

Category:

Technology


1 download

DESCRIPTION

Introduction to the Android NDK and why you should care about native development on Android. Presented at CodeFest Karlsruhe 2014

TRANSCRIPT

Page 1: Introduction to the Android NDK

Introduction to the Android NDK

Sebastian Mauer GDG Aachen

CodeFest Karlsruhe February 20th, 2014

Page 2: Introduction to the Android NDK

Who am I?Sebastian MauerGDG Aachen Co-Lead CS StudentSoftware Engineer I don’t work for Google…yet

Page 3: Introduction to the Android NDK

Part I: NDK? What is that?

Page 4: Introduction to the Android NDK

Android Platforms

MIPS x86ARM

Page 5: Introduction to the Android NDK

Two kinds of Apps

Apps in the DalvikVM(that’s the kind of apps you probably know)

Native Apps(created using the NDK)

Page 6: Introduction to the Android NDK

One VM to rule them all

• Dalvik is a Virtual Machine (VM)

• A VM is a common abstraction across different hardware platforms

• „Translates“ VM Bytecode to platform specific instructions

Page 7: Introduction to the Android NDK

It just works™

• The DalvikVM is already optimized for the x86 Platform

• Apps relying on the Android SDK / Dalvik Bytecodewill automatically benefit from Platform-specific Optimizations (like SSE & Co.)

Page 8: Introduction to the Android NDK

From Source to BytecodeJava

Sourcecode

Java Bytecode (.class)

Dalvik Bytecode (.dex)

JAR Archive

Dalvik VM

Java VM

Page 9: Introduction to the Android NDK

Android VM

Hardware

Linux Kernel

Dalvik VM App

App

App

App

App

App

Page 10: Introduction to the Android NDK

ONE DOES NOT SIMPLY

RUN CODE ON MULTIPLE PLATFORMS

Page 11: Introduction to the Android NDK

NOT SURE IF NDK CAN HELP ME RUN CODE FASTER

OR MAKE THINGS EVEN MORE COMPLICATED

Page 12: Introduction to the Android NDK

Part II: Going native. The NDK.

Page 13: Introduction to the Android NDK

What’s the NDK?• NDK stands for Native Development Kit

• Allows to compile C/C++ code to native (read: platform specific) executables/libraries.

• Build scripts/toolkit to incorporate native code in Android apps via the Java Native Interface (JNI)

• Has to be compiled for every platform you want to support

Page 14: Introduction to the Android NDK

But why?

• Performancee.g., complex algorithms, multimedia applications, games

• Differentiation app that takes advantage of direct CPU/HW accesse.g., using SSSE3 for optimization

• Fluid and lag-free animations

• Software code reuse

Page 15: Introduction to the Android NDK

Why not?

Page 16: Introduction to the Android NDK
Page 17: Introduction to the Android NDK

What could possibly go wrong?

• Performance improvements are not guaranteed

• In fact, you could make it worse (read: slower).

• Added complexity (Java/C++ Interop, Multiple platforms)

• Somewhat harder to debug

Page 18: Introduction to the Android NDK
Page 19: Introduction to the Android NDK

What’s an NDK app?It’s an Android application that uses native libraries. !Libraries are .so files, usually found inside libs/CPU_ABI/. !These libs can be generated from native sources inside jni folder, game engines, or required by other 3rd party libraries. !There is no 100% native application. Even an application purely written in C/C++, using native_app_glue.h, will be executed in the context of the Dalvik Virtual Machine.

Page 20: Introduction to the Android NDK

NDK Development in a NutshellC/C++Code Makefile ndk-build Java* calls GDB debug

Java Framework

Java Application

SDK APIs

JNI

Native Libs

Android* Applications

NDK APIs

Bionic C Library

APP_ABI := all or APP_ABI := x86

through jni

Page 21: Introduction to the Android NDK

NDK Anatomy

Native Platform

Dalvik VMDalvik Bytecode

NDK compiled binary

Java Native Interface

(JNI)

Page 22: Introduction to the Android NDK

Compatibility with Standard C/C++

• Bionic C Library: Lighter than standard GNU C LibraryNot POSIX compliantpthread support included, but limitedNo System-V IPCs Access to Android* system properties

• Bionic is not binary-compatible with the standard C library

• It means you generally need to (re)compile everything using the Android NDK toolchain

Page 23: Introduction to the Android NDK

Pick One

Runtime Exceptions RTTI STL

system No No No

gabi++ Yes Yes No

stlport Yes Yes Yes

gnustl Yes Yes Yes

• By default, libstdc++ is used. It lacks: Standard C++ Library support (except some headers) C++ exceptions supportRTTI support

• Fortunately, you have other libs available with the NDK:

Page 24: Introduction to the Android NDK
Page 25: Introduction to the Android NDK

Compile for all the platforms.If you have the source code of your native libraries, you can compile it for several CPU architectures by setting APP_ABI to all in the Makefile “jni/Application.mk”:! APP_ABI=all

The NDK will generate optimized code for all target ABIs You can also pass APP_ABI variable directly to ndk-build, and specify each ABI: ndk-build APP_ABI=x86

ARM v7a libs are built

ARM v5 libs are built x86 libs are built

mips libs are built

Put APP_ABI=all inside Application.mkRun ndk-build…

Page 26: Introduction to the Android NDK

Fat Binaries

Use lib/armeabi libraries

Use lib/armeabi-v7a libraries

Use lib/x86 libraries

libs/armeabi-v7a

libs/x86

libs/armeabi

APK file

Page 27: Introduction to the Android NDK

Q&A

Page 28: Introduction to the Android NDK

Thanks for your attention.