You are previewing Android NDK Beginner's Guide.
O'Reilly logo
Android NDK Beginner's Guide

Book Description

Discover the native side of Android and inject the power of C/C++ in your applications with this book and ebook

  • Create high performance applications with C/C++ and integrate with Java

  • Exploit advanced Android features such as graphics, sound, input and sensing

  • Port and reuse your own or third-party libraries from the prolific C/C++ ecosystem

In Detail

Android NDK is all about injecting high performance into your apps. Exploit the maximum power of these mobile devices using high-performance and portable code.

This book will show you how to create C/C++ enabled applications and integrate them with Java. You will learn how to access native API and port libraries used in some of the most successful Android applications.

Using this practical step-by-step tutorial, highlighted with comments and tricks, discover how to run C/C++ code embedded in a Java application or in a standalone application. You will create a real native application starting from project creation through to full implementation of native API and the porting of existing third-party libraries. You will discover OpenGL ES and OpenSL ES, which are becoming the new standard in mobility. You will also understand how to access keyboard and input peripherals and how to read accelerometer or orientation sensors. Finally, you will dive into more advanced topics such as debugging and ARM assembler instructions. By the end of the book, you should know the key elements to enable you to start exploiting the power and portability of native code.

Table of Contents

  1. Android NDK Beginner's Guide
    1. Table of Contents
    2. Android NDK Beginner's Guide
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. The mobile challenge
      2. What this book covers
      3. What you need for this book
      4. Who this book is for
      5. Conventions
      6. Time for action – heading
        1. What just happened?
        2. Pop quiz – heading
        3. Have a go hero – heading
      7. Reader feedback
      8. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Setting Up your Environment
      1. Getting started with Android development
      2. Setting up Windows
      3. Time for action – preparing Windows for Android development
        1. What just happened?
      4. Installing Android development kits on Windows
      5. Time for action – installing Android SDK and NDK on Windows
        1. What just happened?
      6. Setting up Mac OS X
      7. Time for action – preparing Mac OS X for Android development
        1. What just happened?
      8. Installing Android development kits on Mac OS X
      9. Time for action – installing Android SDK and NDK on Mac OS X
        1. What just happened?
      10. Setting up Linux
      11. Time for action – preparing Ubuntu Linux for Android development
        1. What just happened?
      12. Installing Android development kits on Linux
      13. Time for action – installing Android SDK and NDK on Ubuntu
        1. What just happened?
      14. Setting up the Eclipse development environment
      15. Time for action – installing Eclipse
        1. What just happened?
      16. Emulating Android
      17. Time for action – creating an Android virtual device
        1. What just happened?
        2. Have a go hero
      18. Developing with an Android device on Windows and Mac OS X
      19. Time for action – setting up your Android device on Windows and Mac OS X
        1. What just happened?
      20. Developing with an Android device on Linux
      21. Time for action – setting up your Android device on Ubuntu
        1. What just happened?
      22. Troubleshooting a development device
      23. Summary
    9. 2. Creating, Compiling, and Deploying Native Projects
      1. Compiling and deploying NDK sample applications
      2. Time for action – compiling and deploying the hellojni sample
        1. What just happened?
        2. Have a go hero – compiling san angeles OpenGL demo
      3. Exploring Android SDK tools
        1. Android debug bridge
        2. Have a go hero – transferring a file to SD card from command line
        3. Project configuration tool
        4. Have a go hero – towards continuous integration
      4. Creating your first Android project using eclipse
      5. Time for action – initiating a Java project
        1. What just happened?
        2. Introducing Dalvik
      6. Interfacing Java with C/C++
      7. Time for action – calling C code from Java
        1. What just happened?
          1. More on Makefiles
      8. Compiling native code from Eclipse
      9. Time for action – creating a hybrid Java/C/C++ project
        1. What just happened?
      10. Summary
    10. 3. Interfacing Java and C/C++ with JNI
      1. Working with Java primitives
      2. Time for action – building a native key/value store
        1. What just happened?
        2. Have a go hero – passing and returning other primitive types
      3. Referencing Java objects from native code
      4. Time for action – saving a reference to an object in the Store
        1. What just happened?
        2. Local and global JNI references
      5. Throwing exceptions from native code
      6. Time for action – raising exceptions from the Store
        1. What just happened?
        2. JNI in C++
      7. Handling Java arrays
      8. Time for action – saving a reference to an object in the Store
        1. What just happened?
        2. Checking JNI exceptions
        3. Have a go hero – handling other array types
      9. Summary
    11. 4. Calling Java Back from Native Code
      1. Synchronizing Java and native threads
      2. Time for action – running a background thread
        1. What just happened?
        2. Attaching and detaching threads
        3. More on Java and native code lifecycles
      3. Calling Java back from native code
      4. Time for action – invoking Java code from a native thread
        1. What just happened?
        2. More on callbacks
        3. JNI method definitions
      5. Processing bitmaps natively
      6. Time for action – decoding camera feed from native code
        1. What just happened?
      7. Summary
    12. 5. Writing a Fully-native Application
      1. Creating a native activity
      2. Time for action – creating a basic native activity
        1. What just happened?
      3. Handling activity events
      4. Time for action – handling activity events
        1. What just happened?
        2. More on Native App Glue
          1. UI thread
          2. Native thread
          3. Android_app structure
        3. Have a go hero – saving activity state
      5. Accessing window and time natively
      6. Time for action – displaying raw graphics and implementing a timer
        1. What just happened?
        2. More on time primitives
      7. Summary
    13. 6. Rendering Graphics with OpenGL ES
      1. Initializing OpenGL ES
      2. Time for action – initializing OpenGL ES
        1. What just happened?
      3. Reading PNG textures with the asset manager
      4. Time for action – loading a texture in OpenGL ES
        1. What just happened?
      5. Drawing a sprite
      6. Time for action – drawing a Ship sprite
        1. What just happened?
      7. Rendering a tile map with vertex buffer objects
      8. Time for action – drawing a tile-based background
        1. What just happened?
      9. Summary
    14. 7. Playing Sound with OpenSL ES
      1. Initializing OpenSL ES
      2. Time for action – creating OpenSL ES engine and output
        1. What just happened?
        2. More on OpenSL ES philosophy
      3. Playing music files
      4. Time for action – playing background music
        1. What just happened?
      5. Playing sounds
      6. Time for action – creating and playing a sound buffer queue
        1. What just happened?
        2. Event callback
      7. Recording sounds
        1. Have a go hero – recording and playing a sound
      8. Summary
    15. 8. Handling Input Devices and Sensors
      1. Interacting with Android
      2. Time for action – handling touch events
        1. What just happened?
      3. Detecting keyboard, D-Pad, and Trackball events
      4. Time for action – handling keyboard, D-Pad, and trackball, natively
        1. What just happened?
        2. Have a go hero – displaying software keyboard
      5. Probing device sensors
      6. Time for action – turning your device into a joypad
        1. What just happened?
        2. Have a go hero – Handling screen rotation
      7. Summary
    16. 9. Porting Existing Libraries to Android
      1. Developing with the Standard Template Library
      2. Time for action – embedding GNU STL in DroidBlaster
        1. What just happened?
        2. Static versus shared
        3. STL performances
      3. Compiling Boost on Android
      4. Time for action – embedding Boost in DroidBlaster
        1. What just happened?
        2. Have a go hero – threading with Boost
      5. Porting third-party libraries to Android
      6. Time for action – compiling Box2D and Irrlicht with the NDK
        1. What just happened?
        2. GCC optimization levels
      7. Mastering Makefiles
        1. Makefile variables
        2. Makefile Instructions
        3. Have a go hero – mastering Makefiles
      8. Summary
    17. 10. Towards Professional Gaming
      1. Simulating physics with Box2D
      2. Time for action – simulating physics with Box2D
        1. What just happened?
        2. More on collision detection
        3. Collision modes
        4. Collision filtering
        5. More resources about Box2D
      3. Running a 3D engine on Android
      4. Time for action – rendring 3D graphics with Irrlicht
        1. What just happened?
        2. More on Irrlicht scene management
      5. Summary
    18. 11. Debugging and Troubleshooting
      1. Debugging with GDB
      2. Time for action – debugging DroidBlaster
        1. What just happened?
      3. Stack trace analysis
      4. Time for action – analysing a crash dump
        1. What just happened?
        2. More on crash dumps
      5. Performance analysis
      6. Time for action – running GProf
        1. What just happened?
        2. How it works
        3. ARM, thumb, and NEON
      7. Summary
    19. A. Afterword
      1. Where we have been
      2. Where you can go
      3. Where to find help
      4. This is just the beginning
    20. Index