Preface
Mobility and the demand for high-performance computations are often very tightly coupled. Current mobile applications do many computationally-intense operations such as 3D and stereoscopic rendering, images and audio recognition, and video decoding and encoding, especially the birth of new technologies such as the augmented reality. This include mobile games, 3D user interface software, and social software, which involves media stream processing.
In some sense, mobile game development forces us to travel back in time several years due to the limited hardware capabilities, low memory bandwidth, and precious battery resources, but also makes us reconsider the basic forms of interaction with the user.
A smooth and responsive user interface based on gesture input, Internet access, ambient sound effects, high-quality text, and graphics are the ingredients of a successful mobile application.
All major mobile operating systems give software developers different possibilities to develop close-to-the-hardware. Google provides an Android Native Development Kit (NDK) to ease the porting of existing applications and libraries from other platforms to Android, and exploit the performance of the underlying hardware offered by the modern mobile devices. C, and especially C++, both have a reputation of being a hard language to learn, and a hard language to write user interface code in. This is indeed true, but only when someone attempts to write everything from scratch. In this book we use C and C++ programming languages, and link them to well-established third-party libraries to allow the creation of content-rich applications with a modern touch-based interface and access to the Representational State Transfer (REST) APIs of popular sites such as Facebook, Twitter, Flickr, Picasa, Instagram, and a myriad of others.
Despite the availability of the information on how to use Internet resources in the applications written in Java or .NET languages, not much has been said about doing this in C++ programming language. Modern OpenGL versions require a sufficient amount of effort to create and use the latest extensions. The programming using the OpenGL API is usually described in literature in a platform-specific way. Things get even more complicated with the mobile version, the OpenGL ES, as developers have to adapt existing shader programs to allow them to run on the mobile graphics processing units (GPUs). Sound playback using standard Android facilities in C++ is also not straightforward, for example, things should be done to re-use the existing PC code for the OpenAL library. This book tries to shed some light on these topics and combine a number of useful recipes to simplify the multiplatform-friendly development with Android NDK.
Android is a mobile operating system based on the Linux kernel and designed for smartphones, tablet computers, netbooks, and other portable devices. Initial development of Android was started by Android Inc, which was bought by Google in 2005. In November 2007, the first version was unveiled, however, the first commercially available Android-based smartphone, HTC Dream, was released almost one year later in 2008.
Android versions, besides a numerical denomination, have official code namesâeach major release is named after a sweet dessert. The following are some significant milestones in Android platform technologies and features related to NDK:
- Version 1.5 (Cupcake): This Android version featured the first release of Android Native Development Kit supporting ARMv5TE instructions.
- Version 1.6 (Donut): First introduction of OpenGL ES 1.1 native library support.
- Version 2.0 (Eclair): OpenGL ES 2.0 native library support.
- Version 2.3 (Gingerbread):
- Concurrent garbage collector in Dalvik VM. This has faster gaming performance and improved efficiency of OpenGL ES operations.
- Capabilities of Native Development Kit are greatly extended, including sensors access, native audio OpenSL ES, the EGL library, activity life cycle management, and native access to assets.
- Version 3.0 (Honeycomb):
- Support for tablet computers with large touch screens
- Support of multicore processors
- Version 4.0 (Ice Cream Sandwich):
- Unified UI for smartphones and tablet
- Hardware-accelerated 2D rendering. VPN client API
- Versions 4.1 and 4.2 (Jelly Bean):
- This has improved rendering performance and triple buffering
- External display support, including external displays over Wi-Fi
- They have high-dynamic range camera support
- New built-in developer options for debugging and profiling. Dalvik VM runtime optimizations
- Version 4.3 (Jelly Bean): OpenGL ES 3.0 native library support.
- Version 4.4 (KitKat): Introduced access to RenderScript from NDK. This feature is backwards compatible with any device running Android 2.2 or higher.
Android Native Development Kit (NDK) is used for multimedia applications that require performance that Dalvik is unable to provide, and direct access to the native system libraries. NDK is also the key for portability, which in turn allows a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains or alike. The typical usage of NDK determines the scope of this bookâintegration of some of the most commonly used C/C++ libraries for graphics, sound, networking, and resource storage.
Initially, NDK was based on the Bionic library. It is a derivation of the BSD standard C library (libc) developed by Google for Android. The main goals of Bionic were as follows:
- License: Original GNU C Library (glibc) is GPL-licensed and Bionic has a BSD license.
- Size: Bionic is much smaller in size compared to GNU C Library.
- Speed: Bionic is designed for mobile CPUs with relatively low clock frequencies. For example, it has a custom implementation of pthreads.
Bionic lacks many important features found in full libc implementations, such as RTTI and C++ exceptions handling support. However, NDK provides several libraries with different C++ helper runtimes which implement these features. These are GAbi++ runtime, STLport runtime, and GNU Standard C++ Library. Besides the basic POSIX features, Bionic has support for Android-specific mechanisms such as logging.
The NDK is a very effective way to reuse a great body of existing C and C++ code.