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.
2. Intel Developers Relations Division 2
Agenda
The Android NDK
Are you using and is not aware?
Be careful with the Binary Interface
Starting with Android Studio + gradle-experimental
Questions?
4. Intel Developers Relations Division 4
“The Android NDK is a set of tools that allows the
implementation of parts of your app using native code
languages like C and C++."
- NDK Android Developers Portal
5. Intel Developers Relations Division 5
and maybe you are doing that already...
... let’s a few interesting NDK usages.
7. Intel Developers Relations Division 7
Game Engines
Others+…
*Other names and brands may be claimed as the property of others.
8. Intel Developers Relations Division 8
Computer Vision
• Linear and Non-Linear image filtering.
• Images Geometric Transformations.
• Moviments Prediction in Videos.
• Background segmentation on Videos.
• Object Tracking.
• ...
Outras: Cardboard-SDK, Vuforia, Layar, LibCCV,
Wikitude ...
OpenCV.org
*Other names and brands may be claimed as the property of others.
9. Intel Developers Relations Division 9
Cross Platform Development
*Other names and brands may be claimed as the property of others.
10. Intel Developers Relations Division 10
Other Usages and Libraries
Realm
Swift
Libpng
Intel TBB
Intel IPP
Libcairo and libpixman
Libsonic
Busybox
Speex
FFMpeg
OpenSSL
Lua
...
*Other names and brands may be claimed as the property of others.
13. Intel Developers Relations Division
Source: Using the NDK Performantly (Big Android BBQ 2015)
15
Architecture Overview
App Code
Framework Classes
Runtime
System Libraries
Linux Kernel
JNI
Your Libraries
Stable ABI
14. Intel Developers Relations Division 16
In practice...
Microchip Icon – Icon Finder.
App
0010011100
0011100111
x86
ARM
MIPS
Your code is:
• Closer to HW
• Platform Dependent
You must be careful about
it!
16. Intel Developers Relations Division 18
3 Golden Rules for NDK Adoption
1
2
3 Legacy Code that must be re-used and/or to expensive to port to Java*.
Specific performance problems.
Features and Experiences that demand performance.
17. Intel Developers Relations Division 19
3 (Main) Barriers for NDK Adoption
1
2
3 Perfomance gains are not assured.
Complexity of Implementation and Maintenance.
Platform Dependency and Compatibility.
18. Intel Developers Relations Division 20
We can, but we should not use NDK for everything.
So...
Still interested?
Take the “red pill” and we will show you how far this
can go...
20. Intel Developers Relations Division 22
Fundamentals: Java Native Interface (JNI)
Defines how Java and native code will interop.
Java C / C++
• Load lib with:
• System.loadLibrary()
• Definition of Native Methods:
• Reserved word native
• Include the JNI Header:
• #include <jni.h>
• Use specific JNI data types:
• jstring, jint, jboolean, etc.
• Special Variables:
• JNIEnv*, JavaVM*
21. Intel Developers Relations Division 23
JNI: Mapping Java Methods To C / C++
In C/C++ the function must:
Use and return Java JNI primitives and Objects:
Follows the naming standards:
Or register functions manually:
jint xxx(JNIEnv* env, jobject instance, ...) { ... }
Java_com_example_hellojni_HelloWorldJni_method
JNIEnv->RegisterNatives();
22. Intel Developers Relations Division 24
JNI: Example
Java
C / C++
public class HelloWorldJNI {
static {
System.loadLibrary("hello-jni");
}
public native String getHelloWorldStringFromJNI();
}
#include <jni.h>
jstring
Java_com_example_hellojni_HelloWorldJNI_getHelloWorldStringFromJNI(JNIEnv *env, jobject instance) {
return (*env)->NewStringUTF(env, "Hello World!");
}
23. Intel Developers Relations Division 25
Old but Gold
Building NDK Apps classic style.
JNICode
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
APP_ABI := all
jni/Android.mk jni/Application.mk
25. Intel Developers Relations Division 27
Old but Gold
Building NDK Apps (almost) classic
style.
• Add libs to jniLibs directory.
• Create java – jni interfaces.
• For libs pre-builts that is all.
• To build from source is a bit more
complicated but is possible as well..
26. Intel Developers Relations Division 28
Limitations
• Debug is not possible on AS. It is necessary to use ndk-gdb and/or Eclipse.
• Current support of gradle plugin was deprecated (ouch).
29. Intel Developers Relations Division 31
Important Warnings
• The experimental version of android gradle plugin will be used.
• Integration with Android Studio was improved but it stills unstable and has
some bugs.
• On Windows there are issues with the Editor and/or build that prevent its
usage. (Issues 195483 and 204552).
• Documentation is virtually inexistent or incomplete.
30. Intel Developers Relations Division 32
AS Integration
NDK Installation
and Configuration
are now integrated
to Android Studio.
38. Intel Developers Relations Division 40
gradle-experimental
• It is also possible:
• Configure pre-built libs;
• Add build flags (general and by flavor);
• The current documentation generally do not follow the releases, then:
• Keep an eye to NDK Samples for references!
39. Intel Developers Relations Division 42
Final Thoughts
When using NDK, direct or inderectly,add
support to as many platform architectures as
possibles.
Use NDK wisely: test, create benchmarks and
assure that it is the right solution to your
problem.
Follow and test the gradle-experimental plugin
for the NDK!
45. Intel Developers Relations Division
Security features enabled by Intel® AMT require an enabled chipset, network hardware and software and a corporate network connection. Intel AMT may not be available or
certain capabilities may be limited over a host OS-based VPN or when connecting wirelessly, on battery power, sleeping, hibernating or powered off. Setup requires
configuration and may require scripting with the management console or further integration into existing security frameworks, and modifications or implementation of new
business processes. For more information, see http://www.intel.com/technology/manage/iamt.
WiMAX connectivity requires a WiMAX enabled device and subscription to a WiMAX broadband service. WiMAX connectivity may require you to purchase additional software
or hardware at extra cost. Availability of WiMAX is limited, check with your service provider for details on availability and network limitations. Broadband performance and
results may vary due to environment factors and other variables. See www.intel.com/go/wimax for more information.
Intel® My WiFi Technology is an optional feature and requires additional software and a Centrino® wireless adapter. Wi-Fi devices must be certified by the Wi-Fi Alliance for
802.11b/g/a in order to connect. See mywifi.intel.com for more details.
Hyper-Threading Technology requires a computer system with a processor supporting HT Technology and an HT Technology-enabled chipset, BIOS and operating system.
Performance will vary depending on the specific hardware and software you use. For more information including details on which processors support HT Technology, see
here
No system can provide absolute security under all conditions. Requires an enabled chipset, BIOS, firmware and software and a subscription with a capable Service
Provider. Consult your system manufacturer and Service Provider for availability and functionality. Intel assumes no liability for lost or stolen data and/or systems or any
other damages resulting thereof. For more information, visit http://www.intel.com/go/anti-theft Intel® Turbo Boost Technology requires a PC with a processor with Intel
Turbo Boost Technology capability. Intel Turbo Boost Technology performance varies depending on hardware, software and overall system configuration. Check with your
PC manufacturer on whether your system delivers Intel Turbo Boost Technology. For more information, see http://www.intel.com/technology/turboboost
Requires an Intel® Wireless Display enabled PC, TV Adapter, and compatible television. Available on select Intel® Core processors. Does not support Blu-Ray or other
protected content playback. Consult your PC manufacturer. For more information, see www.intel.com/go/wirelessdisplay
(Built-in Visuals) Available on the 2nd gen Intel® Core™ processor family. Includes Intel® HD Graphics, Intel® Quick Sync Video, Intel® Clear Video HD Technology, Intel® InTru™
3D Technology, and Intel® Advanced Vector Extensions. Also optionally includes Intel® Wireless Display depending on whether enabled on a given system or not. Whether you
will receive the benefits of built-in visuals depends upon the particular design of the PC you choose. Consult your PC manufacturer whether built-in visuals are enabled on
your system. Learn more about built-in visuals at http://www.intel.com/technology/visualtechnology/index.htm.
Legal Notices and Disclaimers, cont.