Skip to main content
main-content

Über dieses Buch

Android is one of the major players in the mobile phone market. Android is a mobile platform that is built on the top of Linux operating system. The native-code support on Android offers endless opportunities to application developers, not limited the functionality that is provided by Android framework. Pro Android C++ with the NDK is an advanced tutorial and professional reference for today's more sophisticated app developers now porting, developing or employing C++ and other native code to integrate into the Android platform to run sophisticated native apps and better performing apps in general. Using a game app case study, this book explores tools for troubleshooting, debugging, analyzing memory issues, unit testing, unit test code coverage, performance measurement, on native applications, as well as integrating the Android NDK toolchain into existing Autoconf, Makefile, CMake, or JAM based build systems. Pro Android C++ with the NDK also covers the following:

·

The Android platform, and getting up to speed with the Android NDK, and exploring the APIs that are provided in native space. An overview of Java Native Interface (JNI), and auto-generating JNI code through Simplified Wrapper and Interface Generator (SWIG). An introduction to Bionic API, native networking. native multithreading, and the C++ Standard Template Library (STL) support. Native graphics and sound using JNI Graphics, OpenGL ES, and OpenSL ES. Debugging and troubleshooting native applications using Logging, GNU Debugger (GDB), Eclipse Debugger, Valgrind, strace, and other tools. Profiling native code using GProf to identify performance bottlenecks, and NEON/SIMD optimization from an advanced perspective, with tips and recommendations.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Getting Started with C++ on Android

Abstract
Needless to say, exploring and practicing are the best methods for learning. Having a fully functional development environment ready at the very beginning of this book will enable you to explore and experiment with the material while working through the chapters. The Android C++ development environment is mainly formed by the following components:
  • Android Software Development Kit (SDK)
  • Android Native Development Kit (NDK)
  • Android Development Tools (ADT) Plug-In for Eclipse
  • Java Development Kit (JDK)
  • Apache ANT Build System
  • GNU Make Build System
  • Eclipse IDE
Onur Cinar

Chapter 2. Exploring the Android NDK

Abstract
In the previous chapter, you configured your development environment by installing Android development tools and dependencies. Among these tools, the Android Native Development Kit (NDK) is the tool you will be using for C++ development on Android platform. The Android NDK is a companion toolset for the Android Software Development Kit (SDK), designed to augment the Android SDK to allow developers to implement and embed performance-critical portions of their applications using machine code-generating programming languages like C, C++, and Assembly.
Onur Cinar

Chapter 3. Communicating with Native Code using JNI

Abstract
In the previous chapter, you started exploring the Android NDK by going through its components, its structure, and its build system. Using this information you can now build and package any kind of native code with your Android applications. In this chapter, you will focus on the integration part by using the Java Native Interface (JNI) technology to enable the Java application and the native code to communicate with each other.
Onur Cinar

Chapter 4. Auto-Generate JNI Code Using SWIG

Abstract
In the previous chapter you explored JNI technology and you learned how to connect native code to a Java application. As noted, implementing JNI wrapper code and handling the translation of data types is a cumbersome and time-consuming development task. This chapter will introduce the Simplified Wrapper and Interface Generator (SWIG) , a development tool that can simplify this process by automatically generating the necessary JNI wrapper code.
Onur Cinar

Chapter 5. Logging, Debugging, and Troubleshooting

Abstract
In previous chapters, you explored the Android NDK build system and how to connect the native code to the Java application using the JNI technology. Needless to say, learning application development on a new platform involves much experimentation; it takes time to get things right. It is vital to gain the troubleshooting skills pertaining to Android platform before starting to experiment with the native APIs offered, as it can catalyze the learning phase greatly by helping you to spot problems quickly. Your existing troubleshooting skills may not directly apply since the development and execution of Android applications happens on two different machines.
Onur Cinar

Chapter 6. Bionic API Primer

Abstract
In previous chapter, you explored the logging, debugging, and troubleshooting tools and techniques pertaining to Android native application development. Starting with this chapter, you will be exploring the native APIs provided by the Android NDK.
Onur Cinar

Chapter 7. Native Threads

Abstract
A thread is a mechanism enabling a single process to perform multiple tasks concurrently. Threads are lightweight processes sharing the same memory and resources of the same parent process. A single process can contain multiple threads executing in parallel. As part of the same process, threads can communicate with each other and share data. Android supports threads in both Java and the native code. In this chapter, you will be exploring different strategies and APIs that can be used for concurrent programming pertaining to native code.
Onur Cinar

Chapter 8. POSIX Socket API: Connection-Oriented Communication

Abstract
As they get executed in an isolated environment distant from the user, native code applications require a medium of communication either with their parent applications or the external world in order to provide any services. In Chapter 3 , you explored the JNI technology enabling the native code to communicate with its parent Java application. Starting with this chapter, you will start exploring the POSIX Socket APIs available through Bionic that enable the native code to communicate with the external world directly without calling into the Java layer.
Onur Cinar

Chapter 9. POSIX Socket API: Connectionless Communication

Abstract
In the previous chapter, you started exploring the POSIX Socket APIs by going through an example of a connection-oriented communication application using the TCP protocol. In this chapter, you will learn how to establish a connectionless communication between the Android application and a remote end-point. Connectionless communication through UDP sockets provides a lightweight communication medium tailed for real-time applications that can work with unordered and lost data packets. This type of connection does not maintain an open connection. Packets get sent to the target protocol address as needed. Since there is no connection in place, packets may get lost or get out of order during transition. The protocol does not provide any service to handle such situations. Throughout this chapter you will continue to modify the example Echo application to include both UDP server and client native implementations.
Onur Cinar

Chapter 10. POSIX Socket API: Local Communication

Abstract
In the previous two chapters, you explored the POSIX Socket API as it pertains to communication with remote parties. The POSIX Socket API can also be used to establish a communication channel locally on the device between two applications, or between the native and Java layers. In this chapter, you will continue to build on top of the Echo example application. The local socket communication example will demonstrate the following:
  • Local socket server implementation in the native layer.
  • Local client implementation in the Java layer.
  • Establishing a local socket communication between two applications.
Onur Cinar

Chapter 11. C++ Support

Abstract
In the previous chapters you explored the functionality that is offered by the Bionic C standard library. Bionic provides frequently needed basic constructs and a common abstract interface to interact with the functionality provided through the operating system and the hardware. Compared to the Java framework, the extent of generic constructs that are offered by Bionic is fairly minimal. In addition to the standard C library, the C++ ISO standard specifies an additional standard library for the C++ programming language, known as the C++ standard library. This library provides several generic containers, strings, streams, and everyday utility functions. Through the building bricks that it provides, the C++ standard library simplifies the native development by allowing the developers to focus on the actual application logic rather than developing the constructs that are necessary to implement the logic. This takes C++ development to a higher level of productivity and promotes code reuse.
Onur Cinar

Chapter 12. Native Graphics API

Abstract
Needless to say, games and multimedia applications benefit from the Android NDK the most. These applications rely on native code for performance-critical operations. Having the capability to render graphics directly to the display from within the native layer is a highly crucial for such applications. This chapter will explore the following set of native graphics APIs that are provided through the Android NDK :
  • JNI Graphics API (aka Bitmap API)
  • OpenGL ES 1.x and 2.0
  • Native Window API
Onur Cinar

Chapter 13. Native Sound API

Abstract
In the previous chapter, you explored the multiple flavors of the native graphics APIs that are provided by the Android platform. Starting from Android OS version 2.3, API Level 9, the Android platform also provides a native sound API, enabling the native code to play and record audio without invoking any method at Java layer. Android native sound support is based on the OpenSL ES 1.0.1 standard from Khronos Group. OpenSL ES is the short form of the Open Sound Library for Embedded Systems. This chapter will briefly demonstrate the OpenSL ES native sound API pertaining to Android platform.
Onur Cinar

Chapter 14. Profiling and NEON Optimization

Abstract
In the previous chapters, you learned how to develop native applications on the Android platform. You explored the native APIs that are provided by both the Android platform and the Linux operating system. The following key topics will be covered on this last chapter:
  • Profiling the native Android applications to identify performance bottlenecks using the GNU Profiler.
  • Optimizing native applications using ARM NEON technology through compiler intrinsics.
  • Enabling automatic vectorization support in the compiler to seamlessly boost the performance of native applications without changing the source code.
Onur Cinar

Backmatter

Weitere Informationen

Premium Partner