SlideShare a Scribd company logo
Introduction to
the Android NDK

Sebastian Mauer
GDG Aachen

CodeFest Karlsruhe
February 20th, 2014
Who am I?
Sebastian Mauer

GDG Aachen Co-Lead
Software Engineer

CS Student
I don’t work for Google…yet
Part I:
NDK? What is that?
Android Platforms

MIPS

ARM

x86
Two kinds of Apps

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

Native Apps
(created using the 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
It just works™
•

The DalvikVM is already optimized for the x86
Platform

•

Apps relying on the Android SDK / Dalvik Bytecode

will automatically benefit from Platform-specific
Optimizations (like SSE & Co.)
From Source to Bytecode
Java
Sourcecode
Java
Bytecode (.class)
Dalvik
Bytecode (.dex)

JAR Archive

Dalvik
VM

Java
VM
Android VM
Dalvik VM

App

App

App

App

App

App

Linux Kernel
Hardware
ONE DOES NOT SIMPLY

RUN CODE ON MULTIPLE
PLATFORMS
NOT SURE IF NDK CAN HELP ME
RUN CODE FASTER

OR MAKE THINGS EVEN MORE
COMPLICATED
Part II:
Going native. The 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
But why?
•

Performance

e.g., complex algorithms, multimedia applications, games

•

Differentiation 

app that takes advantage of direct CPU/HW access

e.g., using SSSE3 for optimization

•

Fluid and lag-free animations

•

Software code reuse
Why not?
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
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.
NDK Development in a Nutshell
C/C++Code

Makefile

ndk-build

APP_ABI := all
or APP_ABI := x86

SDK APIs

Java* calls

GDB debug

through jni

Android* Applications

Java Framework

Java Application

JNI
Native Libs
Bionic C Library

NDK APIs
NDK Anatomy
Dalvik VM

Dalvik Bytecode
Java Native
Interface
(JNI)

NDK compiled binary
Native Platform
Compatibility with Standard C/C++
•

Bionic C Library:

Lighter than standard GNU C Library

Not POSIX compliant

pthread support included, but limited

No 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
Pick One
•

By default, libstdc++ is used. It lacks: 

Standard C++ Library support (except some headers)

C++ exceptions support

RTTI support

•

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

Exceptions

RTTI

STL

system

No

No

No

gabi++

Yes

Yes

No

stlport

Yes

Yes

Yes

gnustl

Yes

Yes

Yes
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

Put APP_ABI=all inside
Application.mk
Run ndk-build…
ARM v7a libs are built
ARM v5 libs are built
x86 libs are built
mips libs are built

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
Fat Binaries
libs/armeabi

Use lib/armeabi
libraries

libs/armeabi-v7a

libs/x86

…
APK file

Use lib/armeabi-v7a
libraries

Use lib/x86 libraries
Q&A
Thanks for your attention.

More Related Content

PDF
NDK Programming in Android
PPTX
Native development kit (ndk) introduction
PDF
Android Native Development Kit
PPTX
NDK Introduction
PDF
Android NDK and the x86 Platform
PDF
Android ndk: Entering the native world
PDF
Introduction to the Android NDK
PDF
Using the Android Native Development Kit (NDK)
NDK Programming in Android
Native development kit (ndk) introduction
Android Native Development Kit
NDK Introduction
Android NDK and the x86 Platform
Android ndk: Entering the native world
Introduction to the Android NDK
Using the Android Native Development Kit (NDK)

What's hot (20)

PDF
Using the Android Native Development Kit (NDK)
PPTX
Android ndk
PPTX
PPTX
Using the android ndk - DroidCon Paris 2014
PDF
Android NDK: Entrando no Mundo Nativo
PPTX
Android ndk - Introduction
PPT
Android Developer Meetup
PPTX
How to implement a simple dalvik virtual machine
PDF
PIC your malware
PDF
How to reverse engineer Android applications
PPT
Reverse Engineering Android Application
PPTX
Reverse engineering android apps
PPTX
Google ART (Android RunTime)
PDF
LinkedIn - Disassembling Dalvik Bytecode
PDF
Deep Dive into the AOSP
PDF
How to build a tool for operating Flink on Kubernetes
PDF
Toward dynamic analysis of obfuscated android malware
PDF
Understanding the Dalvik bytecode with the Dedexer tool
PDF
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
PDF
Formacion en movilidad: Conceptos de desarrollo en iOS (I)
Using the Android Native Development Kit (NDK)
Android ndk
Using the android ndk - DroidCon Paris 2014
Android NDK: Entrando no Mundo Nativo
Android ndk - Introduction
Android Developer Meetup
How to implement a simple dalvik virtual machine
PIC your malware
How to reverse engineer Android applications
Reverse Engineering Android Application
Reverse engineering android apps
Google ART (Android RunTime)
LinkedIn - Disassembling Dalvik Bytecode
Deep Dive into the AOSP
How to build a tool for operating Flink on Kubernetes
Toward dynamic analysis of obfuscated android malware
Understanding the Dalvik bytecode with the Dedexer tool
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
Formacion en movilidad: Conceptos de desarrollo en iOS (I)
Ad

Similar to Introduction to the Android NDK (20)

PPTX
Advance Android Application Development
PPTX
Getting started with the NDK
PPTX
PDF
Running native code on Android #OSDCfr 2012
PDF
Getting Native with NDK
PDF
Native code in Android applications
PDF
[Android Codefest Germany] Adding x86 target to your Android app by Xavier Ha...
PDF
Android Internals (This is not the droid you’re loking for...)
PPT
Native Android for Windows Developers
PDF
International Journal of Engineering Research and Development
PPTX
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
PPTX
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
PPTX
Alternatives to Java for Android development
PDF
Android on IA devices and Intel Tools
PPTX
Android NDK Intro
PDF
Developing and-benchmarking-native-linux-applications-on-android
PPT
Alternatives to Java for Android development
PDF
Using the NDK and Renderscript
PDF
Android Internals
Advance Android Application Development
Getting started with the NDK
Running native code on Android #OSDCfr 2012
Getting Native with NDK
Native code in Android applications
[Android Codefest Germany] Adding x86 target to your Android app by Xavier Ha...
Android Internals (This is not the droid you’re loking for...)
Native Android for Windows Developers
International Journal of Engineering Research and Development
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Alternatives to Java for Android development
Android on IA devices and Intel Tools
Android NDK Intro
Developing and-benchmarking-native-linux-applications-on-android
Alternatives to Java for Android development
Using the NDK and Renderscript
Android Internals
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Advanced methodologies resolving dimensionality complications for autism neur...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
20250228 LYD VKU AI Blended-Learning.pptx

Introduction to the Android NDK

  • 1. Introduction to the Android NDK Sebastian Mauer GDG Aachen CodeFest Karlsruhe February 20th, 2014
  • 2. Who am I? Sebastian Mauer GDG Aachen Co-Lead Software Engineer CS Student I don’t work for Google…yet
  • 3. Part I: NDK? What is that?
  • 5. Two kinds of Apps Apps in the DalvikVM (that’s the kind of apps you probably know) Native Apps (created using the NDK)
  • 6. 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
  • 7. It just works™ • The DalvikVM is already optimized for the x86 Platform • Apps relying on the Android SDK / Dalvik Bytecode
 will automatically benefit from Platform-specific Optimizations (like SSE & Co.)
  • 8. From Source to Bytecode Java Sourcecode Java Bytecode (.class) Dalvik Bytecode (.dex) JAR Archive Dalvik VM Java VM
  • 10. ONE DOES NOT SIMPLY RUN CODE ON MULTIPLE PLATFORMS
  • 11. NOT SURE IF NDK CAN HELP ME RUN CODE FASTER OR MAKE THINGS EVEN MORE COMPLICATED
  • 13. 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
  • 14. But why? • Performance
 e.g., complex algorithms, multimedia applications, games • Differentiation 
 app that takes advantage of direct CPU/HW access
 e.g., using SSSE3 for optimization • Fluid and lag-free animations • Software code reuse
  • 17. 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
  • 19. 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.
  • 20. NDK Development in a Nutshell C/C++Code Makefile ndk-build APP_ABI := all or APP_ABI := x86 SDK APIs Java* calls GDB debug through jni Android* Applications Java Framework Java Application JNI Native Libs Bionic C Library NDK APIs
  • 21. NDK Anatomy Dalvik VM Dalvik Bytecode Java Native Interface (JNI) NDK compiled binary Native Platform
  • 22. Compatibility with Standard C/C++ • Bionic C Library:
 Lighter than standard GNU C Library
 Not POSIX compliant
 pthread support included, but limited
 No 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
  • 23. Pick One • By default, libstdc++ is used. It lacks: 
 Standard C++ Library support (except some headers)
 C++ exceptions support
 RTTI support • Fortunately, you have other libs available with the NDK: Runtime Exceptions RTTI STL system No No No gabi++ Yes Yes No stlport Yes Yes Yes gnustl Yes Yes Yes
  • 25. 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 Put APP_ABI=all inside Application.mk Run ndk-build… ARM v7a libs are built ARM v5 libs are built x86 libs are built mips libs are built 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
  • 26. Fat Binaries libs/armeabi Use lib/armeabi libraries libs/armeabi-v7a libs/x86 … APK file Use lib/armeabi-v7a libraries Use lib/x86 libraries
  • 27. Q&A
  • 28. Thanks for your attention.