Learning Android Chapter 1 ndk translation 1

Source: Internet
Author: User

Chapter 2 Local Development Tool Set (NDK)

NDK is an attachment that helps you integrate local code. The so-called local code is to use some platform features in Android applications, these platform features are usually exposed through C or C ++ APIs. NDK allows your Android app to call some local code or even contain some local libraries. In Android's Gingerbread version, NDK provides NativeActivity classes for local code support. Now you can use C or C ++ to write the entire activity. However, NativeActivity is not the topic of this chapter. Here, let's take a look at how to integrate local C code into your Java Android app.

What is the same as what NDK does?

The main motivation for using local code in application development is performance. As you can see, NDK supports both mathematical and image libraries and some system libraries. Therefore, NDK is the best candidate for applications with strong graphics and computing requirements. This is a good proof of the recent boom in popular mobile games. Note that any local code that accesses your application through the Java Local interface (JNI) is still running on the Dalvik virtual machine of the application. Therefore, it complies with the same Security Sandbox rules as Android applications. Using C or C ++ to write some code that may not be suitable for Java is not a good reason to use NDK. Remember that many low-level hardware features have been exposed to Java through the Android framework. You can use them at will.

Problem solved by NDK

NDK solves some important issues that you have to deal with during local development.

Tool chain

Java provides an entry to access local code through JNI. To make it work, you may have to compile everything for the target architecture on your host. In this case, you need to have a complete tool chain on your development machine. Installing the specified cross-platform compiler and other tools is not simple. NDK provides a complete tool chain so that you can compile and build local code to run on your target platform. This build system makes it easy to install your environment and integrate your local code in the project.

Package Your Library

If you have a local library and you want it to be available in your application, make sure that the path of your library is the path of the System Load Library. In Linux, the typical path is LD_LIBRARY_PATH. On Android devices, only/system/lib is the path for system loading. Here is a problem because the entire/system zone is read-only and cannot be used to install libraries.

NDK solves this problem by conveying your local library to make it part of the application package (apk. Basically, when you install an APK containing a local library, the system creates a path named/data/your package/lib. If you review the "File System Description" On page 95, this path (partition) is private to this application. This is a safe place to store your library, it can prevent other applications from loading and using your library. This balancer system has brought exciting changes to Android devices for publishing applications. It is also a major event because it provides a huge range of reuse and new local code for games.

Documents and standard header files

NDK provides help documentation and sample programs to explain how to make local code work. At the same time, some C and C ++ header files are standardized, such:

• Libc (C library) header file

• Libm header file

• JNI interface header file

• Libz (Zlib compression) header file

• Liblog (Android log) header file

• OpenGL ES 1.1 and OpenGL ES2.0 (3D graphics library) header files

• Libjnigraphics (pixel cache access) header file (Android2.2 and later)

• Supports the C ++ mini header file set

• OpenSL ES local audio library

• Android local application API

With the standard header file set, you may guess which NDK is more appropriate. In the next section, we will test it.

An example of an NDK: onacci)

Because NDK is especially suitable for high-intensity computing applications, I would like to look for such an example. We need to be able to implement a relatively simple algorithm in the local code and Java code, and compare their relative speed. So I chose the Fibonacci Algorithm for this example. This is a simple algorithm that can be easily implemented in C and Java. In addition, we can also use recursive (recursively) and iterative (iteratively) methods.

Let's take a quick look at the following definitions of the Fibonacci series:

Fib (0) = 0

Fib (1) = 1

Fib (n) = fib (n-1) + fib (n-2)

The order of the Fibonacci series is similar to: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144, and so on.

In this example, we will:

• Create a java class to display the Fibonacci Library

• Create a local code header file

• Use C to implement local code

• Compile and build a shared library

• Use this local code in Android activity

 

FibLib

FibLib is the place where we declare to calculate the Fibonacci series algorithm. We have a total of four versions of the Fibonacci algorithm:

• Java recursive version

• Java iteration version

• Local recursive version

• Local iteration version

We first write a Java implementation in Example 15-1, and then use C to write a local version.

Example 15-1. FibLib. java

 

Package com. marakana; public class FibLib {// Java implementation-recursivepublic static long fiber J (long n) {// comment out one if (n <= 0) return 0; if (n = 1) return 1; return fiber J (n-1) + fiber J (n-2);} // Java implementation-iterativepublic static long fiber Ji (long n) {// Note 2 long previous =-1; long result = 1; for (long I = 0; I <= n; I ++) {long sum = result + previous; previous = result; result = sum;} return result;} // Native implementationstatic {System. loadLibrary ("fib"); // Note 3} // Native implementation-recursivepublic static native long fibN (int n ); // Note 4 // Native implementation-iterativepublic static native long fibNI (int n); // comment 5}

Note 1: This is the Java recursive version of the Fibonacci recursive algorithm.

NOTE 2: This is an iterative version of the same Java recursive algorithm. All tasks that implement recursive algorithms can be decomposed into iterative algorithms.

Note 3: The local version must implement a shared library. Here, we will tell the Java Virtual Machine to load this library. When called, this function will be searched.

Note 4: We declare the local Fibonacci method, but it is not implemented. Note that the native keyword should be used here. It tells the Java Virtual Machine to implement this method in this shared library. This library should be loaded before function calling (prior.

Note 5: The previous declaration is a local recursive version, which is an iterative version.

 

Here, we have finished the Fiblib. However, we still need to use C to implement the local method. First, we need to create an appropriate (appropriate) JNI header file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.