SlideShare a Scribd company logo
Fullstack as a service
Getting Started with the NDK
Native Development Kit (NDK)
Kirill Kounik
Agenda
Introduction: What/Why NDK
ABIs - Architectures and CPUs
NDK and Project setup
Libraries
Debugging
Native Development for Android
Natural/native language of Android development is Java
Android runs Linux kernel at its core
C/C++ development is possible, actually an Android application can be written
entirely in C or C++
Leverages standard Java Native Interface (JNI)
However it will not benefit most apps
Benefits of Native Development for Android
• Mostly useful for computation intensive apps where performance becomes
an issue
• May help to port or share code with other platforms, like iOS
• Use external native libraries
• Sometimes allows access to APIs not exposed in Java layer
NDK development Cons:
Requires compilation for every CPU architecture and, possibly, a separate APK
for each architecture.
May significantly increase APK size
JNI development is cumbersome for Java developers
Native Development Kit or NDK for Android
NDK is a set of tools or toolchain used for for native development for Android
platform.
http://developer.android.com/ndk/index.html
Samples*
https://github.com/googlesamples/android-ndk
ABI is Application Binary Interface
Different Android handsets use different CPUs, which in turn support different
instruction sets. Each combination of CPU and instruction sets has its own
Application Binary Interface, or ABI.
armeabi
armeabi-v7a
arm64-v8a
x86
x86_64
mips
mips64
APK structure with native code
$ unzip -l build/outputs/apk/app-all-debug.apk
Archive: build/outputs/apk/app-all-debug.apk
Length Date Time Name
--------- ---------- ----- ----
1896 03-31-2016 14:10 AndroidManifest.xml
...
2724 03-31-2016 14:10 classes.dex
5592 03-31-2016 14:10 lib/arm64-v8a/libhello-jni.so
13552 03-31-2016 14:10 lib/armeabi/libhello-jni.so
13560 03-31-2016 14:10 lib/armeabi-v7a/libhello-jni.so
5460 03-31-2016 14:10 lib/mips/libhello-jni.so
6048 03-31-2016 14:10 lib/mips64/libhello-jni.so
5264 03-31-2016 14:10 lib/x86/libhello-jni.so
5784 03-31-2016 14:10 lib/x86_64/libhello-jni.so
...
--------- -------
81286 17 files
NDK and project setup
NDK setup
NDK setup is very straight-forward:
[Install Android Studio]
Download and unzip NDK into a convenient location or let Android Studio do it
for you
Optionally get the samples from Github, they are not included in the NDK
https://github.com/googlesamples/android-ndk *
* Look for various branches corresponding to build methods
Several ways to build native code
• Using ndk-build or cmake and externalNativeBuild { } block in build.gradle
(starting with Android Studio 2.2)
Use if you are porting older or very complex project or familiar with GNU make or cmake
• Using experimental Gradle plugin – new build system
Recommended for new projects, doesn’t require external build tool knowledge
• Use toolchain directly
Use you favorite build tools
External Native Build – ndk-build
• Define Android.mk file location
• Pass optional ndk-build parameters
• Define required ABIs
externalNativeBuild {
ndkBuild {
path “Android.mk”
abiFilters 'armeabi-v7a‘, 'arm64-v8a', 'x86', 'x86_64'
arguments 'NDK_DEBUG=0', '-j4', 'ANDROID_TOOLCHAIN=clang'
}
}
Experimental plugin - Project structure
Starting from version 1.3 Android Studio supports NDK development with the
relatively new 'com.android.model.application' gradle plugin
.C, .CPP files under jni folder at the same level as java
*Old toolchain based on makefiles and ndk-build is not covered by this training.
Project structure
Build settings defined in android.ndk section of the build.gradle file, for
example
model {
...
android.ndk {
moduleName = "native-codec-jni"
cppFlags.add("-UNDEBUG")
// for native multimedia
ldLibs.addAll(["OpenMAXAL", "mediandk"])
// for logging
ldLibs.add("log")
// for native windows
ldLibs.add("android")
stl = "stlport_static"
}
...
}
Everything you can configure in android.ndk
android.ndk {
// All configurations that can be changed in android.ndk.
moduleName = "mymodule"
ldLibs.addAll(['log', 'android'])
ldLibs.add("log")
ldFlags.add("-L/custom/lib/path")
...
}
Everything you can configure in android.ndk 2
android.ndk {
// All configurations that can be changed in android.ndk.
...
abiFilters.add("x86") // List of target ABIs
CFlags.add("-DCUSTOM_DEFINE")
cppFlags.add("-DCUSTOM_DEFINE")
debuggable = false
renderscriptNdkMode = false
stl = "stlport_static" // choice of c++ runtimes provided
platformVersion = 15
}
● http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-Ndk-Integration
Define what ABIs to package in the APK
Or Split APKs
In native code causes an APK too large. To build separate APK for each
architecture
android {
...
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a', 'mips' // ABIs to include
universalApk true // build “fat” version
}
}
}
Split APKs - version code support
Every APK in Play store must have unique versionCode
// map for the version code
project.ext.versionCodes = ['armeabi-v7a':1, 'x86':2, 'mips':3]
applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) * 10000
+ variant.versionCode
}
}
* http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
C++ runtimes (stl = "stlport_static")
the build system automatically links the standard C libraries, real-time
extensions, and pthread
By default NDK provides a very minimal standard C++ runtime support library
(libstdc++). This minimal support does not include, for example:
○ Standard C++ Library support (except a few trivial headers).
○ C++ exceptions support
○ RTTI support
Available runtimes: GAbi++, STLport, GNU STL, LLVM libc++
Detailed information about various available runtimes and supplied headers can
be found in the docs: developer.android.com/ndk/guides/cpp-support.html
Stable NDK libraries (ldLibs.addAll(['log', 'android']))
The Android NDK provides a set of native headers for prebuilt libraries that allow
access various system features without the need to go through JNI.
Library Description Header files
log Android log support log.h
z ZLib compression library zlib.h, zconf.h
dl Dynamic linker library dlfcn.h
GLESv1_CM, GLESv2,
OpenSLES, EGL, GLESv3, 3.1
Open GL ES, EGL, libraries for various versions many
android For writing pure native apps many
OpenMAXAL Android native multimedia handling is based on
Khronos Group OpenMAX AL
OMXAL/OpenMAXAL.h,
OMXAL/OpenMAXAL_Platform.h,
OpenMAXAL_Android.h
jnigraphics Native interface to the pixel buffers of bitmaps bitmap.h
Pure Native Activity
android.app.NativeActivity is a glue between Android and and pure native
Activity
No need to subclass it, only properly define in the manifest
Android framework APIs can be accessed through the JNI
Native code should adhere to certain structure defined by NDK in
native_activity.h
Alternatively use helper library defined in android_native_app_glue.h
Implement native function ANativeActivity_onCreate
Implement ANativeActivity->callbacks to manage activity lifecycle
Pure native activity manifest
<application android:label="@string/app_name" android:hasCode="false">
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="native-activity" />
...
</activity>
</application>
● Sample: https://github.com/googlesamples/android-ndk/tree/master/native-activity
Debugging JNI code
In the latest releases of Android Studio native debugger is nicely integrated and
can be used out of the box
Debugging JNI crashes
Tombstone files are crash dumps with some extra information
Up to 10 last tombstone files saved by the system and replaced cyclically
$ adb shell ls /data/tombstones
$ adb pull /data/tombstones/tombstone_00
Examining tombstone files with ndk-stack
Identify crash address in your library
$ ndk-stack -sym <root symbols dir> [-dump <dump file>]
$ ndk-stack -sym .appbuildintermediatessymbols -dump .tombstone_00
Finding file location with addr2line
addr2line command that you need to use depends on the ABI of your crashed file.
alias addr2line= 
’$NDK/toolchains/x86_64-4.9/prebuilt/windows/bin/i686-linux-android-
addr2line.exe’
Find the crashed line
$ addr2line -f -e <input file> <address>
Summary
JNIEnv*, Local/global refs, method signatures, javah, javap,
tombstones, ndk-stack, addr2line
Q & A

More Related Content

What's hot

Migrating from MFC to Qt
Migrating from MFC to QtMigrating from MFC to Qt
Migrating from MFC to Qt
GlobalLogic Ukraine
 
Kubernetes Operators: Rob Szumski
Kubernetes Operators: Rob SzumskiKubernetes Operators: Rob Szumski
Kubernetes Operators: Rob Szumski
Redis Labs
 
CI/CD Development in Kubernetes - Skaffold
CI/CD Development in Kubernetes -  SkaffoldCI/CD Development in Kubernetes -  Skaffold
CI/CD Development in Kubernetes - Skaffold
Suman Chakraborty
 
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
Embarcadero Technologies
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
IJERD Editor
 
Os Rego
Os RegoOs Rego
Os Rego
oscon2007
 
Serverless containers … with source-to-image
Serverless containers  … with source-to-imageServerless containers  … with source-to-image
Serverless containers … with source-to-image
Josef Adersberger
 
Peering Inside the Black Box: A Case for Observability
Peering Inside the Black Box: A Case for ObservabilityPeering Inside the Black Box: A Case for Observability
Peering Inside the Black Box: A Case for Observability
VMware Tanzu
 
Making a Headless Android Device (Oslo Embedded Meetup 2018)
Making a Headless Android Device (Oslo Embedded Meetup 2018)Making a Headless Android Device (Oslo Embedded Meetup 2018)
Making a Headless Android Device (Oslo Embedded Meetup 2018)
Patricia Aas
 
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
mfrancis
 
Making a Headless Android Device
Making a Headless Android DeviceMaking a Headless Android Device
Making a Headless Android Device
Patricia Aas
 
Effective Spring on Kubernetes
Effective Spring on KubernetesEffective Spring on Kubernetes
Effective Spring on Kubernetes
Neven Cvetković
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
dotCloud
 
Automating stateful applications with kubernetes operators - Openstack Summit...
Automating stateful applications with kubernetes operators - Openstack Summit...Automating stateful applications with kubernetes operators - Openstack Summit...
Automating stateful applications with kubernetes operators - Openstack Summit...
Jorge Morales
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservices
Luram Archanjo
 
A basic overview of Containers
A basic overview of ContainersA basic overview of Containers
A basic overview of Containers
Divakar Sharma
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
account inactive
 
Os Lattner
Os LattnerOs Lattner
Os Lattner
oscon2007
 
Introduction to Eclipse Che / EclipseCon 2014
Introduction to Eclipse Che / EclipseCon 2014Introduction to Eclipse Che / EclipseCon 2014
Introduction to Eclipse Che / EclipseCon 2014
Florent BENOIT
 
OpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-SideOpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-Side
Tim Burks
 

What's hot (20)

Migrating from MFC to Qt
Migrating from MFC to QtMigrating from MFC to Qt
Migrating from MFC to Qt
 
Kubernetes Operators: Rob Szumski
Kubernetes Operators: Rob SzumskiKubernetes Operators: Rob Szumski
Kubernetes Operators: Rob Szumski
 
CI/CD Development in Kubernetes - Skaffold
CI/CD Development in Kubernetes -  SkaffoldCI/CD Development in Kubernetes -  Skaffold
CI/CD Development in Kubernetes - Skaffold
 
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
Android on Windows 11 - A Developer's Perspective (Windows Subsystem For Andr...
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Os Rego
Os RegoOs Rego
Os Rego
 
Serverless containers … with source-to-image
Serverless containers  … with source-to-imageServerless containers  … with source-to-image
Serverless containers … with source-to-image
 
Peering Inside the Black Box: A Case for Observability
Peering Inside the Black Box: A Case for ObservabilityPeering Inside the Black Box: A Case for Observability
Peering Inside the Black Box: A Case for Observability
 
Making a Headless Android Device (Oslo Embedded Meetup 2018)
Making a Headless Android Device (Oslo Embedded Meetup 2018)Making a Headless Android Device (Oslo Embedded Meetup 2018)
Making a Headless Android Device (Oslo Embedded Meetup 2018)
 
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
Workflow for Development, Release and Versioning with OSGi / bndtools- Real W...
 
Making a Headless Android Device
Making a Headless Android DeviceMaking a Headless Android Device
Making a Headless Android Device
 
Effective Spring on Kubernetes
Effective Spring on KubernetesEffective Spring on Kubernetes
Effective Spring on Kubernetes
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
 
Automating stateful applications with kubernetes operators - Openstack Summit...
Automating stateful applications with kubernetes operators - Openstack Summit...Automating stateful applications with kubernetes operators - Openstack Summit...
Automating stateful applications with kubernetes operators - Openstack Summit...
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservices
 
A basic overview of Containers
A basic overview of ContainersA basic overview of Containers
A basic overview of Containers
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
 
Os Lattner
Os LattnerOs Lattner
Os Lattner
 
Introduction to Eclipse Che / EclipseCon 2014
Introduction to Eclipse Che / EclipseCon 2014Introduction to Eclipse Che / EclipseCon 2014
Introduction to Eclipse Che / EclipseCon 2014
 
OpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-SideOpenAPI and gRPC Side by-Side
OpenAPI and gRPC Side by-Side
 

Similar to Getting started with the NDK

Android NDK
Android NDKAndroid NDK
Android ndk
Android ndkAndroid ndk
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
Paris Android User Group
 
Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDK
Sebastian Mauer
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - Introduction
Rakesh Jha
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
ナム-Nam Nguyễn
 
Working with the AOSP - Linaro Connect Asia 2013
Working with the AOSP - Linaro Connect Asia 2013Working with the AOSP - Linaro Connect Asia 2013
Working with the AOSP - Linaro Connect Asia 2013
Opersys inc.
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application Development
Ramesh Prasad
 
Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
Xavier Hallade
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Somkiat Khitwongwattana
 
Exploring Next Generation Buildpacks - Anand Rao & Scott Deeg
Exploring Next Generation Buildpacks - Anand Rao & Scott DeegExploring Next Generation Buildpacks - Anand Rao & Scott Deeg
Exploring Next Generation Buildpacks - Anand Rao & Scott Deeg
VMware Tanzu
 
Spooky House Studios: Game development for smartwatches. Challenges and solut...
Spooky House Studios: Game development for smartwatches. Challenges and solut...Spooky House Studios: Game development for smartwatches. Challenges and solut...
Spooky House Studios: Game development for smartwatches. Challenges and solut...
DevGAMM Conference
 
lecture-2-android-dev.pdf
lecture-2-android-dev.pdflecture-2-android-dev.pdf
lecture-2-android-dev.pdf
jakjak36
 
Android NDK Intro
Android NDK IntroAndroid NDK Intro
Android NDK Intro
Giles Payne
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Native Android for Windows Developers
Native Android for Windows DevelopersNative Android for Windows Developers
Native Android for Windows Developers
Yoss Cohen
 
NDK Introduction
NDK IntroductionNDK Introduction
NDK Introduction
RAHUL TRIPATHI
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building Blocks
Max Kleiner
 
Native development kit (ndk) introduction
Native development kit (ndk)  introductionNative development kit (ndk)  introduction
Native development kit (ndk) introduction
Rakesh Jha
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik Bytecode
Alain Leon
 

Similar to Getting started with the NDK (20)

Android NDK
Android NDKAndroid NDK
Android NDK
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
 
Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDK
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - Introduction
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
Working with the AOSP - Linaro Connect Asia 2013
Working with the AOSP - Linaro Connect Asia 2013Working with the AOSP - Linaro Connect Asia 2013
Working with the AOSP - Linaro Connect Asia 2013
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application Development
 
Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
 
Exploring Next Generation Buildpacks - Anand Rao & Scott Deeg
Exploring Next Generation Buildpacks - Anand Rao & Scott DeegExploring Next Generation Buildpacks - Anand Rao & Scott Deeg
Exploring Next Generation Buildpacks - Anand Rao & Scott Deeg
 
Spooky House Studios: Game development for smartwatches. Challenges and solut...
Spooky House Studios: Game development for smartwatches. Challenges and solut...Spooky House Studios: Game development for smartwatches. Challenges and solut...
Spooky House Studios: Game development for smartwatches. Challenges and solut...
 
lecture-2-android-dev.pdf
lecture-2-android-dev.pdflecture-2-android-dev.pdf
lecture-2-android-dev.pdf
 
Android NDK Intro
Android NDK IntroAndroid NDK Intro
Android NDK Intro
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
 
Native Android for Windows Developers
Native Android for Windows DevelopersNative Android for Windows Developers
Native Android for Windows Developers
 
NDK Introduction
NDK IntroductionNDK Introduction
NDK Introduction
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building Blocks
 
Native development kit (ndk) introduction
Native development kit (ndk)  introductionNative development kit (ndk)  introduction
Native development kit (ndk) introduction
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik Bytecode
 

Recently uploaded

Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
kalichargn70th171
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 

Recently uploaded (20)

Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 

Getting started with the NDK

  • 1. Fullstack as a service Getting Started with the NDK Native Development Kit (NDK) Kirill Kounik
  • 2. Agenda Introduction: What/Why NDK ABIs - Architectures and CPUs NDK and Project setup Libraries Debugging
  • 3. Native Development for Android Natural/native language of Android development is Java Android runs Linux kernel at its core C/C++ development is possible, actually an Android application can be written entirely in C or C++ Leverages standard Java Native Interface (JNI) However it will not benefit most apps
  • 4.
  • 5. Benefits of Native Development for Android • Mostly useful for computation intensive apps where performance becomes an issue • May help to port or share code with other platforms, like iOS • Use external native libraries • Sometimes allows access to APIs not exposed in Java layer
  • 6. NDK development Cons: Requires compilation for every CPU architecture and, possibly, a separate APK for each architecture. May significantly increase APK size JNI development is cumbersome for Java developers
  • 7. Native Development Kit or NDK for Android NDK is a set of tools or toolchain used for for native development for Android platform. http://developer.android.com/ndk/index.html Samples* https://github.com/googlesamples/android-ndk
  • 8. ABI is Application Binary Interface Different Android handsets use different CPUs, which in turn support different instruction sets. Each combination of CPU and instruction sets has its own Application Binary Interface, or ABI. armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mips64
  • 9. APK structure with native code $ unzip -l build/outputs/apk/app-all-debug.apk Archive: build/outputs/apk/app-all-debug.apk Length Date Time Name --------- ---------- ----- ---- 1896 03-31-2016 14:10 AndroidManifest.xml ... 2724 03-31-2016 14:10 classes.dex 5592 03-31-2016 14:10 lib/arm64-v8a/libhello-jni.so 13552 03-31-2016 14:10 lib/armeabi/libhello-jni.so 13560 03-31-2016 14:10 lib/armeabi-v7a/libhello-jni.so 5460 03-31-2016 14:10 lib/mips/libhello-jni.so 6048 03-31-2016 14:10 lib/mips64/libhello-jni.so 5264 03-31-2016 14:10 lib/x86/libhello-jni.so 5784 03-31-2016 14:10 lib/x86_64/libhello-jni.so ... --------- ------- 81286 17 files
  • 11. NDK setup NDK setup is very straight-forward: [Install Android Studio] Download and unzip NDK into a convenient location or let Android Studio do it for you Optionally get the samples from Github, they are not included in the NDK https://github.com/googlesamples/android-ndk * * Look for various branches corresponding to build methods
  • 12. Several ways to build native code • Using ndk-build or cmake and externalNativeBuild { } block in build.gradle (starting with Android Studio 2.2) Use if you are porting older or very complex project or familiar with GNU make or cmake • Using experimental Gradle plugin – new build system Recommended for new projects, doesn’t require external build tool knowledge • Use toolchain directly Use you favorite build tools
  • 13. External Native Build – ndk-build • Define Android.mk file location • Pass optional ndk-build parameters • Define required ABIs externalNativeBuild { ndkBuild { path “Android.mk” abiFilters 'armeabi-v7a‘, 'arm64-v8a', 'x86', 'x86_64' arguments 'NDK_DEBUG=0', '-j4', 'ANDROID_TOOLCHAIN=clang' } }
  • 14. Experimental plugin - Project structure Starting from version 1.3 Android Studio supports NDK development with the relatively new 'com.android.model.application' gradle plugin .C, .CPP files under jni folder at the same level as java *Old toolchain based on makefiles and ndk-build is not covered by this training.
  • 15. Project structure Build settings defined in android.ndk section of the build.gradle file, for example model { ... android.ndk { moduleName = "native-codec-jni" cppFlags.add("-UNDEBUG") // for native multimedia ldLibs.addAll(["OpenMAXAL", "mediandk"]) // for logging ldLibs.add("log") // for native windows ldLibs.add("android") stl = "stlport_static" } ... }
  • 16. Everything you can configure in android.ndk android.ndk { // All configurations that can be changed in android.ndk. moduleName = "mymodule" ldLibs.addAll(['log', 'android']) ldLibs.add("log") ldFlags.add("-L/custom/lib/path") ... }
  • 17. Everything you can configure in android.ndk 2 android.ndk { // All configurations that can be changed in android.ndk. ... abiFilters.add("x86") // List of target ABIs CFlags.add("-DCUSTOM_DEFINE") cppFlags.add("-DCUSTOM_DEFINE") debuggable = false renderscriptNdkMode = false stl = "stlport_static" // choice of c++ runtimes provided platformVersion = 15 } ● http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-Ndk-Integration
  • 18. Define what ABIs to package in the APK
  • 19. Or Split APKs In native code causes an APK too large. To build separate APK for each architecture android { ... splits { abi { enable true reset() include 'x86', 'armeabi-v7a', 'mips' // ABIs to include universalApk true // build “fat” version } } }
  • 20. Split APKs - version code support Every APK in Play store must have unique versionCode // map for the version code project.ext.versionCodes = ['armeabi-v7a':1, 'x86':2, 'mips':3] applicationVariants.all { variant -> // assign different version code for each output variant.outputs.each { output -> output.versionCodeOverride = project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) * 10000 + variant.versionCode } } * http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
  • 21. C++ runtimes (stl = "stlport_static") the build system automatically links the standard C libraries, real-time extensions, and pthread By default NDK provides a very minimal standard C++ runtime support library (libstdc++). This minimal support does not include, for example: ○ Standard C++ Library support (except a few trivial headers). ○ C++ exceptions support ○ RTTI support Available runtimes: GAbi++, STLport, GNU STL, LLVM libc++ Detailed information about various available runtimes and supplied headers can be found in the docs: developer.android.com/ndk/guides/cpp-support.html
  • 22. Stable NDK libraries (ldLibs.addAll(['log', 'android'])) The Android NDK provides a set of native headers for prebuilt libraries that allow access various system features without the need to go through JNI. Library Description Header files log Android log support log.h z ZLib compression library zlib.h, zconf.h dl Dynamic linker library dlfcn.h GLESv1_CM, GLESv2, OpenSLES, EGL, GLESv3, 3.1 Open GL ES, EGL, libraries for various versions many android For writing pure native apps many OpenMAXAL Android native multimedia handling is based on Khronos Group OpenMAX AL OMXAL/OpenMAXAL.h, OMXAL/OpenMAXAL_Platform.h, OpenMAXAL_Android.h jnigraphics Native interface to the pixel buffers of bitmaps bitmap.h
  • 23. Pure Native Activity android.app.NativeActivity is a glue between Android and and pure native Activity No need to subclass it, only properly define in the manifest Android framework APIs can be accessed through the JNI Native code should adhere to certain structure defined by NDK in native_activity.h Alternatively use helper library defined in android_native_app_glue.h Implement native function ANativeActivity_onCreate Implement ANativeActivity->callbacks to manage activity lifecycle
  • 24. Pure native activity manifest <application android:label="@string/app_name" android:hasCode="false"> <!-- Our activity is the built-in NativeActivity framework class. This will take care of integrating with our NDK code. --> <activity android:name="android.app.NativeActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="native-activity" /> ... </activity> </application> ● Sample: https://github.com/googlesamples/android-ndk/tree/master/native-activity
  • 25. Debugging JNI code In the latest releases of Android Studio native debugger is nicely integrated and can be used out of the box
  • 26.
  • 27. Debugging JNI crashes Tombstone files are crash dumps with some extra information Up to 10 last tombstone files saved by the system and replaced cyclically $ adb shell ls /data/tombstones $ adb pull /data/tombstones/tombstone_00
  • 28. Examining tombstone files with ndk-stack Identify crash address in your library $ ndk-stack -sym <root symbols dir> [-dump <dump file>] $ ndk-stack -sym .appbuildintermediatessymbols -dump .tombstone_00
  • 29. Finding file location with addr2line addr2line command that you need to use depends on the ABI of your crashed file. alias addr2line= ’$NDK/toolchains/x86_64-4.9/prebuilt/windows/bin/i686-linux-android- addr2line.exe’ Find the crashed line $ addr2line -f -e <input file> <address>
  • 30. Summary JNIEnv*, Local/global refs, method signatures, javah, javap, tombstones, ndk-stack, addr2line Q & A

Editor's Notes

  1. Presenter should come with downloaded NDK zip file
  2. http://tools.android.com/tech-docs/new-build-system/gradle-experimental
  3. Alternative way is to use product flavors. However if you have real product flavors this will be cumbersome. Samples still use product flavors method and abiFilters
  4. It is recommended to write Android application in Java and only delegate selected parts to native code. However it is possible to write an app entirely in C/C++. Consider Web Browser or very complex high-performance game
  5. Working directory: C:\Users\kirill\kirill\Projects\tikal\android-ndk-samples\hello-jni Crashlytics upload symbols
  6. Working directory: C:\Users\kirill\kirill\Projects\tikal\android-ndk-samples\hello-jni $ ndk-stack -sym .\app\build\intermediates\symbols -dump .\tombstone_00
  7. Working directory: C:\Users\kirill\kirill\Projects\tikal\android-ndk-samples\hello-jni alias addr2line -f -e app/build/intermediates/binaries/debug/x86-64/obj/x86_64/libhello-jni.so 07dd addr2line=’/cygdrive/c/apps/android-ndk-r11c/toolchains/x86_64-4.9/prebuilt/windows/bin/x86_64-linux-android-addr2line.e