Android ndk Overview

Source: Internet
Author: User
Android ndk Overview

Introduction:

Android ndk 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.

Important:
Android ndk can only be used to use the platform's cupcake (1.5) or update the released system image.

Particularly, system images 1.0 and 1.1 do not support ndk, because toolchain and associated Abi were changed in the 1.5 release.

1. Android ndk goals:
---------------------

Android virtual machine allows your application source code to implement local code through JNI calls. In short, this means:

-The application Source Code declares one or more methods with the 'native 'keyword to indicate that these methods are implemented through local code. For example:
Native byte [] LoadFile (string filepath );

-If you have a shared library containing these images, the shared library will be packaged into the .apk file of your application.
This shared library should be named according to standard UNIX conventions, such as lib <something>. So, and should contain standard JNI entry points (more details are provided below ). For example:
Libfileloader. So

-Your application must explicitly load this library. For example, to load an application at the beginning, you only need to add the following content to the application's source code:
Static {
System. loadlibrary ("fileloader ");
}

Note that here, you do not need to use the 'lib' prefix and the '. so' suffix.

Android ndk is a supplement to the android SDK and helps you:

-Generate a JNI-compatible shared library that can run on the Android platform (and later versions) on the ARM processor ).

-When Beibei generates a shared database to the location of the application project, they will be automatically uploaded to your latest (.apk file and the signed. APK file.

-In future ndk revisions, we intend to provide tools to help debug your local code and more source/symbol information through a remote GDB connection.

In addition, Android ndk rules:

-A set of cross-tool chains (compilers, connectors, etc.) that can generate local arm binary files for use in Linux, OS X, and Windows operating systems (using cygwin)

-A system header is applicable to the stable local API list supported by the Android platform. This is equivalent to ensuring that all platforms of higher versions are supported.


Important:
Remember that most of the local system libraries on Android images are not fixed in the later update and release platforms and may be completely changed or even deleted.

-A compilation system allows developers to write very short compilation files to describe which codes need to be compiled and how to compile them. This compilation system handles all the details of hair-like tools/platforms/processors/Abi.
In addition, the updated ndk can be added to support more tools, platforms, and system interfaces without changing the development and compilation files (see the following for more details ).

2. It is not the target of Android ndk:
--------------------------

Ndk is not used to compile common local code running on Android devices. In particular, your application should still be written in Java. To handle Android system events, avoid popping up the "application does not respond" dialog box or handling Android applications.

The life cycle of the program.

However, note that ndk is suitable for compiling a complex application with local code and a small "application encapsulation" for start/stop ".

We strongly recommend that you understand JNI well, because the specific actions required by many businesses in this environment come from developers without the need for specific local code. These include:

-You cannot directly access the content of a VM object through a local pointer. For example, you cannot safely obtain a pointer that corresponds to the iteration of a string object's 16-character array in a loop.

-When managing local code, you must keep the handle between Vm objects and JNI calls.

Ndk only provides system headers. It is applicable to a very limited set of local APIs and libraries supported by the Android platform. Although a typical Android system image contains many local shared libraries, these shared libraries should be considered
Execution details, which may be completely changed between released platforms and updates.

If an Android system library is not directly supported by the ndk header, the application should not be only available but dependent on it. Otherwise, it will not be available after the next air system updates multiple devices.

The selected system library is gradually added to a set of stable ndk APIs.

3. ndk development practices:
---------------------------------

Here is a rough overview of how to use Android ndk to develop local code:

1/run build/host-setup.sh to configure ndk

2/place your code to the directory sources/<mysrc>

3. Compile the sources/<mysrc>/Android. mk file to describe your code to the ndk compilation system.

4. Compile the apps/<MyApp>/application. mk file to describe the applications and Local Code Compiled by the ndk compiling system.

5. Run "make APP = <MyApp>" in the ndk top-level directory to compile your local code.
Note: 5/is executed in cygwin.

If the compilation is successful, The last step is to copy the Shared Library to the root directory of your application project. Then you can generate the final. APK file by following the normal script.

Now, we have some more detailed information:

3.1 configure ndk:
--------------

After you install the ndk as described in the docs/install. txt file, you should run the 'build/host-setup.sh 'script to configure the ndk.

This script is used to detect your host system and confirm some prerequisites.
A configuration file, such as out/host/config-host.mk, is generated and will be used during subsequent ndk compilation.

In some cases, this may instruct you to download a binary compressed file containing the pre-compilation tool for the development platform and decompress it to the ndk root directory.
This prompt should contain enough information for you to do this.

If you forget this step, an error message will be generated when you try to use the ndk compilation. This information will tell you how to do this.

3.2 place C and C ++ code:
-----------------

The ndk Compilation System Schedules your resources to be visible under the top-level directory 'sources. You should first create the following directory:
$ Ndk/sources/<mysrc>/

You can flexibly organize the content under 'sources' according to the directory structure you want. This directory name and structure will not affect the final application package, therefore, you do not need to use authentic and unique names,
For example, Com. <mycompany>. <myproject> is used as the application package name.

For records, ndk comes from the 'Sources/samples' Directory, which contains subdirectories of multiple simple modules.

Note that C and C ++ sources are supported. The default C ++ file extension is the '. cpp' supported by ndk, but other extensions can also be well processed (see Docs/ANDROID-MK.TXT files ).

You can store your source files in different locations as long as you create $ ndk/sources/<mysrc> as a symbolic link. For proper operations, the ndk compilation system must be able to find the source files and compilation scripts from $ ndk/sources.

3.3 compile the Android. mk compilation script:
----------------------

The android. mk file is a small compilation script. You can write it to describe the source file to the ndk compilation system. Their syntax is described in detail in the file docs/ANDROID-MK.TXT.

In short, the ndk organizes the source file into "modules", where each module can be one of the following:

-A static library
-A Shared Library

You can define several modules in a separate Android. mk file, or you can write several Android. mk files. Each file defines a separate module.

All Android. mk files are parsed by the compilation system before other compilation.
Note that a separate Android. mk can be parsed multiple times by the compilation system, so do not think that some variables are not defined in them.

By default, the ndk will look for all files that match the following:
By default, ndk searches for all files matching the following:

$ Ndk/sources/*/Android. mk

If you want to define Android. mk to sub-directories, you should explicitly include them in the top-level Android. mk file. This can be done by a help function, for example, using:

Include $ (call all-subdir-makefiles)

This will contain all the Android. mk files in the subdirectories of the current compiled file path.

3.4 compile the application. mk compilation file:
-----------------------

Although an android. mk file description module is provided to the compilation system, you also need to write an application. mk file to describe the application and the modules required by the application.
The file must be located:

$ Ndk/apps/<MyApp>/application. mk

<MyApp> is the brief description name of an application and will be used to call ndk compilation (and will not enter the final APK file ).
This file is used to compile the following content for ndk:

-Location of the Android Application Project path.

-List of ndk modules required by the application.
This should be a list of real 'shared database' modules.

-Optional information, such as determining whether you want to publish or adjust the compilation, and specifying the C or C ++ compilation flag.

-Plan: A list of specific platforms/processors. You have a clear goal (only one is supported currently ).

The syntax of the application. mk file is very simple and described in the docs/APPLICATION-MK.TXT file.

You can define several application. mk files to build different compilations for the same application, for example:

$ Ndk/apps/release/application. mk
$ Ndk/apps/debug/application. mk

3.5 call the ndk compilation system:
------------------

On the command line, enter the ndk top-level directory, and then use the following command to call the compilation system:

Make APP = <MyApp>

'Make' here refers to GNU make, and <MyApp> is the name of a '$ ndk/apps/' sub-directory.

This will try to create a selection of all relevant modules. The application. mk file lists the final shared libraries. If the compilation is successful,
Copy the Shared Library to the root directory of your application project (Note: The unstripped version may be for debugging purposes, so it is not necessary to copy unstripped binary to the device ).
Note: unstripped does not know how to translate, so it is retained.

4. debugging support:
------------

It is still rough to debug the local code using the initial ndk version.

Note: We plan to make debugging easier in the update of ndk, which does not need to change your source, Android. mk and application. mk files. For your security, please only open the URL with reliable source

From: http://hi.baidu.com/rocklad/blog/item/c35898db22bf60d1b7fd4878.html

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.