Tải bản đầy đủ (.pdf) (436 trang)

Android NDK beginner's guide (2012, ratabouil s )

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.85 MB, 436 trang )

Android NDK
Beginner's Guide
Discover the nave side of Android and inject the power
of C/C++ in your applicaons
Sylvain Ratabouil
BIRMINGHAM - MUMBAI
Android NDK
Beginner's Guide
Copyright © 2012 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon contained in this book is sold without
warranty, either express or implied. Neither the author nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly
or indirectly by this book.
Packt Publishing has endeavored to provide trademark informaon about all of the companies
and products menoned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this informaon.
First published: January 2012
Producon Reference: 1200112
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-152-9
www.packtpub.com
Cover Image by Marcus Grandon ()
Credits


Author
Sylvain Ratabouil
Reviewers
Marko Gargenta
Dr. Frank Grützmacher
Robert Mitchell
Acquision Editor
Sarah Cullington
Lead Technical Editor
Dayan Hyames
Technical Editor
Pramila Balan
Copy Editor
Laxmi Subramanian
Project Coordinator
Jovita Pinto
Proofreader
Lynda Sliwoski
Indexer
Hemangini Bari
Graphics
Valenna D'silva
Producon Coordinators
Prachali Bhiwandkar
Melwyn D'sa
Nilesh Mohite
Cover Work
Alwin Roy
About the Author
Sylvain Ratabouil is a conrmed IT consultant with experience in C++ and Java

technologies. He worked for the space industry and got involved in aeronauc projects at
Valtech Technologies where he now takes part in the Digital Revoluon.
Sylvain earned the master's degree in IT from Paul Sabaer University in Toulouse and did
M.Sc. in Computer Science from Liverpool University.
As a technology lover, he is passionate about mobile technologies and cannot live or sleep
without his Android smartphone.
I would like to thank Steven Wilding for oering me to write this book;
Sneha Harkut and Jovita Pinto for awaing me with so much paence;
Reshma Sundaresan, and Dayan Hyames for pung this book on the
right track; Sarah Cullington for helping me nalizing this book;
Dr. Frank Grützmacher, Marko Gargenta, and Robert Mitchell for
all their helpful comments.
About the Reviewers
Dr. Frank Grützmacher has worked for several major German rms in the area of large
distributed systems. He was an early user of dierent Corba implementaons in the past.
He got his Ph.D. in the eld of electrical engineering, but with the focus on distributed
heterogeneous systems. In 2010, he was involved in a project, which changed parts of the
Android plaorm for a manufacturer. From there, he got his knowledge about the android
NDK and nave processes on this plaorm.
He has already worked as a reviewer for another Android 3.0 book.
Robert Mitchell is an MIT graduate with over 40 years experience in Informaon
Technology and is semirered. He has developed soware for all the big iron companies:
IBM, Amdahl, Fujitsu, Naonal Semiconductor, and Storage Technology. Soware companies
include Veritas and Symantec. Recent languages that he knows are Ruby and Java, with a
long background in C++.
www.PacktPub.com
Support les, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support les and downloads related to
your book.
Did you know that Packt oers eBook versions of every book published, with PDF and ePub

les available? You can upgrade to the eBook version at
www.PacktPub.com and as a print
book customer, you are entled to a discount on the eBook copy. Get in touch with us at
for more details.
At
www.PacktPub.com, you can also read a collecon of free technical arcles, sign up for
a range of free newsleers and receive exclusive discounts and oers on Packt books and
eBooks.

Do you need instant soluons to your IT quesons? PacktLib is Packt's online digital book
library. Here, you can access, read and search across Packt's enre library of books.
Why Subscribe?
 Fully searchable across every book published by Packt
 Copy and paste, print and bookmark content
 On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view nine enrely free books. Simply use your login credenals for
immediate access.
Table of Contents
Preface 1
Chapter 1: Seng Up your Environment 7
Geng started with Android development 7
Seng up Windows 8
Time for acon – preparing Windows for Android development 8
Installing Android development kits on Windows 12
Time for acon – installing Android SDK and NDK on Windows 13
Seng up Mac OS X 18
Time for acon – preparing Mac OS X for Android development 18
Installing Android development kits on Mac OS X 20

Time for acon – installing Android SDK and NDK on Mac OS X 20
Seng up Linux 22
Time for acon – preparing Ubuntu Linux for Android development 22
Installing Android development kits on Linux 27
Time for acon – installing Android SDK and NDK on Ubuntu 27
Seng up the Eclipse development environment 29
Time for acon – installing Eclipse 29
Emulang Android 33
Time for acon – creang an Android virtual device 33
Developing with an Android device on Windows and Mac OS X 37
Time for acon – seng up your Android device on Windows and Mac OS X 37
Developing with an Android device on Linux 39
Time for acon – seng up your Android device on Ubuntu 39
Troubleshoong a development device 42
Summary 43
Chapter 2: Creang, Compiling, and Deploying Nave Projects 45
Compiling and deploying NDK sample applicaons 46
Time for acon – compiling and deploying the hellojni sample 46
Table of Contents
[ ii ]
Exploring Android SDK tools 51
Android debug bridge 51
Project conguraon tool 54
Creang your rst Android project using eclipse 56
Time for acon – iniang a Java project 56
Introducing Dalvik 59
Interfacing Java with C/C++ 60
Time for acon – calling C code from Java 60
More on Makeles 65
Compiling nave code from Eclipse 67

Time for acon – creang a hybrid Java/C/C++ project 67
Summary 72
Chapter 3: Interfacing Java and C/C++ with JNI 73
Working with Java primives 74
Time for acon – building a nave key/value store 75
Referencing Java objects from nave code 85
Time for acon – saving a reference to an object in the Store 85
Local and global JNI references 90
Throwing excepons from nave code 91
Time for acon – raising excepons from the Store 92
JNI in C++ 96
Handling Java arrays 96
Time for acon – saving a reference to an object in the Store 97
Checking JNI excepons 106
Summary 107
Chapter 4: Calling Java Back from Nave Code 109
Synchronizing Java and nave threads 110
Time for acon – running a background thread 111
Aaching and detaching threads 120
More on Java and nave code lifecycles 121
Calling Java back from nave code 122
Time for acon – invoking Java code from a nave thread 122
More on callbacks 133
JNI method denions 134
Processing bitmaps navely 135
Time for acon – decoding camera feed from nave code 136
Summary 146
Chapter 5: Wring a Fully-nave Applicaon 147
Creang a nave acvity 148
Time for acon – creang a basic nave acvity 148

Table of Contents
[ iii ]
Handling acvity events 155
Time for acon – handling acvity events 155
More on Nave App Glue 166
UI thread 167
Nave thread 168
Android_app structure 170
Accessing window and me navely 171
Time for acon – displaying raw graphics and implemenng a mer 172
More on me primives 181
Summary 181
Chapter 6: Rendering Graphics with OpenGL ES 183
Inializing OpenGL ES 184
Time for acon – inializing OpenGL ES 184
Reading PNG textures with the asset manager 193
Time for acon – loading a texture in OpenGL ES 194
Drawing a sprite 208
Time for acon – drawing a Ship sprite 209
Rendering a le map with vertex buer objects 220
Time for acon – drawing a le-based background 221
Summary 238
Chapter 7: Playing Sound with OpenSL ES 239
Inializing OpenSL ES 241
Time for acon – creang OpenSL ES engine and output 241
More on OpenSL ES philosophy 248
Playing music les 249
Time for acon – playing background music 249
Playing sounds 256
Time for acon – creang and playing a sound buer queue 257

Event callback 266
Recording sounds 268
Summary 272
Chapter 8: Handling Input Devices and Sensors 273
Interacng with Android 274
Time for acon – handling touch events 276
Detecng keyboard, D-Pad, and Trackball events 288
Time for acon – handling keyboard, D-Pad, and trackball, navely 289
Probing device sensors 298
Time for acon – turning your device into a joypad 300
Summary 313
Table of Contents
[ iv ]
Chapter 9: Porng Exisng Libraries to Android 315
Developing with the Standard Template Library 316
Time for acon – embedding GNU STL in DroidBlaster 316
Stac versus shared 326
STL performances 327
Compiling Boost on Android 328
Time for acon – embedding Boost in DroidBlaster 328
Porng third-party libraries to Android 338
Time for acon – compiling Box2D and Irrlicht with the NDK 339
GCC opmizaon levels 346
Mastering Makeles 346
Makele variables 347
Makele Instrucons 348
Summary 351
Chapter 10: Towards Professional Gaming 353
Simulang physics with Box2D 353
Time for acon – simulang physics with Box2D 354

More on collision detecon 366
Collision modes 367
Collision ltering 368
More resources about Box2D 369
Running a 3D engine on Android 369
Time for acon – rendring 3D graphics with Irrlicht 370
More on Irrlicht scene management 381
Summary 382
Chapter 11: Debugging and Troubleshoong 383
Debugging with GDB 383
Time for acon – debugging DroidBlaster 384
Stack trace analysis 392
Time for acon – analysing a crash dump 392
More on crash dumps 396
Performance analysis 397
Time for acon – running GProf 398
How it works 403
ARM, thumb, and NEON 403
Summary 405
Index 411
Preface
The short history of compung machines has witnessed some major events, which
forever transformed our usage of technology. From the rst massive main frames to
the democrazaon of personal computers, and then the interconnecon of networks.
Mobility is the next revoluon. Like the primive soup, all the ingredients are now
gathered: an ubiquitous network, new social, professional and industrial usages, a
powerful technology. A new period of innovaon is blooming right now in front of our
eyes. We can fear it or embrace it, but it is here, for good!
The mobile challenge
Today's mobile devices are the product of only a few years of evoluon, from the rst

transportable phones to the new ny high-tech monsters we have in our pocket. The
technological me scale is denitely not the same as the human one.
Only a few years ago, surng on the successful wave of its musical devices, Apple and
its founder Steve Jobs combined the right hardware and the right soware at the right
me not only to sasfy our needs, but to create new ones. We are now facing a new
ecosystem looking for a balance between iOS, Windows Mobile, Blackberry, WebOS, and
more importantly Android! The appete of a new market could not let Google apathec.
Standing on the shoulder of this giant Internet, Android came into the show as the best
alternave to the well established iPhones and other iPads. And it is quickly becoming
the number one.
In this modern Eldorado, new usages or technically speaking, applicaons (acvies, if
you already are an Android adept) sll have to be invented. This is the mobile challenge.
And the dematerialized country of Android is the perfect place to look for. Android is
(mostly) an open source operang system now supported by a large panel of mobile
device manufacturers.
Preface
[ 2 ]
Portability among hardware and adaptability to the constrained resources of mobile devices:
this is the real essence of the mobile challenge from a technical perspecve. With Android,
ones has to deal with mulple screen resoluons, various CPU and GPU speed or capabilies,
memory limitaons, and so on, which are not topics specic to this Linux-based system,
(that is, Android) but can parcularly be incommoding.
To ease portability, Google engineers packaged a virtual machine with a complete framework
(the Android SDK) to run programs wrien in one of the most spread programming language
nowadays: Java. Java, augmented with the Android framework, is really powerful. But rst,
Java is specic to Android. Apple's products are wrien for example in Objecve C and can be
combined with C and C++. And second, a Java virtual machine does not always give you enough
capability to exploit the full power of mobile devices, even with just-in-me compilaon
enabled. Resources are limited on these devices and have to be carefully exploited to oer
the best experience. This is where the Android Nave Development Kit comes into place.

What this book covers
Chapter 1, Seng Up your Environment, covers the tools required to develop an applicaon
with the Android NDK. This chapter also covers how to set up a development environment,
connect your Android device, and congure the Android emulator.
Chapter 2, Creang, Compiling, and Deploying Nave Projects, we will compile, package, and
deploy NDK samples and create our rst Android Java/C hybrid project with NDK and Eclipse.
Chapter 3, Interfacing Java and C/C++ with JNI, presents how Java integrates and
communicates with C/C++ through Java Nave Interface.
Chapter 4, Calling Java Back from Nave Code, we will call Java from C to achieve
bidireconal communicaon and process graphic bitmaps navely.
Chapter 5, Wring a Fully-nave Applicaon, looks into the Android NDK applicaon life-cycle.
We will also write a fully nave applicaon to get rid of Java.
Chapter 6, Rendering Graphics with OpenGL ES, teaches how to display advanced 2D and 3D
graphics at full speed with OpenGL ES. We will inialize display, load textures, draw sprites
and allocate vertex and index buers to display meshes.
Chapter 7, Playing Sound with OpenSL ES, adds a musical dimension to nave applicaons
with OpenSL ES, a unique feature provided only by the Android NDK. We will also record
sounds and reproduce them on the speakers.
Preface
[ 3 ]
Chapter 8, Handling Input Devices and Sensors, covers how to interact with an Android
device through its mul-touch screen. We will also see how to handle keyboard events
navely and apprehend the world through sensors and turn a device into a game controller.
Chapter 9, Porng Exisng Libraries to Android, we will compile the indispensable C/C++
frameworks, STL and Boost. We will also see how to enable excepons and RunTime Type
Informaon. And also port our own or third-party libraries to Android, such as, Irrlicht 3D
engine and Box2D physics engine.
Chapter 10, Towards Professional Gaming, creates a running 3D game controlled with
touches and sensors using Irrlicht and Box2D.
Chapter 11, Debugging and Troubleshoong, provides an in-depth analysis of the running

applicaon with NDK debug ulity. We will also analyze crash dumps and prole the
performance of our applicaon.
What you need for this book
A PC with either Windows or Linux or an Intel-based Mac. As a test machine, an Android device
is highly advisable, although the Android NDK provides an emulator which can sasfy most of
the needs of a hungry developer. But for 2D and 3D graphics, it is sll too limited and slow.
I assume you already understand C and C++ languages, pointers, object-oriented features,
and other modern language concepts. I also assume you have some knowledge about
the Android plaorm and how to create Android Java applicaons. This is not a strong
prerequisite, but preferable. I also guess you are not frighten by command-line terminals.
The version of Eclipse used throughout this book is Helios (3.6).
Finally, bring all your enthusiasm because these lile beasts can become really amazing
when they demonstrate all their potenal and sense of contact.
Who this book is for
Are you an Android Java programmer who needs more performance? Are you a C/C++
developer who doesn't want to bother with Java stu and its out-of-control garbage
collector? Do you want to create fast intensive mulmedia applicaons or games? Answer
yes to any of the above quesons and this book is for you. With some general knowledge
of C/C++ development, you will be able to dive head rst into nave Android development.
Preface
[ 4 ]
Conventions
In this book, you will nd several headings appearing frequently.
To give clear instrucons of how to complete a procedure or task, we use:
Time for action – heading
1. Acon 1
2. Acon 2
3. Acon 3
Instrucons oen need some extra explanaon so that they make sense, so they are
followed with:

What just happened?
This heading explains the working of tasks or instrucons that you have just completed.
You will also nd some other learning aids in the book, including:
Pop quiz – heading
These are short mulple choice quesons intended to help you test your own understanding.
Have a go hero – heading
These set praccal challenges and give you ideas for experimenng with what you
have learned.
You will also nd a number of styles of text that disnguish between dierent kinds of
informaon. Here are some examples of these styles, and an explanaon of their meaning.
Code words in text are shown as follows: "Open a command line window and key in
java –version to check the installaon."
A block of code is set as follows:
export ANT_HOME=`cygpath –u "$ANT_HOME"`
export JAVA_HOME=`cygpath –u "$JAVA_HOME"`
export ANDROID_SDK=`cygpath –u "$ANDROID_SDK"`
export ANDROID_NDK=`cygpath –u "$ANDROID_NDK"`
Preface
[ 5 ]
When we wish to draw your aenon to a parcular part of a code block, the relevant lines
or items are set in bold:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" /> package="com.example.hellojni"
android:versionCode="1"
android:versionName="1.0">
Any command-line input or output is wrien as follows:
$ make –version
New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in the text like this: "When proposed, include
Devel/make and Shells/bash packages".

Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to develop
tles that you really get the most out of.
To send us general feedback, simply send an e-mail to
, and
menon the book tle through the subject of your message.
If there is a topic that you have experse in and you are interested in either wring or
contribung to a book, see our author guide on
www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help
you to get the most from your purchase.
Preface
[
6
]
Downloading the example code
You can download the example code les for all Packt books you have purchased from your
account at

. If you purchased this book elsewhere, you can
visit
/> and register to have the les e-mailed directly
to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do
happen. If you nd a mistake in one of our books—maybe a mistake in the text or the

code—we would be grateful if you would report this to us. By doing so, you can save other
readers from frustraon and help us improve subsequent versions of this book. If you
nd any errata, please report them by vising
selecng your book, clicking on the errata submission form link, and entering the details
of your errata. Once your errata are veried, your submission will be accepted and the
errata will be uploaded to our website, or added to any list of exisng errata, under the
Errata secon of that tle.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At
Packt, we take the protecon of our copyright and licenses very seriously. If you come
across any illegal copies of our works, in any form, on the Internet, please provide us with
the locaon address or website name immediately so that we can pursue a remedy.
Please contact us at

with a link to the suspected pirated material.
We appreciate your help in protecng our authors, and our ability to bring you
valuable content.
Questions
You can contact us at

if you are having a problem with any
aspect of the book, and we will do our best to address it.
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 illuminang your desk? Then let’s
not wait a minute more!
In this rst chapter, we are going to do the following:
 Download and install the necessary tools to develop applicaons using Android
 Set up a development environment

 Connect and prepare an Android device for development
Getting started with Android development
What dierenates mankind from animals is the use of tools. Android developers,
this authenc species you are about to belong to, are no dierent!
To develop applicaons on Android, we can use any of the following three plaorms:
 Microso Windows PC
 Apple Mac OS X
 Linux PC
Windows 7, Vista, Mac OS X, and Linux systems are supported in both 32 and 64-bit versions,
but Windows XP in 32-bit mode only. Only Mac OS X computers of version 10.5.8 or later and
based on Intel architectures are supported (not PowerPC processors). Ubuntu is supported
only from version 8.04 (Hardy Heron).
Seng Up your Environment
[ 8 ]
Right, this is a good start but unless you are able to read and write binary language like English,
having an OS is not enough. We also need soware dedicated to Android development:
 The JDK (Java Development Kit)
 The Android SDK (Soware Development Kit)
 The Android NDK (Nave Development Kit)
 An IDE (Integrated Development Environment): Eclipse
Android, and more specically Android NDK compilaon system is heavily based on Linux.
So we also need to set up some ulies by default, and we need to install one environment
that supports them: Cygwin (unl NDK R7). This topic is covered in detail later in the chapter.
Finally, a good old command-line Shell to manipulate all these ulies is essenal: we will
use Bash (the default on Cygwin, Ubuntu, and Mac OS X).
Now that we know what tools are necessary to work with Android, let’s start with the
installaon and setup process.
The following secon is dedicated to Windows. If you are a Mac or Linux
user, you can immediately jump to the
Seng up Mac OS X or the

Seng up Linux secon.
Setting up Windows
Before installing the necessary tools, we need to set up Windows to host our Android
development tools properly.
Time for action – preparing Windows for Android development
To work with the Android NDK, we need to set up a Cygwin Linux-like environment
for Windows:
Since NDK R7, Cygwin installaon is not required anymore
(steps 1 to 9). The Android NDK provides addional nave Windows
binaries (for example, ndk-build.cmd).
1. Go to />2. Download setup.exe and execute it.
3. Select Install from Internet.
Chapter 1
[ 9 ]
4. Follow the wizard screens.
5. Select a download site from where Cygwin packages are going to be downloaded.
Consider using a server in your country:
6. When proposed, include Devel/make and Shells/bash packages:
Seng Up your Environment
[ 10 ]
7. Follow the installaon wizard unl the end. This may take some me depending
on your Internet connecon.
8. Aer installaon, launch Cygwin. Your prole les get created on rst launch.
9. Enter the following command to check if Cygwin works:
$ make –version
To run Eclipse and allow compilaon of Android Java code to bytecode, a Java Development
Kit is required. On Windows, the obvious choice is the Oracle Sun JDK:
1. Go to the Oracle website and download the latest Java Development Kit: http://
www.oracle.com/technetwork/java/javase/downloads/index.html
.

2. Launch the downloaded program and follow the installaon wizard. At the end
of the installaon, a browser is opened asking for JDK registraon. This step is
absolutely not compulsory and can be ignored.
3. To make sure the newly installed JDK is used, let’s dene its locaon in environment
variables. Open the Windows Control panel and go to the System panel (or right-
click on Computer item in the Windows Start menu and select Properes). Then go
to Advanced system sengs. The System Properes window appears. Finally, select
Advanced tab and click on the Environment Variables buon.
4. In the Environment Variables window, inside the System variables list, insert the
JAVA_HOME variable with JDK installaon directory as value and validate. Then
edit PATH (or Path) and insert the %JAVA_HOME%\bin directory before any other
directory and separate it with a semicolon. Validate and close the window.
5. Open a command-line window and key in java –version to check the installaon.
The result should be similar to the following screenshot. Check carefully to make
sure that the version number corresponds to the version of the newly installed JDK:
$ java –version
Chapter 1
[ 11 ]
To compile projects from the command line, the Android SDK supports Ant—a Java-based
build automaon ulity. Let’s install it:
1. Go to and download Ant binaries,
packed within a ZIP archive.
2. Unzip Ant in the directory of your choice (for example, C:\Ant).
3. Go back to the Environment Variables window, as in step 12, and create the
ANT_HOME variable with the Ant directory as the value. Append the %ANT_HOME%\
bin
directory to PATH:
4. From a classic Windows terminal, check the Ant version to make sure it is
properly working:
Seng Up your Environment

[ 12 ]
What just happened?
We have prepared Windows with the necessary underlying ulies to host Android
development tools: Cygwin and Java Development Kit.
Cygwin is an open source soware collecon that allows the Windows plaorm to emulate
a Unix-like environment. It aims at navely integrang soware based on POSIX standard
(such as Unix, Linux, and so on) into Windows. It can be considered as an intermediate layer
between applicaons originated from Unix/Linux (but navely recompiled on Windows) and
the Windows OS itself.
We have also deployed a Java Development Kit in version 1.6 and checked if it is properly
working from the command line. Because Android SDK uses generics, the JDK in version 1.5
is the least required when developing with Android. JDK is simple to install on Windows but
it is important to make sure a previous installaon, such as JRE (Java Runme Environment,
which aims at execung applicaons but not developing them) is not interfering. This is why
we have dened
JAVA_HOME and PATH environment variables to ensure proper JDK is used.
Finally, we have installed Ant ulity that we are going to use in the next chapter to build
projects manually. Ant is not required for Android development but is a very good soluon
to set up a connuous integraon chain.
Where is Java’s home?
Dening the JAVA_HOME environment variable is not required. However,
JAVA_HOME is a popular convenon among Java applicaons, Ant being one
of them. It rst looks for the java command in JAVA_HOME (if dened)
before looking in PATH. If you install an up-to-date JDK in another locaon
later on, do not forget to update JAVA_HOME.
Installing Android development kits on Windows
Once JDK is installed on our system, we can start installing Android SDK and NDK to create,
compile, and debug Android programs.
Chapter 1
[ 13 ]

Time for action – installing Android SDK and NDK on Windows
1. Open your Web browser and go to
This web page lists all available SDKs, one for each plaorm.
2. Download Android SDK for Windows, packaged as an Exe installer.
3. Then, go to and download the
Android NDK (not SDK!) for Windows, packaged as a ZIP archive this me.
4. Execute Android SDK installer. Select an appropriate installaon locaon (for example,
C:\Android\android-sdk), knowing that Android SDK and NDK together can take
more than 3 GB of disk space (currently!) with all ocial API versions installed. As a
precauon, avoid leaving any space in the target installaon path.
5. Follow the installaon wizard unl the end. Check the Start SDK Manager:
6. The Android SDK and AVD Manager is launched. The Package installaon window
appears automacally.
Seng Up your Environment
[ 14 ]
7. Check the Accept All opon and click on Install to start the installaon of
Android components:

8. Aer a few minutes, all packages get downloaded and a message asking to restart
ADB service (the Android Debug Bridge) appears. Validate by clicking on Yes.
9. Close the applicaon.
10. Now, unzip Android NDK archive into its nal locaon (for example, C:\Android\
android-ndk
). Again, avoid leaving any space in the installaon path (or some
problems could be encountered with Make).
To easily access Android ulies from the command line, let’s dene the
environment variables:
11. Open the Environment Variables system window, as we did in the previous part.
Inside the System variables list, insert the ANDROID_SDK and ANDROID_NDK
variables with the corresponding directories as values.

12. Append %ANDROID_SDK%\tools, %ANDROID_SDK%\platform-tools and
%ANDROID_NDK%, all separated by a semicolon, to your PATH.

×