Android NDK development Basics

Source: Internet
Author: User

1. Introduction to NKD

NDK (Native Development Kit) "Native" is binary

AndroidNDK is a set of tools that allow Android Application developers to embed local machine code compiled from C, C ++ source code files into their respective application software packages and access through JNI. Android supports NDK from 1.5.


Common android development methods are java-encapsulated libraries. The underlying implementation of these libraries is implemented by C/C ++, such as media and graphics libraries.

To achieve this, java calls JNI (Java Native Interface)

The library encapsulated by google is used at ordinary times. Even if the underlying implementation is not using Java, a unified Java interface is provided.

 

The function of NDK is to "write local code by ourselves" (C/C ++) and encapsulate it as a Java interface using JNI.

For example, if we want to make a calculation, this is not the strength of Java. We can use C/C ++ to write the implementation, and return an operation result.

NDKr5 has implemented the function of not writing a line of Java code development programs, but still using virtual machines. The details are encapsulated and hidden.

 


Many Android Developers of JNI and NDK do not understand this problem.

JNI is a mechanism for Java to call Native. It is called Java Native Interface, and Microsoft is similar. the p/invoke on the Net Framework allows C # Or Visual Basic. net can call C/C ++ APIs. Therefore, JNI has nothing to do with Android. It is often used to develop Java applications on a PC, for example, read and write the Windows registry.

NDK is an Application Development Kit launched by Google to help Android Developers compile applications in C/C ++ local languages, contains C/C ++ header files, library files, instructions and sample code. We can understand that, like Windows Platform SDK, it is written in pure C/C ++, however, Android does not support applications written in pure C/C ++. At the same time, the library and function functions provided by NDK are very limited. It only handles some algorithm efficiency-sensitive issues, therefore, Android123 recommends that beginners learn Java and then JNI.

 

 


Ndkhas more than one tool for packaging .so.apk. This is very important.

JNI development is not packaged, but the. so file is placed in a specific location of the file system.

If you are developing an application, you need an NDK tool. Otherwise, how can you use your developed application? Does it help him recompile the file system?

For other implementations, java calls local C/C ++ functions and their writing methods. I think there is no difference between JNI and NDK.

I personally understand that NDK can only use the limited header file provided by NDK to develop C/C ++, while JNI can use the header file in the file system, for example, utils/Log. h

While the latter has more resources. Of course, NDK is working hard to support more interfaces.


 

2. Build the development environment:
The following objects must be installed for NDK development:

During installation, select make, gcc, g ++, bash,


After installation, start cygwin and run:

It will be OK if you see the following 11 results (Note: GNU Make requires version 3.81 or later)

 

 


3. Use of NDK
Download the Ndk and decompress it to a directory at Will (the path stored after the ndk is decompressed must not contain spaces or Chinese characters, such as "D: \ Program Files \ Android-ndk-windows "cannot be identified in cygwin .)

Such as: D: \ AndroidDevelop \ NDK \ android-ndk-r8b

In the D: \ AndroidDevelop \ cygwin \ home directory, find the. bash_profile file and add:

NDK =/cygdrive/d/AndroidDevelop/NDK/android-ndk-r8b

Export NDK

 


After configuration, re-open cygwin

Input:

Cd $ NDK

 


 

If the following prompt is displayed, the path is configured:

 


Input: $ NKD/ndk-build to verify whether NDK can be used?

 


If the following information is displayed, the configuration is successful!

 

 

 

 

 

 

If an error occurs:

 

 

Use the nano or vim editor to edit your script in Cygwin. Use dos2unix to convert existing scripts. For Windows, the line feed is <LF> <CR>, for Linux, <LF>, and for Mac, <CR>.

Run dos2unix. bash_profile on your script first.


4. Simple NDK Demo

 


1. Now we use the installed NDK to compile a simple program. Create a myjni directory under the NDK installation directory to store our Android project.


Go to Eclipse to create a new project JNITest

(1) Write Java code: com. jun. jnitest MainActivity. java

Package com. jun. jnitest;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;

Public class MainActivity extends Activity {
Private TextView tvShow = null;

// Declare the native function speak (). The function is implemented in C/C ++ code and called through the dynamic library.
Public native String speak ();


// Static code block, which is executed before the constructor. The C/C ++ code library is loaded here: *. so.
Static {
// Note that the final dynamic library file suffix is. so. Do not add a suffix when loading, but add a suffix when loading the dynamic library in the Linux development environment. Remember this.
System. loadLibrary ("jni ");

}

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Init ();

String text = speak ();

TvShow. setText (text );
}

Private void init (){
// TODO Auto-generated method stub
TvShow = (TextView) findViewById (R. id. TV _data );

}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}


(2) generate the. h header file and go to the bin/classes directory of the project under the windows command line to execute the command:

Javah-classpath.-d jni com. jun. jnitest. MainActivity

The required header file is generated under the jni directory in the current directory.

 

 

(3) create the Application. mk file in the project directory.

 

 


APP_PROJECT_PATH: = $ (call my-dir)

APP_MODULES: = jni

 

 

 


(4) copy the generated header file to the jni directory under the project folder and create the content of the Android. mk file:

 

 

 

LOCAL_PATH: = $ (call my-dir)

Include $ (CLEAR_VARS)

LOCAL_MODULE: = jni

LOCAL_SRC_FILES: = com_jun_jnitest_MainActivity

Include $ (BUILD_SHARED_LIBRARY)

 

 

Create com_jun_jnitest_MainActivity.c in the jni directory.

# Include <stdlib. h>

# Include <stdio. h>

# Include "com_jun_jnitest_MainActivity.h" // include the header file just generated

 

Jstring JNICALL Java_com_jun_jnitest_MainActivity_speak
(JNIEnv *, jobject ){
Return (* env)-> NewStringUTF (env, "Hello word! This is my first NDK program! ");

}

There are three folders under the jni directory of our project.

 


Finally, the end is coming. Now we use cygwin to compile our Application. After specifying the name of the Application (directory), The NKD compilation system will first find the Application in the directory. mk file, according to Application. mk file information, determine the APP_MODULES module required for Android sharing, and then search all Android devices in the jni directory. mk file. Find the Android that matches the APP_MODULES module. after the mk file. the information provided by the mk file is compiled into the specified C/C ++ source code file to form a shared library file. Finally, the generated shared library file is copied to the specified directory of the Android project. After cygwin is started, enter the command cd $ NDK to enter the ndk installation directory, enter the myjni project directory, and enter the command $ NDK/ndk-build to see the following information, install indicates that you have completed all the work!

 

 

 

 

 

 

Refresh Eclipse to run our program! View

 

 

 

5. FAQs about NDK
Q: What is NDK?

1. NDK is a collection of tools.

NDK provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and java applications into apk together. These tools are of great help to developers.
NDK integrates with the cross compiler and provides mk files to isolate differences such as CPU, platform, and ABI, developers can create so simply by modifying the mk file (indicating "which files need to be compiled" and "Compilation feature requirements.
NDK can automatically package so and Java applications, greatly reducing developers' packaging work.
2. NDK provides a stable and functional API header file statement.

Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the NDK version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), Log Library (liblog ).

 


 

Q: What does NDK bring?
1. With the release of NDK, the development method of "Java + C" has finally become an official supported development method.

With NDK, we can use C to develop high-performance application logic, thus improving the application execution efficiency.
With NDK, we can use C to develop the application logic that requires confidentiality. After all, Java packages can be decompiled.
NDK promotes the emergence of professional so component vendors. (Optimistic conjecture depends on the number of Android users)
2. NDK will be the start of C development on the Android platform.

NDK provides a collection of development tools, allowing developers to easily develop and release C components.

 


 

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.