The document provides information about using the Native Development Kit (NDK) to build native code for Android applications. It discusses what the NDK is, why developers may want to use native code, and how to set up a sample project to call native code from Java using the Java Native Interface (JNI). The sample project implements Fibonacci functions recursively and iteratively in both Java and C++ to demonstrate how to define and call native functions from an Android app.
The Android Native Development Kit (NDK)
NDK Info
What , Why , Why not ?
Requirement Tools & Installation of NDK
Developing an ADD application that uses the NDK
Using the Android Native Development Kit (NDK)DroidConTLV
Android NDK is used to integrate C/C++ code into Android applications. Learn when and how to use it ; and what it implies to do so from development, integration within Eclipse and Android Studio, to distribution.
Target audience:
- Users who have just started using the Asset Store in Unity
Key points:
- Bulk import popular, or even seemingly popular, assets from inside or outside of the country
This article contains information about performance optimization of Unity3D games for android. Different solutions provided both for CPU and GPU. Also here you can find methodology which will help you to detect performance problems, analyze them and perform appropriate optimization.
The Android NDK is a set of tools that allows the integration of native code (C/C++) in your Android app. In this presentation get know interesting usages of NDK, advantages and disadvantages, and how to stat using it with Android Studio.
Unity - Internals: memory and performanceCodemotion
by Marco Trivellato - In this presentation we will provide in-depth knowledge about the Unity runtime. The first part will focus on memory and how to deal with fragmentation and garbage collection. The second part will cover implementation details and their memory vs cycles tradeoffs in both Unity4 and the upcoming Unity5.
Rendering Techniques for Augmented Reality and a Look Ahead at AR FoundationUnity Technologies
Have you ever wondered why your virtual and real-world objects look disconnected from each other? Are you curious about how to blend virtual and physical spaces to create a seamless experience? This talk will cover advanced techniques using lighting, depth masks, occlusion, shadows and more to create a sense of immersion. You'll also get an introduction to a set of tools that will improve your development process when using Unity and AR Foundation.
Jimmy Alamparambil - Unity Technologies
Lukasz Pasek - Unity Technologies
Bryce Harrington, Senior Graphics Engineer with the Samsung Open Source Group, compares two 2-D drawing libraries (Cairo and Skia), including showcasing work on a testing framework (Caskbench) for measuring performance of these two systems
Introduction to Skia by Ryan Chou @20141008Ryan Chou
This introduces the fundamental knowledge about Skia which is open-source project used in Android. In this, it contains the history of skia, and the roll of skia in Android.
The Android Native Development Kit (NDK)
NDK Info
What , Why , Why not ?
Requirement Tools & Installation of NDK
Developing an ADD application that uses the NDK
Using the Android Native Development Kit (NDK)DroidConTLV
Android NDK is used to integrate C/C++ code into Android applications. Learn when and how to use it ; and what it implies to do so from development, integration within Eclipse and Android Studio, to distribution.
Target audience:
- Users who have just started using the Asset Store in Unity
Key points:
- Bulk import popular, or even seemingly popular, assets from inside or outside of the country
This article contains information about performance optimization of Unity3D games for android. Different solutions provided both for CPU and GPU. Also here you can find methodology which will help you to detect performance problems, analyze them and perform appropriate optimization.
The Android NDK is a set of tools that allows the integration of native code (C/C++) in your Android app. In this presentation get know interesting usages of NDK, advantages and disadvantages, and how to stat using it with Android Studio.
Unity - Internals: memory and performanceCodemotion
by Marco Trivellato - In this presentation we will provide in-depth knowledge about the Unity runtime. The first part will focus on memory and how to deal with fragmentation and garbage collection. The second part will cover implementation details and their memory vs cycles tradeoffs in both Unity4 and the upcoming Unity5.
Rendering Techniques for Augmented Reality and a Look Ahead at AR FoundationUnity Technologies
Have you ever wondered why your virtual and real-world objects look disconnected from each other? Are you curious about how to blend virtual and physical spaces to create a seamless experience? This talk will cover advanced techniques using lighting, depth masks, occlusion, shadows and more to create a sense of immersion. You'll also get an introduction to a set of tools that will improve your development process when using Unity and AR Foundation.
Jimmy Alamparambil - Unity Technologies
Lukasz Pasek - Unity Technologies
Bryce Harrington, Senior Graphics Engineer with the Samsung Open Source Group, compares two 2-D drawing libraries (Cairo and Skia), including showcasing work on a testing framework (Caskbench) for measuring performance of these two systems
Introduction to Skia by Ryan Chou @20141008Ryan Chou
This introduces the fundamental knowledge about Skia which is open-source project used in Android. In this, it contains the history of skia, and the roll of skia in Android.
http://fr.droidcon.com/2014/agenda/
http://fr.droidcon.com/2014/agenda/detail?title=The+Android+Native+Development+Kit
The Android NDK is used to integrate C/C++ code into Android applications and libraries.
Learn how you can use the NDK and NDK-based libraries with Eclipse and Android Studio, and how you can debug and optimize your code.
Discover what changes from the new Android Runtime may break your integration, and how you can target new 64-bit architectures with the upcoming android L-release.
Speaker : Alexander Weggerle, Intel
Native development kit (ndk) introductionRakesh Jha
Native development kit (ndk) introduction
Topics covered include •
o What is an NDK and Why NDK? •
o Java Native Interface (JNI) •
o Using NDK •
o NDK and JNI by Example •
o NDK's Stable APIs
• Android NDK Multithreading
o Introduction To NDK Native Threading
o Creating and terminating native threads at Android NDK
Build with pthreads
Thread creation
Thread termination
o Synchronizing native threads with conditional variables at Android NDK
Initialize and destroy conditional variables
o Synchronizing native threads with semaphore at Android NDK
Initialize and destroy a semaphore
o Managing data for native threads at Android NDK
International Journal of Engineering Research and DevelopmentIJERD Editor
Electrical, Electronics and Computer Engineering,
Information Engineering and Technology,
Mechanical, Industrial and Manufacturing Engineering,
Automation and Mechatronics Engineering,
Material and Chemical Engineering,
Civil and Architecture Engineering,
Biotechnology and Bio Engineering,
Environmental Engineering,
Petroleum and Mining Engineering,
Marine and Agriculture engineering,
Aerospace Engineering.
Overview of Android NDK (Native Development Toolkit).
Android application development is primarily done with the Android SDK.
Apps are written in Java thus benefitting from the high-level constructs
of the managed environment as well as the wealth of functionality provided
by the Android application platform.
However, sometimes it is necessary to implement certain functionality natively
in C++, e.g. when access to hardware is required or 3rd party stacks have to
be included that are only available as C++ code.
The NDK is a lightweight development toolkit for writing native applications
and libraries that can interwork with Java application code.
In dieser Session werfen wir einen Blick auf die Android Platform jenseits der Dalvik VM. Wir entdecken den Android Source Code und erklären wo sich interessante Beispiele und Referenzen für App-Entwickler sowie nützliche Tools verbergen. Ein High-Level Überblick über die Platform-Architektur und das Platform-Build-System runden die Session ab.
This meetup discussed different concepts for building Android applications for multiple versions and devices out on the market today. This included the ability to take advantage of new 2.0 and 2.1 features while still supporting 1.x users in the same binary.
Using Native Development Toolkit . Information collected from many sources http://lyaug.fr/slides/presentation-ndk/#/step-11
http://psrdotcom.blogspot.fr/2011/12/android-ndk-jni-windows-xp7-with-3264.html
My session in Wearable DevCon 2014, Burlingame, CA
[Note: now the conference is called "Wearable Tech Con" ]
The session gives an introduction to using the Java Native Interface (JNI) in Java, and in particular in the Android Platform. The session then covers the use of the Native Development Kit (NDK) for developing Android applications.
Bullet Physics is a professional open source collision detection, rigid body and soft body dynamics library. The library is free for commercial use under the ZLib license.
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Globus
Large Language Models (LLMs) are currently the center of attention in the tech world, particularly for their potential to advance research. In this presentation, we'll explore a straightforward and effective method for quickly initiating inference runs on supercomputers using the vLLM tool with Globus Compute, specifically on the Polaris system at ALCF. We'll begin by briefly discussing the popularity and applications of LLMs in various fields. Following this, we will introduce the vLLM tool, and explain how it integrates with Globus Compute to efficiently manage LLM operations on Polaris. Attendees will learn the practical aspects of setting up and remotely triggering LLMs from local machines, focusing on ease of use and efficiency. This talk is ideal for researchers and practitioners looking to leverage the power of LLMs in their work, offering a clear guide to harnessing supercomputing resources for quick and effective LLM inference.
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus
As part of the DOE Integrated Research Infrastructure (IRI) program, NERSC at Lawrence Berkeley National Lab and ALCF at Argonne National Lab are working closely with General Atomics on accelerating the computing requirements of the DIII-D experiment. As part of the work the team is investigating ways to speedup the time to solution for many different parts of the DIII-D workflow including how they run jobs on HPC systems. One of these routes is looking at Globus Compute as a way to replace the current method for managing tasks and we describe a brief proof of concept showing how Globus Compute could help to schedule jobs and be a tool to connect compute at different facilities.
First Steps with Globus Compute Multi-User EndpointsGlobus
In this presentation we will share our experiences around getting started with the Globus Compute multi-user endpoint. Working with the Pharmacology group at the University of Auckland, we have previously written an application using Globus Compute that can offload computationally expensive steps in the researcher's workflows, which they wish to manage from their familiar Windows environments, onto the NeSI (New Zealand eScience Infrastructure) cluster. Some of the challenges we have encountered were that each researcher had to set up and manage their own single-user globus compute endpoint and that the workloads had varying resource requirements (CPUs, memory and wall time) between different runs. We hope that the multi-user endpoint will help to address these challenges and share an update on our progress here.
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Shahin Sheidaei
Games are powerful teaching tools, fostering hands-on engagement and fun. But they require careful consideration to succeed. Join me to explore factors in running and selecting games, ensuring they serve as effective teaching tools. Learn to maintain focus on learning objectives while playing, and how to measure the ROI of gaming in education. Discover strategies for pitching gaming to leadership. This session offers insights, tips, and examples for coaches, team leads, and enterprise leaders seeking to teach from simple to complex concepts.
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
Even though at surface level ‘java.lang.OutOfMemoryError’ appears as one single error; underlyingly there are 9 types of OutOfMemoryError. Each type of OutOfMemoryError has different causes, diagnosis approaches and solutions. This session equips you with the knowledge, tools, and techniques needed to troubleshoot and conquer OutOfMemoryError in all its forms, ensuring smoother, more efficient Java applications.
Accelerate Enterprise Software Engineering with PlatformlessWSO2
Key takeaways:
Challenges of building platforms and the benefits of platformless.
Key principles of platformless, including API-first, cloud-native middleware, platform engineering, and developer experience.
How Choreo enables the platformless experience.
How key concepts like application architecture, domain-driven design, zero trust, and cell-based architecture are inherently a part of Choreo.
Demo of an end-to-end app built and deployed on Choreo.
Large Language Models and the End of ProgrammingMatt Welsh
Talk by Matt Welsh at Craft Conference 2024 on the impact that Large Language Models will have on the future of software development. In this talk, I discuss the ways in which LLMs will impact the software industry, from replacing human software developers with AI, to replacing conventional software with models that perform reasoning, computation, and problem-solving.
Software Engineering, Software Consulting, Tech Lead.
Spring Boot, Spring Cloud, Spring Core, Spring JDBC, Spring Security,
Spring Transaction, Spring MVC,
Log4j, REST/SOAP WEB-SERVICES.
Developing Distributed High-performance Computing Capabilities of an Open Sci...Globus
COVID-19 had an unprecedented impact on scientific collaboration. The pandemic and its broad response from the scientific community has forged new relationships among public health practitioners, mathematical modelers, and scientific computing specialists, while revealing critical gaps in exploiting advanced computing systems to support urgent decision making. Informed by our team’s work in applying high-performance computing in support of public health decision makers during the COVID-19 pandemic, we present how Globus technologies are enabling the development of an open science platform for robust epidemic analysis, with the goal of collaborative, secure, distributed, on-demand, and fast time-to-solution analyses to support public health.
Check out the webinar slides to learn more about how XfilesPro transforms Salesforce document management by leveraging its world-class applications. For more details, please connect with sales@xfilespro.com
If you want to watch the on-demand webinar, please click here: https://www.xfilespro.com/webinars/salesforce-document-management-2-0-smarter-faster-better/
Quarkus Hidden and Forbidden ExtensionsMax Andersen
Quarkus has a vast extension ecosystem and is known for its subsonic and subatomic feature set. Some of these features are not as well known, and some extensions are less talked about, but that does not make them less interesting - quite the opposite.
Come join this talk to see some tips and tricks for using Quarkus and some of the lesser known features, extensions and development techniques.
A Comprehensive Look at Generative AI in Retail App Testing.pdfkalichargn70th171
Traditional software testing methods are being challenged in retail, where customer expectations and technological advancements continually shape the landscape. Enter generative AI—a transformative subset of artificial intelligence technologies poised to revolutionize software testing.
How Recreation Management Software Can Streamline Your Operations.pptxwottaspaceseo
Recreation management software streamlines operations by automating key tasks such as scheduling, registration, and payment processing, reducing manual workload and errors. It provides centralized management of facilities, classes, and events, ensuring efficient resource allocation and facility usage. The software offers user-friendly online portals for easy access to bookings and program information, enhancing customer experience. Real-time reporting and data analytics deliver insights into attendance and preferences, aiding in strategic decision-making. Additionally, effective communication tools keep participants and staff informed with timely updates. Overall, recreation management software enhances efficiency, improves service delivery, and boosts customer satisfaction.
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Globus
The U.S. Geological Survey (USGS) has made substantial investments in meeting evolving scientific, technical, and policy driven demands on storing, managing, and delivering data. As these demands continue to grow in complexity and scale, the USGS must continue to explore innovative solutions to improve its management, curation, sharing, delivering, and preservation approaches for large-scale research data. Supporting these needs, the USGS has partnered with the University of Chicago-Globus to research and develop advanced repository components and workflows leveraging its current investment in Globus. The primary outcome of this partnership includes the development of a prototype enterprise repository, driven by USGS Data Release requirements, through exploration and implementation of the entire suite of the Globus platform offerings, including Globus Flow, Globus Auth, Globus Transfer, and Globus Search. This presentation will provide insights into this research partnership, introduce the unique requirements and challenges being addressed and provide relevant project progress.
Code reviews are vital for ensuring good code quality. They serve as one of our last lines of defense against bugs and subpar code reaching production.
Yet, they often turn into annoying tasks riddled with frustration, hostility, unclear feedback and lack of standards. How can we improve this crucial process?
In this session we will cover:
- The Art of Effective Code Reviews
- Streamlining the Review Process
- Elevating Reviews with Automated Tools
By the end of this presentation, you'll have the knowledge on how to organize and improve your code review proces
We describe the deployment and use of Globus Compute for remote computation. This content is aimed at researchers who wish to compute on remote resources using a unified programming interface, as well as system administrators who will deploy and operate Globus Compute services on their research computing infrastructure.
Into the Box Keynote Day 2: Unveiling amazing updates and announcements for modern CFML developers! Get ready for exciting releases and updates on Ortus tools and products. Stay tuned for cutting-edge innovations designed to boost your productivity.
2. About Marko Gargenta
Marko Gargenta
Entrepreneur, Author, Speaker
Developer of Android Bootcamp for Marakana.
Instructor for 1,000s of developers on Android at Qualcomm, Cisco, Motorola, Intel, DoD and other great orgs.
Author of Learning Android published by O’Reilly.
Speaker at OSCON (4x), ACM, IEEE(2x), SDC(2x), AnDevCon(3x), DroidCon.
Co-Founder of SFAndroid.org
Co-Chair of Android Open conference: Android Open
3. Objectives of NDK Module
Android is put together of about equal part Java and C. So, no wonder that we need an easy
way to bridge between these two totally different worlds. Java offers Java Native Interface
(JNI) as a framework connecting the world of Java to the native code. Android goes a step
further by packaging other useful tools and libraries into a Native Development Kit, or NDK.
NDK makes developing C/C++ code that works with an Android app much simpler than if one
was to do it by hand. Topics covered include:
What is in NDK?
Why NDK?
Java Native Interface (JNI)
Using NDK
NDK and JNI by Example
NDK’s Stable APIs
Lab: NDK
4. NDK in Action
Using NDK to connect Activity to native code
6. What is in NDK?
NDK is a toolchain
Cross-compiler, linker, what you need to build for ARM, x86, MIPS, etc.
NDK provides a way to bundle lib.so into your APK
The native library needs to be loadable in a secure way.
NDK "standardizes" various native platforms
It provides headers for lliibbcc, lliibbmm, lliibbzz, lliibblloogg, lliibbjjnniiggrraahhiiccss, OpenGL/OpenSL ES,
JNI headers, minimal C++ support headers, and Android native app APIs.
NDK comes with docs and samples
Helps you get up to speed with native development.
Android’s Dalvik VM allows our applications written in Java to call methods implemented in native code
through the Java Native Interface (JNI)
For example:
package com.marakana.android.fetchurl;
public class FetchUrl {
public static native byte[] fetch(String url);
static {
System.loadLibrary("fetchurl");
}
}
NDK is a tool-chain to build and cross-compile our native code for the device-specific architecture
At the moment, NDK supports ARMv5TE, ARMv7-A, x86 (since r6), and mips (since r8) ABIs
For example, we would implement native ffeettcchh method in C and compile it into a library
lliibbffeettcchhuurrll..ssoo
NDK offers a way to package our native code library (as lliibb<<ssoommeetthhiinngg>>..ssoo) into the APK file so we can
distribute it with our application easily
For example, our library would be packaged as lliibbss//aarrmmeeaabbii//lliibbffeettcchhuurrll..ssoo in the APK
NDK provides a set of native system headers that will be supported for the future releases of Android platform
(lliibbcc, lliibbmm, lliibbzz, lliibblloogg, lliibbjjnniiggrraahhiiccss, OpenGL/OpenSL ES, JNI headers, minimal C++ support
headers, and Android native app APIs)
NDK comes with extensive documentation, sample code, and examples
As of ADT r20, NDK also adds a plugin to ADT that enables debugging NDK projects in Eclipse and requires:
7. CDT (v7.0.2 or 8.0.2) for Eclipse from http://download.eclipse.org/tools/cdt/releases/indigo
ADT (r20) with SDK/NDK plugins for Eclipse from https://dl-ssl.google.com/android/eclipse/ with
SDK/NDK paths configured
Before an Eclipse project app can be debugged, it must have:
native support (project properties → Android Tools → Add native support) enabled
NNDDKK__DDEEBBUUGG==11 (project properties → C/C++ Build → Build Command → nnddkk--bbuuiilldd NNDDKK__DDEEBBUUGG==11)
set
To debug an Eclipse project app:
Set the break point(s) as desired
Select project → Run → Debug As → Android Native Application
8. Why NDK?
For performance
Sometimes, native code still runs faster.
For legacy support
You may have that C/C++ code you’d like to use in your app.
For access to low-level libraries
In a rare case when there is no Java API to do something.
For cross-platform development
C is the new portable language.
But
Adding JNI to your app will make it more complex.
Plus
Java often offers much richer APIs, memory protection, OOP, productivity.
NDK allows us to develop parts of our Android application in C/C++
Generally, we do not develop native-only apps via NDK
Android’s Gingerbread (2.3) release supports NNaattiivveeAAccttiivviittyy, which allows handling lifecycle
callbacks in native code, so we can now develop native-only apps,
No support for native services, broadcast receivers, content providers, etc.
NDK code still subject to security sandboxing - we don’t get extra permissions for running natively
Main motivation for native code is performance (CPU-intensive, self-contained, low-memory footprint code)
and the re-use of legacy code
For system integrators, NDK offers access to low-level libraries (e.g. access to user-space HAL)
Using NDK always increases complexity of applications, so it should only be used when it’s essential to
the application
Programming in Java offers richer APIs, memory protection and management, higher-level language
constructs (OOP), all of which generally results in higher productivity
11. NDK and JNI by Example
NDK is best explained via an example (based on Fibonacci)
The code is available
As a ZIP archive: https://github.com/marakana/FibonacciNative/zipball/master
By Git: ggiitt cclloonnee https://github.com/marakana/FibonacciNative.git
Start by creating a new Android Project
Project Name: FibonacciNative
Build Target: Android 2.2 (API 8) or later
Application Name: Fibonacci Native
Package: ccoomm..mmaarraakkaannaa..aannddrrooiidd..ffiibboonnaacccciinnaattiivvee
Create Activity: FFiibboonnaacccciiAAccttiivviittyy
12. Fibonacci - Java Native Function Prototypes
We start off by defining C function prototypes as nnaattiivvee Java methods (wrapped in some
class):
FFiibboonnaacccciiNNaattiivvee//ssrrcc//ccoomm//mmaarraakkaannaa//aannddrrooiidd//ffiibboonnaacccciinnaattiivvee//FFiibbLLiibb..jjaavvaa
13. package com.marakana.android.fibonaccinative;
import android.util.Log;
public class FibLib {
private static final String TAG = "FibLib";
private static long fib(long n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}
// Recursive Java implementation of the Fibonacci algorithm
// (included for comparison only)
public static long fibJR(long n) {
Log.d(TAG, "fibJR(" + n + ")");
return fib(n);
}
// Function prototype for future native recursive implementation
// of the Fibonacci algorithm
public native static long fibNR(long n);
// Iterative Java implementation of the Fibonacci algorithm
// (included for comparison only)
public static long fibJI(long n) {
Log.d(TAG, "fibJI(" + n + ")");
long previous = -1;
long result = 1;
for (long i = 0; i <= n; i++) {
long sum = result + previous;
previous = result;
result = sum;
}
return result;
}
// Function prototype for future iterative recursive implementation
// of the Fibonacci algorithm
public native static long fibNI(long n);
static {
// as defined by LOCAL_MODULE in Android.mk
System.loadLibrary("com_marakana_android_fibonaccinative_FibLib");
}
}
14. Fibonacci - Function Prototypes in a C Header File
We then extract our C header file with our function prototypes:
1. On the command line, change to your project’s root directory
$ cd /path/to/workspace/FibonacciNative
2. Create jjnnii sub-directory
3. Extract the C header file from ccoomm..mmaarraakkaannaa..aannddrrooiidd..ffiibboonnaacccciinnaattiivvee..FFiibbLLiibb
class:
Prior to ADT r14, compiled class files were kept directly in the bbiinn// directory, so in our jjaavvaahh command
we would’ve used --ccllaassssppaatthh bbiinn instead.
4. Check out the resulting file:
FFiibboonnaacccciiNNaattiivvee//jjnnii//ccoomm__mmaarraakkaannaa__aannddrrooiidd__ffiibboonnaacccciinnaattiivvee__FFiibbLLiibb..hh
$ mkdir jni
$ javah -jni -classpath bin/classes -d jni com.marakana.android.fibonaccinative.FibLib
15. /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_marakana_android_fibonaccinative_FibLib */
#ifndef _Included_com_marakana_android_fibonaccinative_FibLib
#define _Included_com_marakana_android_fibonaccinative_FibLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_marakana_android_fibonaccinative_FibLib
* Method: fibNR
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_marakana_android_fibonaccinative_FibLib_fibNR
(JNIEnv *, jclass, jlong);
/*
* Class: com_marakana_android_fibonaccinative_FibLib
* Method: fibNI
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_marakana_android_fibonaccinative_FibLib_fibNI
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
} #endif
#endif
The function prototype names are name-spaced to the classname they are found in.
16. Fibonacci - Provide C Implementation
We provide the C implementation of com_marakana_android_ffiibboonnaaccccii__FFiibbLLiibb..hh
header file:
FFiibboonnaacccciiNNaattiivvee//jjnnii//ccoomm__mmaarraakkaannaa__aannddrrooiidd__ffiibboonnaacccciinnaattiivvee__FFiibbLLiibb..cc
/* Include the header file that was created via "javah -jni" command */
#include "com_marakana_android_fibonaccinative_FibLib.h"
#include <android/log.h>
/* Recursive implementation of the fibonacci algorithm (in a helper function) */
static jlong fib(jlong n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}
/* Actual implementation of JNI-defined `fibNR` (recursive) function */
JNIEXPORT jlong JNICALL Java_com_marakana_android_fibonaccinative_FibLib_fibNR
(JNIEnv *env, jclass clazz, jlong n) {
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNR(%lld)", n);
return fib(n);
}
/* Actual implementation of JNI-defined `fibNI` (iterative) function */
JNIEXPORT jlong JNICALL Java_com_marakana_android_fibonaccinative_FibLib_fibNI
(JNIEnv *env, jclass clazz, jlong n) {
jlong previous = -1;
jlong result = 1;
jlong i;
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNI(%lld)", n);
for (i = 0; i <= n; i++) {
jlong sum = result + previous;
previous = result;
result = sum;
}
return result;
}
17. Fibonacci - An Alternative Implementation (C++)
We could also use an alternative mechanism of linking native-code to managed code by pre-registering
our functions. This leads to earlier detection of method-function mismatch issues,
a slight performance improvement, and spares us the redundancy of the header file and the
use of the jjaavvaahh command.
See Registering Native Methods and JNI_OnLoad
FFiibboonnaacccciiNNaattiivvee//jjnnii//ccoomm__mmaarraakkaannaa__aannddrrooiidd__ffiibboonnaacccciinnaattiivvee__FFiibbLLiibb..ccpppp
18. #include <jni.h>
#include <android/log.h>
namespace com_marakana_android_fibonaccinative {
static jlong fib(jlong n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}
static jlong fibNR(JNIEnv *env, jclass clazz, jlong n) {
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNR(%lld)", n);
return fib(n);
}
static jlong fibNI(JNIEnv *env, jclass clazz, jlong n) {
jlong previous = -1;
jlong result = 1;
jlong i;
__android_log_print(ANDROID_LOG_DEBUG, "FibLib.c", "fibNI(%lld)", n);
for (i = 0; i <= n; i++) {
jlong sum = result + previous;
previous = result;
result = sum;
}
return result;
}
static JNINativeMethod method_table[] = {
{ "fibNR", "(J)J", (void *) fibNR },
{ "fibNI", "(J)J", (void *) fibNI }
};
}
using namespace com_marakana_android_fibonaccinative;
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
} else {
jclass clazz = env->FindClass("com/marakana/android/fibonaccinative/FibLib");
if (clazz) {
jint ret = env->RegisterNatives(clazz, method_table, sizeof(method_table) /
sizeof(method_table[0]));
env->DeleteLocalRef(clazz);
return ret == 0 ? JNI_VERSION_1_6 : JNI_ERR;
} else {
return JNI_ERR;
}
}
}
Most of the Android’s JNI-based shared libraries are built using this, "alternative", approach where the
functions are pre-registered.
19. Fibonacci - Makefile
We need a AAnnddrrooiidd..mmkk makefile, which will be used by NDK to compile our JNI code into a
shared library:
FFiibboonnaacccciiNNaattiivvee//jjnnii//AAnnddrrooiidd..mmkk
# Defines the root to all other relative paths
# The macro function my-dir, provided by the build system,
# specifies the path of the current directory (i.e. the
# directory containing the Android.mk file itself)
LOCAL_PATH := $(call my-dir)
# Clear all LOCAL_XXX variables with the exception of
# LOCAL_PATH (this is needed because all variables are global)
include $(CLEAR_VARS)
# List all of our C files to be compiled (header file
# dependencies are automatically computed)
LOCAL_SRC_FILES := com_marakana_android_fibonaccinative_FibLib.c
# The name of our shared module (this name will be prepended
# by lib and postfixed by .so)
LOCAL_MODULE := com_marakana_android_fibonaccinative_FibLib
# We need to tell the linker about our use of the liblog.so
LOCAL_LDLIBS += -llog
# Collects all LOCAL_XXX variables since "include $(CLEAR_VARS)"
# and determines what to build (in this case a shared library)
include $(BUILD_SHARED_LIBRARY)
It’s easiest to copy the AAnnddrrooiidd..mmkk file from another (sample) project and adjust LLOOCCAALL__SSRRCC__FFIILLEESS and
LLOOCCAALL__MMOODDUULLEE as necessary
See //ppaatthh//ttoo//nnddkk--iinnssttaallllaattiioonn--ddiirr//ddooccss//AANNDDRROOIIDD--MMKK..hhttmmll for the complete reference of
Android make files (build system)
20. Fibonacci - Compile Our Shared Module
Finally, from the root of our project (i.e. FFiibboonnaacccciiNNaattiivvee//), we run nnddkk--bbuuiilldd to build
our code into a shared library
(FFiibboonnaacccciiNNaattiivvee//lliibbss//aarrmmeeaabbii//lliibbccoomm__mmaarraakkaannaa__aannddrrooiidd__ffiibboonnaaccccii__FFiibbLLiibb..$ ndk-build
Compile thumb : com_marakana_android_fibonaccinative_FibLib <=
com_marakana_android_fibonaccinative_FibLib.c
SharedLibrary : libcom_marakana_android_fibonaccinative_FibLib.so
Install : libcom_marakana_android_fibonaccinative_FibLib.so =>
libs/armeabi/libcom_marakana_android_fibonaccinative_FibLib.so
The command nnddkk--bbuuiilldd comes from the NDK’s installation directory (e.g. //ppaatthh//ttoo//aannddrrooiidd--nnddkk--rr55bb),
so it’s easiest if we add this directory to our PPAATTHH.
On Windows, older version of NDK required Cygwin (a Unix-like environment and command-line interface for
Microsoft Windows) to provide "shell" (bash) and "make" (gmake) to nnddkk--bbuuiilldd.
Controlling CPU Application Binary Interface (ABI)
By default, the NDK will generate machine code for the aarrmmeeaabbii - i.e. ARMv5TE with
support for Thumb-1.
In addition to ARMv5, NDK also comes with the toolchains necessary to build code for:
ARMv7-A (including hardware FPU/VFPv3-D16, Thumb-2, VFPv3-D32/ThumbEE, and SIMD/NEON)
x86 (as of r6)
MIPS (as of r8)
We can explicitly select ABIs using AAPPPP__AABBII variable:
$ ndk-build APP_ABI=armeabi
$ ndk-build APP_ABI=armeabi-v7a
$ ndk-build APP_ABI=x86
$ ndk-build APP_ABI=mips
We can also combine them, thereby building a "fat binary":
$ ndk-build "APP_ABI=armeabi armeabi-v7a x86 mips"
$ ndk-build APP_ABI=all
Finally, we can also persist our choice of ABI, by saving AAPPPP__AABBII in a AApppplliiccaattiioonn..mmkk file:
jjnnii//AApppplliiccaattiioonn..mmkk:
APP_ABI := all
Each ABI-specific library, gets packaged as lliibb//<<AABBII>>//lliibb<<nnaammee>>..ssoo inside our APK.
21. Upon installation, the library that best matches the native ABI of the device it will execute
on, will get copied to /data/data/<package>/lliibb//lliibb<<nnaammee>>..ssoo.
To remove all generated binaries, run:
$ ndk-build clean
Clean: com_marakana_android_fibonaccinative_FibLib [armeabi]
Clean: stdc++ [armeabi]
22. Fibonacci - Client
We can now build the "client" of our library (in this case a simple activity) to use our FFiibbLLiibb
library.
Fibonacci - String Resources
FFiibboonnaacccciiNNaattiivvee//rreess//vvaalluueess//ssttrriinnggss..xxmmll
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Get Your Fibonacci Numbers Here!</string>
<string name="fibJR">fibJR</string>
<string name="fibJI">fibJI</string>
<string name="fibNR">fibNR</string>
<string name="fibNI">fibNI</string>
<string name="app_name">FibonacciNative</string>
<string name="button">Get Fibonacci Result</string>
Fibonacci - User Interface (Layout)
Fibonacci Native Main Layout
FFiibboonnaacccciiNNaattiivvee//rreess//llaayyoouutt//mmaaiinn..xxmmll
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- This is just a simple title ("Get Your Fibonacci Here!") -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hello"
android:textSize="25sp" />
<!-- This is the entry box for our number "n" -->
<EditText
23. android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="right"
android:inputType="number" >
<requestFocus />
</EditText>
<!-- This radio group allows the user to select the fibonacci implementation type -->
<RadioGroup
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/type_fib_jr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/fibJR" />
<RadioButton
android:id="@+id/type_fib_ji"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/fibJI" />
<RadioButton
android:id="@+id/type_fib_nr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/fibNR" />
<RadioButton
android:id="@+id/type_fib_ni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/fibNI" />
</RadioGroup>
<!-- This button allows the user to trigger fibonacci calculation -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button" />
<!-- This is the output area for the fibonacci result -->
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
25. protected String doInBackground(Void... params) {
long result = 0;
long t = SystemClock.uptimeMillis();
switch (FibonacciActivity.this.type.getCheckedRadioButtonId()) {
case R.id.type_fib_jr:
result = FibLib.fibJR(n);
break;
case R.id.type_fib_ji:
result = FibLib.fibJI(n);
break;
case R.id.type_fib_nr:
result = FibLib.fibNR(n);
break;
case R.id.type_fib_ni:
result = FibLib.fibNI(n);
break;
}
t = SystemClock.uptimeMillis() - t;
return String.format("fib(%d)=%d in %d ms", n, result, t);
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
FibonacciActivity.this.output.setText(result);
}
}.execute();
}
}
27. NDK’s Stable APIs
The header files for NDK stable APIs are available at
/path/to/ndk/platforms/<android-platform>/<arch-nnaammee>>//uussrr//iinncclluuddee.
28. Android-specific Log Support
Include <<aannddrrooiidd//lloogg..hh>> to access various functionality that can be used to send log messages to the
kernel (i.e. logcat buffers) from our native code
Requires that our code be linked to //ssyysstteemm//lliibb//lliibblloogg..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --lllloogg in our
AAnnddrrooiidd..mmkk file
29. ZLib Compression Library
Include <<zzlliibb..hh>> and <<zzccoonnff..hh>> to access ZLib compression library
See hhttttpp::////wwwwww..zzlliibb..nneett//mmaannuuaall..hhttmmll for more info on ZLib
Requires that our code be linked to //ssyysstteemm//lliibb//lliibbzz..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --llzz in our
AAnnddrrooiidd..mmkk file
30. The OpenGL ES 1.x Library
Include <<GGLLEESS//ggll..hh>> and <<GGLLEESS//gglleexxtt..hh>> to access OpenGL ES 1.x rendering calls from native code
The "1.x" here refers to both versions 1.0 and 1.1
Using 1.1 requires OpenGL-capable GPU
Using 1.0 is universally supported since Android includes software renderer for GPU-less devices
Requires that we include <<uusseess--ffeeaattuurree>> tag in our manifest file to indicate the actual OpenGL
version that we expect
Requires that our code be linked to //ssyysstteemm//lliibb//lliibbGGLLEESSvv11__CCMM..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --
llGGLLEESSvv11__CCMM..ssoo in our AAnnddrrooiidd..mmkk file
Since API 4 (Android 1.6)
31. The OpenGL ES 2.0 Library
Include <<GGLLEESS22//ggll22..hh>> and <<GGLLEESS22//ggll22eexxtt..hh>> to access OpenGL ES 2.0 rendering calls from native
code
Enables the use of vertex and fragment shaders via the GLSL language
Since not all devices support OpenGL 2.0, we should include <<uusseess--ffeeaattuurree>> tag in our manifest file
to indicate this requirement
Requires that our code be linked to //ssyysstteemm//lliibb//lliibbGGLLEESSvv22..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --
llGGLLEESSvv22..ssoo in our AAnnddrrooiidd..mmkk file
Since API 4 (Android 2.0)
32. The jjnniiggrraapphhiiccss Library
Include <<aannddrrooiidd//bbiittmmaapp..hh>> to reliably access the pixel buffers of Java bitmap objects from native code
Requires that our code be linked to //ssyysstteemm//lliibb//lliibbjjnniiggrraapphhiiccss..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --
lljjnniiggrraapphhiiccss in our AAnnddrrooiidd..mmkk file
Since API 8 (Android 2.2)
33. The OpenSL ES native audio Library
Include <<SSLLEESS//OOppeennSSLLEESS..hh>> and <<SSLLEESS//OOppeennSSLLEESS__PPllaattffoorrmm..hh>> to perform audio input and output
from native code
Based on Khronos Group OpenSL ES™ 1.0.1
Requires that our code be linked to //ssyysstteemm//lliibb//lliibbOOppeennSSLLEESS..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --
llOOppeennSSLLEESS in our AAnnddrrooiidd..mmkk file
Since API 9 (Android 2.3)
34. The Android native application APIs
Makes it possible to write our entire application in native code
Mainly added for gaming
Our code still depends on the Dalvik VM since most of the platform features are managed in the VM and
accessed via JNI (Native → Java)
Include <android/nnaattiivvee__aaccttiivviittyy..hh>> to write an Android activity (with its life-cycle callbacks) in
native code
A native activity would serve as the main entry point into our native application
Include <<aannddrrooiidd//llooooppeerr..hh>>, <<aannddrrooiidd//iinnppuutt..hh>>, <<aannddrrooiidd//kkeeyyccooddeess..hh>>, and
<<aannddrrooiidd//sseennssoorr..hh>> to listen to input events and sensors directly from native code
Include <<aannddrrooiidd//rreecctt..hh>>, <<aannddrrooiidd//wwiinnddooww..hh>>, <<aannddrrooiidd//nnaattiivvee__wwiinnddooww..hh>>, and
<<aannddrrooiidd//nnaattiivvee__wwiinnddooww__jjnnii..hh>> for window management from native code
Includes ability to lock/unlock the pixel buffer to draw directly into it
Include <<aannddrrooiidd//ccoonnffiigguurraattiioonn..hh>>, <<aannddrrooiidd//aasssseett__mmaannaaggeerr..hh>>,
<<aannddrrooiidd//ssttoorraaggee__mmaannaaggeerr..hh>>, and <<aannddrrooiidd//oobbbb..hh>> for direct access to the assets embedded in
our ..aappkk files Opaque Binary Blob (OBB) files
All access is read-only
Requires that our code be linked to lliibbaannddrrooiidd..ssoo with LLOOCCAALL__LLDDLLIIBBSS ++== --llaannddrrooiidd in our
AAnnddrrooiidd..mmkk file
Since API 9 (Android 2.3)
With the exception of the libraries listed above, the native system libraries in the Android platform are not
considered "stable" and may change in future platform versions. Unless our library is being built for a specific
Android ROM, we should only make use of the stable libraries provided by the NDK.
All the header files are available under: //ppaatthh//ttoo//nnddkk--iinnssttaallllaattiioonn--ddiirr//ppllaattffoorrmmss//aannddrrooiidd--
99//aarrcchh--aarrmm//uussrr//iinncclluuddee//
See //ppaatthh//ttoo//nnddkk--iinnssttaallllaattiioonn--ddiirr//ddooccss//SSTTAABBLLEE--AAPPIISS..hhttmmll for the complete reference of
NDK’s stable APIs.
35. Lab: NDK
The objective of this lab is to test your understanding of JNI and NDK. We will do so by
adding JNI code to an existing application.
1. Start by importing LogNative application into Eclipse
1. Menu Bar → File → Import… → Git → Projects from Git → Next >
2. Under Select Repository Source select URI → Next >
3. Under Source Git Repository → Location → URI: enter
https://github.com/marakana/LLooggNNaattiivvee..ggiitt → Next >
4. Under Branch Selection, leave all branches selected (checked) → Next >
5. Under Local Destination → Destination specify directory of your choice (e.g.
~~//aannddrrooiidd//wwoorrkkssppaaccee//LLooggNNaattiivvee) → Next >
6. Under Select a wizard to use for importing projects, leave Wizard for project import as
Import existing projects → Next >
7. Under Import Projects → Projects, leave LogNative as selected (checked) → Finish
This project can also be downloaded as a ZIP file
2. Examine and test your project in Eclipse
1. Run the application on a device/emulator
2. Enter some tag and message to log, click on the Log button and observe via aaddbb
llooggccaatt that your message get logged (assuming Java was selected)
3. Implement ccoomm..mmaarraakkaannaa..aannddrrooiidd..llooggnnaattiivvee..LLooggLLiibb..llooggNN((iinntt pprriioorriittyy,,
SSttrriinngg ttaagg,, SSttrriinngg mmssgg)) in C
1. Mark the method as nnaattiivvee
2. Remove its body
3. Extract its function prototype into a C header file (hint: jjaavvaahh)
4. Implement the function by taking advantage of <<aannddrrooiidd//lloogg..hh>> (i.e.
36. /ssyysstteemm//lliibb//lliibblloogg..ssoo)
5. Provide the makefile(s)
4. Build (via nnddkk--bbuuiilldd)
5. Run your application
6. Test by selecting NNaattiivvee in the UI and checking that the log tag/message shows up in
aaddbb llooggccaatt
7. As a bonus:
1. Throw jjaavvaa..llaanngg..NNuullllPPooiinntteerrEExxcceeppttiioonn if ttaagg or mmssgg are null
2. Throw jjaavvaa..llaanngg..IIlllleeggaallAArrgguummeennttEExxcceeppttiioonn if pprriioorriittyy is not one of the
allowed types or if ttaagg or mmssgg are empty
Don’t forget to convert ttaagg and mmssgg strings from the Java format (jjssttrriinngg) to native format (cchhaarr **) before
trying to use them in iinntt ____aannddrrooiidd__lloogg__wwrriittee((iinntt pprriioo,, ccoonnsstt cchhaarr **ttaagg,, ccoonnsstt cchhaarr
**tteexxtt)). Be sure to free the native strings before returning from the native method. Finally, don’t forget to tell
the linker about your use of the log library.
The solution is provided:
As a ZIP archive: https://github.com/marakana/LogNative/zipball/solution
By Git: ggiitt cclloonnee https://github.com/marakana/LogNative.git --bb ssoolluuttiioonn
37. Summary of NDK Module
In this module, you learned how Android uses JNI to bridge between the world of Java and
the native code. You also learned how NDK makes the process of working with JNI simpler by
providing tools and framework for developing native libraries as well as packaging them with
the app.
Thank you!
Marko Gargenta & Marakana Team
@MarkoGargenta
Special thanks to Aleksandar (Sasa) Gargenta as well as the rest of Marakana team for
research related to NDK.
Slides & video of this presentation is available at Marakana.com
Yamba source code is available at https://github.com/marakana/
(c) Marakana.com