Brief introductionAndroid C + + Advanced programming-using the NDK provides an overview of Java Native Interface (JNI), Bionic APIs, POSIX threading and sockets, C + + support, native graphics and sound APIs, and NEON/SIMD optimizations.
This article records the main points of knowledge.
1. In-depth understanding of the Android Ndkandroid NDK is not a unique tool; It is a comprehensive toolset that contains APIs, cross compilers, linker, debuggers, build tools, documentation, and instance applications.
The components are as follows:
ARM, x86, and MIPS cross compilers
Build the System
Java Native Connect oral file
C Library
Math Library
POSIX threads
The smallest C + + library
Zlib Compression Library
Dynamic Link Library
Android Log Library
Android Pixel Buffer Library
Android native App APIs
OpenGL ES 3D Graphics Library
OpenSL ES Native Audio Library
OpenMAX AL Minimum Support
Android NDK Structure:
Ndk-build The starting point of the building system
NDK-GDB debugging native components with the gun debugger
Ndk-stack analysis of stack traces of native components at the time of collapse
Build all modules of the system
Platforms supports different Android target versions of header files and library files
Samples instances
Sources sharing Module
Toolchains cross-compiler support for different target-machine architectures
2. Using JNI to communicate with native code JNI is the most powerful feature of the Java programming language, which allows some methods of Java classes to be implemented natively, while allowing them to be invoked and used as normal Java methods.
Reference types are opaque to native methods and cannot be used and modified directly, and JNI provides a set of APIs that are closely related to these reference types, which are provided to native functions through jnienv interface pointers.
JNI supports Unicode encoding format and UTF-8 encoded format strings.
Native NiO has improved performance in buffer management areas, large-scale network and file I/O and character set support, and is suitable for transferring large amounts of data between Java programs.
Java has two types of domains: an instance domain and a static domain, and JNI provides a way to access the two types of domains.
Local references cannot be cached and reused in subsequent calls, mainly because their lifetime is limited to the native method, and once the native function returns, the local reference is freed.
Global references are still valid during subsequent calls to native methods, unless they are released by native code display.
Another type of global reference is a weak global reference, like a global reference, where a weak global reference is still valid during subsequent calls to the native method, and unlike a global reference, a weak global reference does not prevent a potential object from being garbage collected.
Local references are valid only during native method execution and in the thread environment in which the native method is being executed, and local references cannot be shared among multiple threads, only global references can be shared by more than one thread.
The JNIEnv interface pointer passed to each native method is also valid in the thread associated with the method call and cannot be cached or used by other threads.
3. Log, mode and fault handling Android log frame is the kernel module of logger.
A log message consists of a section:
Main Application log Information
Event System Events
Radio Radio Related log information
Low-level system-mode information generated by system debugging.
By default, the JNI function basically does not check for errors. However, JNI provides an extended detection method (CHECKJNI), and when this function is activated, the JAVAVM and jnienv interface pointers switch to the function table, which performs an extended error check before invoking the actual implementation, checking for the following issues:
Attempt to allocate an array of negative size
Passing the wrong pointer or null pointer to the JNI function
Syntax error passing class name
Invoke Jni in critical section
Passing error parameters to Newdirectbyebuffer
Invoke JNI when an exception hangs
JNIEnv interface pointers used in the wrong thread
The domain type does not match the Set<type>field function
The method type does not match the Call<type>method function
With the wrong reference type
Wrong release mode
Returning incompatible types from native methods
Invalid utf-8 sequence passed to JNI call
There are two ways to diagnose memory problems:
1. Use Lib mode
2. Using tools, Valgrind and strace memory analysis tools
4. Native Thread
A thread is a mechanism that allows a single process to execute multiple tasks concurrently, a lightweight process that shares the memory and resources of the same parent process, and a process can contain multiple threads that execute concurrently.
Android supports threads in Java and native code.
POSIX thread abbreviation pthreads, is a thread of the POSIX standard. When the thread ends, it can return a result.
POSIX thread synchronization mechanism: mutex, semaphore.
POSIX thread scheduling policy: Sched_fifo, SCHED_RR.
5. Socket Programming
Android is based on the Linux kernel, and the NDK provides header files that are basically POSIX protocols.
A socket is the end point of a connection that can be named and addressed to transfer data between applications on different machines in the same machine or network.
Communication is divided into: connection-oriented communication, non-connected communication, local communication.
6. Graphics API, Audio API
The Android framework provides the Android.graphics.Bitmap class for manipulating and using the bitmap pixel cache in Java code.
Android provides the JNI Graphics API to use native code to access and manipulate the bitmap object's pixel cache.
Android C + + Advanced programming