Chapter 9. Porting Existing Libraries to Android

There are two main reasons why one would be interested in the Android NDK: first, for performance, and, second, for portability. In the previous chapters, we saw how to access the main native Android APIs from native code for efficiency purposes. In this chapter, we will bring the whole C/C++ ecosystem to Android, well, at least discovering the path, as decades of C/C++ development would be difficult to fit the limited memory of mobile devices anyway! Indeed, C and C++ are still some of the most widely used programming languages nowadays.

In previous NDK releases, portability was limited due to the partial support of C++, especially Exceptions and Run-Time Type Information (RTTI, a basic C++ reflection mechanism to get data types at runtime such as instanceof in Java). Any library requiring them could not be ported without modifying their code or installing a custom NDK (the Crystax NDK, rebuilt by the community from official sources, and available at http://www.crystax.net/). Hopefully, many of these restrictions have been lifted since (except wide character support).

Although not necessarily difficult, porting an existing library is not a trivial process. A few APIs might be missed (despite good POSIX support), some #define directives have to be tweaked, some dependencies have to be ported, as well as dependencies of dependencies. Some libraries will be easy to port, while some other will involve more effort.

In this chapter, in order to port existing code to Android, we are going to learn how to do the following code:

  • Activate the Standard Template Library (STL)
  • Port the Box2D physics engine
  • Prebuild and use the Boost framework
  • Discover more in-depth how to write NDK module Makefiles

By the end of this chapter, you should understand the native building process and know how to use Makefiles appropriately.

Activating the Standard Template Library

The Standard Template Library is a normalized library of containers, iterators, algorithms, and helper classes to ease most common programming operations, such as dynamic arrays, associative arrays, strings, sorting, and so on. This library gained recognition among developers over the years and is widely spread. Developing in C++ without the STL is like coding with one hand behind your back!

In this first part, let's embed GNU STL in DroidBlaster to ease collection management.

Note

Resulting project is provided with this book under the name DroidBlaster_Part16.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.62