Book Image

Mastering Android NDK

Book Image

Mastering Android NDK

Overview of this book

Android NDK is used for multimedia applications that require direct access to system resources. NDK is also the key for portability, which in turn allows a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains. This is a hands-on guide to extending your game development skills with Android NDK. The book takes you through many clear, step-by-step example applications to help you further explore the features of Android NDK and some popular C++ libraries and boost your productivity by debugging the development process. Through the course of this book, you will learn how to write portable multi-threaded native code, use HTTP networking in C++, play audio files, use OpenGL ES 3, and render high-quality text. Each chapter aims to take you one step closer to building your application. By the end of this book, you will be able to create an engaging, complete gaming application.
Table of Contents (17 chapters)
Mastering Android NDK
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Accessing application assets


To access the data packed inside the .apk package on Android in your C++ code, we need to get the path to .apk by using Java code and passing the result into our C++ code using JNI.

In the onCreate() method, pass the value obtained from getApplication().getApplicationInfo().sourceDir into our native code:

  @Override protected void onCreate( Bundle icicle )
  {
    onCreateNative( getApplication().getApplicationInfo().sourceDir );
  }
  public static native void onCreateNative( String APKName );

The implementation of onCreateNative() can be found in 1_ArchiveFileAccess\jni\Wrappers.cpp and looks as follows:

  extern "C"
  {
    JNIEXPORT void JNICALL
    Java_com_packtpub_ndkmastering_AppActivity_onCreateNative( JNIEnv* env, jobject obj, jstring APKName )
    {
      g_APKName = ConvertJString( env, APKName );
      LOGI( "APKName = %s", g_APKName.c_str() );
      OnStart( g_APKName );
    }
  }

We use the ConvertJString() function to convert jstring into std::string...