Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Summary


In this chapter, we saw how to make Java communicate with C/C++. Android is now almost bilingual! Java can call C/C++ code with any type of data or object.

We first initialized a native JNI library using the JNI_OnLoad hook. Then, we converted Java Strings inside native code and saw the difference between Modified UTF-8 and UTF-16 character encoding. We also passed Java primitives to native code. Each of these primitives has their C/C++ equivalent type they can be cast to.

We also handled Java object references in native code using Global references and learned the difference between these and Local references. The first must be carefully deleted to ensure proper garbage collection, while the latter has native method scope and must be managed with care as their number is limited by default.

We also discussed how to manage Java arrays in native code so that we could access their content as native arrays. Arrays may or may not be copied by the VM when manipulated in native code. This...