Ndk Development Guide-android ndk Overview

Source: Internet
Author: User
Android ndk Overview

Introduction:

Android SDK is a set of tools that allow Android Application developers to compile and embed application packages in the local source code using C or C ++ source files.

Important:

Android ndk can only be used for Android 1.5 or later versions.

1.Android ndkPurpose:

The android virtual machine allows your application source code to be locally implemented through JNI calls. In short, this means:

 

---- Your application declares one or more methods with the 'native 'keyword to indicate that they are implemented through local code.

Example: Native byte [] LoadFile (string filepath)

 

---- You must provide a shared library (that is. so), package the shared library into your application package APK. These library files must be named lib <something> according to the standard UNIX conventions. so, and must contain a standard JNI interface, such

Libfileloader. So

 

---- Your application must explicitly load these library files (. So files). For example, to load these files at the beginning of the program, you only need to simply add a few source codes:

Static {

System. loadlibrary ("fileloader ");

}

Note: you do not need to write the prefix lib and suffix. So here.

 

Android ndk is only a component for the android SDK. It can help you:

 

---- The JNI-compatible shared library can be run on an arm cpu larger than Android.

 

---- Copy the generated shared libraries to the appropriate path of the program project to ensure that they are automatically added to your APK package (and signed)

 

---- In future versions, we will provide information to help your source code be connected through remote GDB and as much source code as possible.

 

In addition, Android ndk also provides:

 

---- A set of cross-compilation links (compiler, linker, etc.) to generate binary files that can be run in Linux, OS X, and Windows (with cygwin)

 

---- A set of header files with a stable list of local APIs provided by the Android platform

They are described in the docs/STABLE-APIS.html

 

Important:

Remember, in the future update and release platforms, most local system libraries in Android images are not static, but can be completely changed or even deleted.

 

---- A build system allows developers to write a very short build file to describe which source code needs to be compiled and how to compile it. The compilation system can solve all the toolchain/platform/CPU/Abi details. In addition, more toolchains, platforms, and system interfaces are added in later versions of ndk without changing developers' compilation files.

 

2.Android ndkDisadvantages

Ndk is not a method that can write common source code and run on Android devices. Your applications still need to use Java programs, handle System events appropriately to avoid the "application does not respond" dialog box or process the lifecycle of Android applications

 

Note: you can write a complex application in the source code to start/stop a small "application package"

 

We strongly recommend that you understand JNI well, because many developers in this environment require specific actions, not necessarily common typical local code. These measures include:

 

---- You cannot directly access VM objects through pointers. For example, you cannot safely obtain a loop traversal of a 16-bit char array pointing to a string object.

 

---- VM objects between JNI calls must be processed when reference management is required.

 

Ndk only provides system header files supported by limited local APIs and library files on the Android platform. However, a standard Android system image includes many local shared libraries, all of these should be taken into account the implementation details that can be thoroughly changed in the update and release versions.

 

If the Android system library is not explicitly supported by ndk, then the application should not rely on it to provide, or break the wireless system updates on various devices in the future.

 

The selected system library is gradually added to the stable ndk API.

 

3.NdkDevelopment practices

The following gives a rough overview of how to use Android ndk to develop local code.

(1) Place the local code in $ Project/JNI /... For example, put hello. c In the apps/Hello/JNI/directory.

(2) Describe your source code in $ Project/JNI/Android. mk in your ndk compilation system

(3) Optional: In $ Project/JNI/application. MK is used to describe your project in detail in your compilation system, although you do not need it at the beginning, but it allows you to use more CPU or overwrite the compiler/linker tag (see Docs/APPLICATION-MK.html for more details)

(4) compile your code from the directory of your project by running "$ ndk/ndk-build", or starting from the subdirectory

(5) You can copy the last step. In case of success, strip the application-layer sequence of the Shared Library to the project root directory of your application. Then you generate the final APK through the usual method.

Now, let's start with some more details.

① Configure n dk

Previous releases require you to run the build/host-setup.sh script to configure your ndk. This step has been completely removed since Release 4 (ndk R ).

② Place C/C ++ code

Suppose we created the test directory and the code "Hello. c" was created.

Put hello. c In the test/JNI directory.

The location of this project is equivalent to the path of your Android Application project.

 

In this way, you can easily organize the JNI directory you want. The name and structure of the project directory won't affect the final generated APK, so you don't have to use Com. <mycompany>. <myproject> as the application package name

 

Note that ndk supports C and C ++, and the c ++ file extension supported by ndk is '. CPP ', but other extensions can also be processed (see Docs/ANDROID-MK.html learn more)

 

It can adjust your android. mk file to place the source code in different locations.

 

③ Create an android. mk compilation script

 

The android. mk file is a small compilation script. You can use it in the ndk compilation system to describe your source code. More detailed descriptions in docs/ANDROID-MK.html

 

All in all, ndk aggregates your source code into modules, and each module can execute one of the following

---- A static library (lib <project>.)

---- A dynamic library (lib <project>. So)

 

You can define multiple modules in Android. mk, Or you can write multiple Android. mk files, each of which defines a separate module.

 

Note that a separate Android. mk can also be parsed multiple times by the compilation system to determine which variables are not defined.

By default, ndk searches

Test/JNI/Android. mk (storage location)

If you want to define Android. mk to sub-directories, you need to explicitly include them in the top Android. mk. The following is a help method to implement this function.

 

Include $ (call all-subdir-makefiles)

 

It adds all the Android. mk files in the subdirectory to the path of the current compiled file.

 

④ Write an application. mk compilation file (optional)

 

While there is an android. mk file description module in your compilation system, the application. mk file despise your application. See the docs/APPLICATION-MK.html documentation to understand what this file allows us to do. This includes

 

---- Your application requires an accurate list of modules

---- Machine code generated by CPU architecture

---- Optional information, whether you need a release or debug build, special C/C ++ compiler flag, and other build for all modules

This file is an optional file: by default, ndk will provide. simple compilation of all modules in MK (all makefiles are in it) and the default CPU Abi is specified.

 

There are two ways to use application. mk:

---- Put it in test/JNI/application. mk, and it will be automatically found by the 'ndk-build' script.

---- Place it in ndk/<Name>/application. mk, that is, the path where ndk is installed, and then execute "make APP = <Name>" in the ndk directory"

 

This method was used before Android ndk R4. It is still compatible. However, we strongly recommend that you use the first method because it is simpler and does not need to modify the directory of the ndk installation tree.

Take a look at docs/APPLICATION-MK.html for its complete description

 

⑤ Call the ndk compilation system

 

The best way to compile an ndk into a machine code is to use the "ndk-build" script. You can also use the second one, depending on the "$ ndk/apps" subdirectory that is common early in the morning.

 

In either case, the final stripped binary module (that is, the shared library) required by the copy application is successfully built to the project path of the application. (Note, unstripped versions are mainly used for debugging purposes and do not need to copy unstripped binary files to the device)

 

[1]: Use the 'ndk-build' command

The 'ndk-build' script is located at the top of the ndk installation directory and can be directly stored in the application project directory (where your androidmanifest. xml file is located) or any other sub-directories.

 

$ CD $ Project

$ Ndk/ndk-build (Note: $ ndk/ndkbuild, This is a command)

Premise: you first find. bash_profile in the Cyg/etc/skel directory, copy it to the Administrator directory, and then add

 

Java code
  1. Ndk =/cygdrive/e/loveandroid/Android/android-ndk-r6
  2. Export ndk

 

Restart to use it.

For example:

$ CD samples/two-libs

$ Ndk/ndk-build

 

The result is as follows:

 

 

This will start the ndk build script, which will automatically detect the System and Application project files you have developed to determine the build settings

 

For example:

$ Ndk-build

$ Ndk-build clean à clear the generated binary file

$ Ndk-Build-B V = 1à force full re-build, display command

 

By default, it expects the optional files $ Project/JNI/application. mk and required files $ Project/JNI/Android. mk

 

If it succeeds, it copies the generated binary module (that is, the shared library. So file) to the appropriate location in your project tree. You can re-build the complete Android Application Package or run the "ant" command or the ADT plug-in later.

 

See docs/NDK-BUILD.html for more information

 

[2]: use $ ndk/apps/<Name>/application. mk

 

This build method was earlier than Android ndk R4, but it is still compatible with the current version. We strongly recommend that you use 'ndk-build' as much as possible, because we may delete the support in later releases of ndk.

 

① Create a sub-directory $ ndk/apps/<Name>/

 

② Write an application. mk file under the $ ndk/apps/<Name>/directory, and then define an app_project_path to execute the directory of your application project.

 

③ Enter the ndk installation directory, and then enter the following command:

$ CD $ ndk

Note: After Entering CD $ ndk, it will automatically jump to the ndk directory you set.

 

$ Make APP = <Name>

Or

$ Make APP = <Name>-B Indicates re-Compilation

 

The result is the same as that of the first method, except that the intermediate file is placed in $ ndk/out/apps/<Name>/

 

4.New buildYour application package

After the binary file generated by ndk, you need to use the general method to re-build your Android Application Package file (APK), or use the "ant" command or ADT plug-in

 

For more information, see the android sdkdocumentation. The new .apk will be embedded in your shared library. They will automatically extract the software packages installed by the system to your Android device during installation.

 

5.Debugging support

Ndk provides a service script named "ndk-GDB", which can easily launch a local debugging session for an application.

 

Local debugging can only run on Android 2.2 or later without the root permission or privileged access, so you can debug your application at will.

 

For more information, see Docs/ndk-gdb.html. In summary, local debugging
Follow this simple plan:
(1) Make sure that your application debugging (for example, setting up a robot: Debugging "true" in your androidmanifest. XML)

(2) "ndk build" to build your application and install it on your device/Simulator

(3) start the application.

(4) Run "ndk-GDB" from your application project directory.

You will get a GDB prompt. For a useful list, see the gdb user manual commands.

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.