Android NDK Beginner's Guide - Second Edition

Book description

Discover the native side of Android and inject the power of C/C++ in your applications

In Detail

Android NDK is all about injecting high-performance and portable code into your mobile apps by exploiting the maximum speed of the device they run on.

This book will show you how to create C/C++-enabled mobile applications and integrate them with Java. The books starts with teaching you how to access native API and port libraries used in some of the most successful Android applications. Next, you will move on to create a real native application project through the complete implementation of a native API and porting existing third-party libraries. Moving forward, you will learn how to access the keyboard and input peripherals and read accelerometer or orientation sensors. Finally, you will dive into more advanced topics such as RenderScript.

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

Table of contents

  1. Android NDK Beginner's Guide Second Edition
    1. Table of Contents
    2. Android NDK Beginner's Guide Second Edition
    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. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
      5. Time for action – heading
        1. What just happened?
        2. Have a go hero – heading
      6. Conventions
      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
        1. Setting up Windows
      2. Time for action – preparing Windows for Android development
        1. What just happened?
        2. Installing Android development kits on Windows
      3. Time for action – installing Android SDK and NDK on Windows
        1. What just happened?
        2. Setting up OS X
      4. Time for action – preparing OS X for Android development
        1. What just happened?
        2. Installing Android development kits on OS X
      5. Time for action – installing Android SDK and NDK on OS X
        1. What just happened?
        2. Setting up Linux
      6. Time for action – preparing Ubuntu for Android development
        1. What just happened?
        2. Installing Android development kits on Linux
      7. Time for action – installing Android SDK and NDK on Ubuntu
        1. What just happened?
        2. Installing the Eclipse IDE
      8. Time for action – installing Eclipse with ADT on your OS
        1. What just happened?
        2. Setting up the Android emulator
      9. Time for action – creating an Android virtual device
        1. What just happened?
        2. Developing with an Android device
      10. Time for action – setting up an Android device
        1. What just happened?
        2. More about ADB
      11. Summary
    9. 2. Starting a Native Android Project
      1. Building NDK sample applications
      2. Time for action – compiling and deploying San Angeles sample
        1. What just happened?
        2. Generating project files with Android manager
        3. Compiling native code with NDK-Build
        4. Building and packaging an application with Ant
        5. Deploying an application package with Ant
        6. Launching an application with ADB Shell
        7. More about Android tooling
      3. Creating your first native Android project
      4. Time for action – creating a native Android project
        1. What just happened?
          1. Introducing Dalvik and ART
      5. Interfacing Java with C/C++
      6. Time for action – calling C code from Java
        1. What just happened?
      7. Debugging native Android applications
      8. Time for action – debugging a native Android application
        1. What just happened?
        2. Defining NDK application-wide settings
        3. NDK-GDB day-to-day
      9. Analyzing native crash dumps
      10. Time for action – analyzing a native crash dump
        1. What just happened?
        2. Deciphering crash dumps
      11. Setting up a Gradle project to compile native code
      12. Time for action – creating a native Android project
        1. What just happened?
      13. Time for action – using your own Makefiles with Gradle
        1. What just happened?
      14. Summary
    10. 3. Interfacing Java and C/C++ with JNI
      1. Initializing a native JNI library
      2. Time for action – defining a simple GUI
        1. What just happened?
      3. Time for action – initializing the native store
        1. What just happened?
      4. Converting Java strings in native code
      5. Time for action – handling strings in the native store
        1. What just happened?
        2. Native character encoding
        3. JNI String API
      6. Passing Java primitives to native code
      7. Time for action – handling primitives in the native store
        1. What just happened?
        2. Have a go hero – passing and returning other primitive types
      8. Referencing Java objects from native code
      9. Time for action – saving references to Objects in native Store
        1. What just happened?
        2. Local references
        3. Global references
        4. Weak references
      10. Managing Java arrays
      11. Time for action – handling Java arrays in native Store
        1. What just happened?
        2. Primitive arrays
        3. Have a go hero – handling other array types
        4. Object arrays
      12. Raising and checking Java exceptions
      13. Time for action – raising & catching exceptions in native Store
        1. What just happened?
        2. Executing code in Exception state
        3. Exception handling API
      14. Summary
    11. 4. Calling Java Back from Native Code
      1. Calling Java back from native code
      2. Time for action – determining JNI method signatures
        1. What just happened?
      3. Time for action – calling back Java from native code
        1. What just happened?
        2. More on the JNI Reflection API
        3. Debugging JNI
        4. Synchronizing Java and native threads
      4. Time for action – allocating an object with JNI
        1. What just happened?
      5. Time for action – running and synchronizing a thread
        1. What just happened?
        2. Synchronizing Java and C/C++ with JNI Monitors
        3. Attaching and detaching native threads
      6. Processing bitmaps natively
      7. Time for action – decoding a camera's feed
        1. What just happened?
      8. Time for action – processing pictures with the Bitmap API
        1. What just happened?
        2. Registering native methods manually
        3. JNI in C versus JNI in C++
      9. 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?
        2. More about the Native App Glue
      3. Handling Activity events
      4. Time for action – stepping the event loop
        1. What just happened?
      5. Time for action – handling Activity events
        1. What just happened?
      6. Accessing window surface natively
      7. Time for action – displaying raw graphics
        1. What just happened?
      8. Measuring time natively
      9. Time for action – animating graphics with a timer
        1. What just happened?
      10. Summary
    13. 6. Rendering Graphics with OpenGL ES
      1. Initializing OpenGL ES
      2. Time for action – initializing OpenGL ES
        1. What just happened?
      3. Time for action – clearing and swapping buffers
        1. What just happened?
      4. An insight into the OpenGL pipeline
      5. Loading textures using the Asset manager
      6. Time for action – reading assets with the Asset manager
        1. What just happened?
        2. More about the Asset Manager API
      7. Time for action – compiling and embedding libpng module
        1. What just happened?
      8. Time for action – loading a PNG image
        1. What just happened?
      9. Time for action – generating an OpenGL texture
        1. What just happened?
        2. More about textures
      10. Drawing 2D sprites
      11. Time for action – initializing OpenGL ES
        1. What just happened?
        2. Vertex Arrays versus Vertex Buffer Object
      12. Rendering particle effects
      13. Time for action – rendering a star field
        1. What just happened?
        2. Programming shaders with GLSL
      14. Adapting graphics to various resolutions
      15. Time for action – adapting resolution with off-screen rendering
        1. What just happened?
      16. 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. Using callbacks to detect sound queue events
        3. Low latency on Android
      7. Recording sounds
        1. Have a go hero – recording and playing a sound
        2. Creating and releasing the recorder
        3. Recording a sound
        4. Recording a callback
      8. Summary
    15. 8. Handling Input Devices and Sensors
      1. Interacting with touch events
      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 events natively
        1. What just happened?
      5. Probing device sensors
      6. Time for action – handling accelerometer events
        1. What just happened?
      7. Time for action – turning an Android device into a Joypad
        1. What just happened?
        2. More on sensors
      8. Summary
    16. 9. Porting Existing Libraries to Android
      1. Activating the Standard Template Library
      2. Time for action – activating GNU STL in DroidBlaster
        1. What just happened?
      3. Time for action – read files with STL stream
        1. What just happened?
      4. Time for action – using STL containers
        1. What just happened?
      5. Porting Box2D to Android
      6. Time for action – compiling Box2D on Android
        1. What just happened?
      7. Time for action – running Box2D physics engine
        1. What just happened?
        2. Diving into the Box2D world
        3. More on collision detection
        4. Collision modes and filtering
        5. Going further with Box2D
      8. Prebuilding Boost on Android
      9. Time for action – prebuilding Boost static library
        1. What just happened?
      10. Time for action – compiling an executable linked to Boost
        1. What just happened?
      11. Mastering module Makefiles
        1. Makefile variables
        2. Enabling C++ 11 support and the Clang compiler
        3. Makefile Instructions
        4. Have a go hero – mastering Makefiles
        5. CPU Architectures (ABI)
        6. Advanced instruction sets (NEON, VFP, SSE, MSA)
      12. Summary
    17. 10. Intensive Computing with RenderScript
      1. What is RenderScript ?
      2. Executing a predefined Intrinsic
      3. Time for action – creating a Java UI
      4. Time for action – running RenderScript Blur intrinsic
        1. What just happened?
      5. Writing a custom Kernel
      6. Time for action – writing a luminance threshold filter
        1. What just happened?
      7. Combining scripts together
      8. Time for action – combining Intrinsics and scripts together
        1. What just happened?
      9. Summary
    18. 11. Afterword
      1. Where we have been
      2. Where you can go
      3. Where to find help
      4. This is just the beginning
    19. Index

Product information

  • Title: Android NDK Beginner's Guide - Second Edition
  • Author(s): Sylvain Ratabouil
  • Release date: April 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783989645