Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Android NDK Beginner`s Guide - Second Edition
Android NDK Beginner`s Guide - Second Edition

Android NDK Beginner`s Guide - Second Edition: Discover the native side of Android and inject the power of C/C++ in your applications

eBook
€28.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Android NDK Beginner`s Guide - Second Edition

Chapter 1. Setting Up Your Environment

Are you ready to take up the mobile challenge? Is your computer switched on, mouse and keyboard plugged in, and screen illuminating your desk? Then let's not wait a minute more!

Developing Android applications requires a specific set of tools. You may already know about the Android Software Development Kit for pure Java applications. However, getting full access to the power of Android devices requires more: the Android Native Development Kit.

Setting up a proper Android environment is not that complicated, however it can be rather tricky. Indeed, Android is still an evolving platform and recent additions, such as Android Studio or Gradle, are not well supported when it comes to NDK development. Despite these annoyances, anybody can have a ready-to-work environment in an hour.

In this first chapter, we are going to:

  • Install prerequisites packages
  • Set up an Android development environment
  • Launch an Android emulator
  • Connect an Android device for development

Getting started with Android development

What differentiates mankind from animals is the use of tools. Android developers, the authentic species you belong to, are no different!

To develop applications on Android, we can use any of the following three platforms:

  • Microsoft Windows (XP and later)
  • Apple OS X (Version 10.4.8 or later)
  • Linux (distributions using GLibc 2.7 or later, such as latest versions of Ubuntu)

These systems are supported on x86 platforms (that is, PCs with processors such as Intel or AMD) in both 32- and 64-bit versions, except for Windows XP (32-bit only).

This is a good start but, unless you are able to read and write binary code as well as speak your mother tongue, having a raw OS is not enough. We also need software dedicated to Android development:

  • A JDK (Java Development Kit)
  • An Android SDK (Software Development Kit)
  • An Android NDK (Native Development Kit)
  • An IDE (Integrated Development Environment) such as Eclipse or Visual Studio (or vi for hard-core coders). Android Studio and IntelliJ are not yet well-suited for NDK development, although they provide basic support for native code.
  • A good old command-line shell to manipulate all these tools. We will use Bash.

Now that we know what tools are necessary to work with Android, let's start with the installation and setup process.

Note

The following section is dedicated to Windows. If you are a Mac or Linux user, you can jump to Setting up an OS X or Setting up Linux section.

Setting up Windows

Before installing the necessary tools, we need to set up Windows to host our Android development tools properly. Although it is not the most natural fit for Android development, Windows still provides a fully functional environment.

The following section explains how to set up the prerequisite packages on Windows 7. The process is the same for Windows XP, Vista, or 8.

Time for action – preparing Windows for Android development

To develop with the Android NDK on Windows, we need to set up a few prerequisites: Cygwin, a JDK, and Ant.

  1. Go to http://cygwin.com/install.html and download the Cygwin setup program suitable for your environment. Once downloaded, execute it.
  2. In the installation window, click on Next and then Install from Internet.
    Time for action – preparing Windows for Android development

    Follow the installation wizard screens. Consider selecting a download site from where Cygwin packages are downloaded in your country.

    Then, when proposed, include the Devel, Make, Shells, and bash packages:

    Time for action – preparing Windows for Android development

    Follow the installation wizard until the end. This may take some time depending on your Internet connection.

  3. Download Oracle JDK 7 from the Oracle website at http://www.oracle.com/technetwork/java/javase/downloads/index.html (or JDK 8, although it is not officially supported at the time this book is written). Launch and follow the installation wizard until the end.
  4. Download Ant from its website at http://ant.apache.org/bindownload.cgi and unzip its binary package in the directory of your choice (for example, C:\Ant).
  5. After installation, define JDK, Cygwin, and Ant locations in environment variables. To do so, open Windows Control Panel and go to the System panel (or right-click on the Computer item in the Windows Start menu and select Properties).

    Then, go to Advanced system settings. The System Properties window appears. Finally, select the Advanced tab and click on the Environment Variables button.

  6. In the Environment Variables window, inside the System variables list, add:
    • The CYGWIN_HOME variable with the Cygwin installation directory as the value (for example, C:\Cygwin)
    • The JAVA_HOME variable with the JDK installation directory as the value
    • The ANT_HOME variable with the Ant installation directory as the value (for example, C:\Ant)

    Prepend %CYGWIN_HOME%\bin;%JAVA_HOME%\bin;%ANT_HOME%\bin;, all separated by a semicolon, at the beginning of your PATH environment variable.

    Time for action – preparing Windows for Android development
  7. Finally, launch a Cygwin terminal. Your profile files get created on the first launch. Check the make version to ensure Cygwin works:
    make –version
    

    You will see the following output:

    Time for action – preparing Windows for Android development
  8. Ensure JDK is properly installed by running Java and checking its version. Check carefully to make sure the version number corresponds to the newly installed JDK:
    java –version
    

    You will see the following output on the screen:

    Time for action – preparing Windows for Android development
  9. From a classic Windows terminal, check the Ant version to make sure it is properly working:
    ant -version
    

    You will see the following on the terminal:

    Time for action – preparing Windows for Android development

What just happened?

Windows is now set up with all the necessary packages to host Android development tools:

  • Cygwin, which is an open source software collection, allows the Windows platform to emulate a Unix-like environment. It aims at natively integrating software based on the POSIX standard (such as Unix, Linux, and so on) into Windows. It can be considered as an intermediate layer between applications originated from Unix/Linux (but natively recompiled on Windows) and the Windows OS itself. Cygwin includes Make, which is required by the Android NDK compilation system to build native code.

    Tip

    Even if Android NDK R7 introduced native Windows binaries, which does not require a Cygwin runtime, it is still recommended to install the latter for debugging purpose.

  • A JDK 7, which contains the runtime and tools necessary to build Java applications on Android and run the Eclipse IDE as well as Ant. The only real trouble that you may encounter when installing a JDK is some interferences from a previous installation, such as an existing Java Runtime Environment (JRE). Proper JDK use can be enforced through the JAVA_HOME and PATH environment variables.

    Tip

    Defining the JAVA_HOME environment variable is not required. However, JAVA_HOME is a popular convention among Java applications, Ant being one of them. It first looks for the java command in JAVA_HOME (if defined) before looking in PATH. If you install an up-to-date JDK in another location later on, do not forget to update JAVA_HOME.

  • Ant, which is a Java-based build automation utility. Although not a requirement, it allows building Android applications from the command line, as we will see in Chapter 2, Starting a Native Android Project. It is also a good solution to set up a continuous integration chain.

The next step consists of setting up the Android development kits.

Installing Android development kits on Windows

Android requires specific development kits to develop applications: the Android SDK and NDK. Hopefully, Google has thought about the developer community and provides all the necessary tools for free.

In the following part, we will install these kits to start developing native Android applications on Windows 7.

Time for action – installing Android SDK and NDK on Windows

The Android Studio bundle already contains the Android SDK. Let's install it.

  1. Open your web browser and download the Android Studio bundle from http://developer.android.com/sdk/index.html.

    Run the downloaded program and follow the installation wizard. When requested, install all Android components.

    Time for action – installing Android SDK and NDK on Windows

    Then, choose the installation directories for Android Studio and the Android SDK (for example, C:\Android\android-studio and C:\Android\sdk).

  2. Launch Android Studio to ensure it is properly working. If Android Studio proposes to import settings from a previous installation, select your preferred option and click on OK.
    Time for action – installing Android SDK and NDK on Windows

    The Android Studio welcome screen should then appear. Close it.

    Time for action – installing Android SDK and NDK on Windows
  3. Go to http://developer.android.com/tools/sdk/ndk/index.html and download the Android NDK (not SDK!) suitable for your environment. Extract the archive inside the directory of your choice (for example, C:\Android\ndk).
  4. To easily access Android utilities from the command line, let's declare the Android SDK and NDK as environment variables. From now on, we will refer to these directories as $ANDROID_SDK and $ANDROID_NDK.

    Open the Environment Variables system window, as we did previously. Inside the System variables list, add the following:

    • The ANDROID_SDK variable with the SDK installation directory (for example, C:\Android\sdk)
    • The ANDROID_NDK variable with the NDK installation directories (for example, C:\Android\ndk)

    Prepend %ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%ANDROID_NDK%;, all separated by a semicolon, at the beginning of your PATH environment variable.

    Time for action – installing Android SDK and NDK on Windows
  5. All Windows environment variables should be imported automatically by Cygwin when launched. Open a Cygwin terminal and list the Android devices connected to your computer (even if none are currently) with adb to check whether SDK is working. No error should appear:
    adb devices
    
    Time for action – installing Android SDK and NDK on Windows
  6. Check the ndk-build version to ensure that NDK is working. If everything works, the Make version should appear:
    ndk-build -version
    
    Time for action – installing Android SDK and NDK on Windows
  7. Open Android SDK Manager, located in the ADB bundle directory's root.
    Time for action – installing Android SDK and NDK on Windows

    In the opened window, click on New to select all the packages and then click on the Install packages... button. Accept the licenses in the popup that appears and start the installation of Android development packages by clicking on the Install button.

    After a few long minutes, all packages are downloaded and a confirmation message indicating that the Android SDK manager has been updated appears.

    Validate and close the manager.

What just happened?

Android Studio is now installed on the system. Although it is now the official Android IDE, we are not going to use it much throughout the book because of its lack of support of the NDK. It is, however, absolutely possible to use Android Studio for Java development, and command line or Eclipse for C/C++.

The Android SDK has been set up through the Android Studio package. An alternative solution consists of manually deploying the SDK standalone package provided by Google. On the other hand, the Android NDK has been deployed manually from its archive. Both the SDK and NDK are made available through the command line thanks to a few environment variables.

To get a fully functional environment, all Android packages have been downloaded thanks to the Android SDK manager, which aims at managing all the platforms, sources, samples, and emulation features available through the SDK. This tool greatly simplifies the update of your environment when new SDK API and components are released. There is no need to reinstall or overwrite anything!

However, the Android SDK Manager does not manage the NDK, which explains why we downloaded it separately, and why you will need to update it manually in the future.

Tip

Installing all Android packages is not strictly necessary. Only the SDK platform (and possibly Google APIs) releases targeted by your application are really required. Installing all packages may avoid troubles when importing other projects or samples though.

The installation of your Android development environment is not over yet. We still need one more thing to develop comfortably with the NDK.

Note

This is the end of the section dedicated to the Windows setup. The following section is dedicated to OS X.

Setting up OS X

Apple computers have a reputation for being simple and easy to use. I must say that this adage is rather true when it comes to Android development. Indeed, as a Unix-based system, OS X is well adapted to run the NDK toolchain.

The following section explains how to set up the prerequisite packages on Mac OS X Yosemite.

Left arrow icon Right arrow icon

Description

Are you an Android Java programmer who needs more performance? Are you a C/C++ developer who doesn’t want to bother with the complexity of Java and its out-of-control garbage collector? Do you want to create fast intensive multimedia applications or games? If you’ve answered yes to any of these questions then this book is for you. With some general knowledge of C/C++ development, you will be able to dive headfirst into native Android development.

What you will learn

  • Build your first Android native project from scratch
  • Communicate with Java through Java Native Interfaces
  • Learn the key design intricacies of creating a native OpenGL ES 2.0 graphics application
  • Initialize, play, and record sound and music with OpenSL ES
  • Handle input events and sensors to create different interaction types
  • Port an existing library on Android by compiling most common C++ frameworks on Android
  • Interface and optimize the existing code with RenderScript
  • Combine graphics, sound, input, sensors, and physics in your application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 28, 2015
Length: 494 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989652
Vendor :
Google
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Apr 28, 2015
Length: 494 pages
Edition : 1st
Language : English
ISBN-13 : 9781783989652
Vendor :
Google
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 115.97
Android Game Programming By Example
€36.99
Learning Java by Building Android Games
€36.99
Android NDK Beginner`s Guide - Second Edition
€41.99
Total 115.97 Stars icon

Table of Contents

12 Chapters
1. Setting Up Your Environment Chevron down icon Chevron up icon
2. Starting a Native Android Project Chevron down icon Chevron up icon
3. Interfacing Java and C/C++ with JNI Chevron down icon Chevron up icon
4. Calling Java Back from Native Code Chevron down icon Chevron up icon
5. Writing a Fully Native Application Chevron down icon Chevron up icon
6. Rendering Graphics with OpenGL ES Chevron down icon Chevron up icon
7. Playing Sound with OpenSL ES Chevron down icon Chevron up icon
8. Handling Input Devices and Sensors Chevron down icon Chevron up icon
9. Porting Existing Libraries to Android Chevron down icon Chevron up icon
10. Intensive Computing with RenderScript Chevron down icon Chevron up icon
11. Afterword Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6
(8 Ratings)
5 star 25%
4 star 50%
3 star 0%
2 star 12.5%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




iPaul Aug 20, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good intro to the NDK usage with Android Studio.
Amazon Verified review Amazon
Fabio Radin Jul 29, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I love this book! This is a great book to start with the Android NDK, even it it is targeted to someone that has some experience developing on Android and has some programming experience in C/C++. I really appreciated the chapters on setting it up for Linux, Mac and Windows and, over the top, inside Android Studio!I liked a lot that it provides information about integrating existing C++ code in Android, as OpenGL from C++, and integrating both Box2D, and the Irrlicht open source 3d game engine.
Amazon Verified review Amazon
jdc Oct 02, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is a tricky one to review. I'm giving 4 stars because I think it's coverage of the NDK is fairly good but I'm personally not too interested in writing app completely in C++. I would have preferred the book to focus more on the interaction between Java and C++ since to get the most out of Android you really have to use Java
Amazon Verified review Amazon
Krunal Jhaveri Jul 13, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is really useful. It is right from the scratch and covers a wide range of topics. Examples are lucid and easy to implement.
Amazon Verified review Amazon
Robin T. Wernick May 25, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book on the Android NDK is the most comprehensive source of NDK examples in both C and C++ that I have seen in five years of searching. Had it included program development on the Android Studio instead of the obsolete and often unconfigurable Eclipse, I would have given it five stars.I'm very glad to have this book because it's clarity and organization helped to clarify some lingering issues concerning the details of using C(++) libraries and controlling sensors that were overlooked in most of the previous sources. Now, I have a source that tells me in detail how to control the UI and respond to attitude and touch events in the NDK. Reading sensors and files are also included.I highly recommend buying this book if you want to work with the NDK and potentially build C(++) libraries that can support both Android and iPhone mobile applications.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.