Android ndk learning Summary

Source: Internet
Author: User

1. What is ndk?

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
Integrated with the cross-compiler, and provided the corresponding MK file to isolate differences such as CPU, platform, and Abi, developers only need to modify mk
File (indicating "which files need to be compiled" and "Compilation feature requirements"), so can be created. Ndk can automatically add so and Java
Application packaging greatly reduces developers' packaging work. In short, ndk is a set of cross-compilation tools that help you write data in C or C ++.Code, Compiled as. So (similar
Files in. dll format under Win, so that you canProgramJava language (JNI) is used to call the code.

 

Ii. Download and install cygwin


Since make and GCC must be used to compile the ndk code, you must first build a Linux environment,
Cygwin is a UNIX simulation environment running on the Windows platform. It is applicable to applications that learn the Unix/Linux operating environment or from UNIX to Windows.
Porting is very useful. With this feature, you can use ndk to compile C and C ++ code without installing Linux. Next we will install cygwin step by step.

(I forgot it during installation)

First, you must first runHttp://www.cygwin.comDownload setup.exe

1. Double-click "run". After running, you will see the Installation Wizard interface:

2. Click "Next" to select the installation method:

1) install from Internet: Download the package directly from the Internet and install it immediately (after the installation is complete, the downloaded package will not be deleted, but will still be retained so that the package can be installed again next time ).

2) download without installing: Download the installation file to your local computer, but it is not installed yet.

3) install from local directory: without downloading the installation file, install it directly from a local directory containing the installation file.

3. Select the first item and click Next:

4. Select the directory to be installed. Note that it is best not to place it in a directory with Chinese characters and spaces, which may cause installation problems. Other options do not need to be changed. Next, click:

Step 5: select the installation directory of cygwin. this is the directory of the installation package you downloaded. Click Next To Run setup.exe:

6. You have three Connection Methods:

1) Direct Connection: a direct connection.

2) use ie5 settings: Use the connection parameter settings of IE to connect.

3) Use http/FTP Proxy: Use http or FTP Proxy Server for connection (you need to enter the server address and port number ).

You can select based on your network connection. Generally, you can select the first method, that is, direct connection. Then click "Next ",

7. This is the site to download. I useHttp://mirrors.kernel.orgThe speed is quite fast. Click Next

8. The installation package list will be downloaded.

9. Search allows you to enter the name of the package you want to download and quickly filter out the package you want to download. The four single-choice buttons select the style of the bottom tree. By default, they do not need to be moved. The default view is
Category. We recommend that you change it to "full" to show all the packages and check them again. Save some of the packages to hide them. The check box in the lower-left corner shows whether to hide the expired package. By default, you can check it and download the package below.
To avoid downloading all the packages to be installed, the following packages used for ndk development are listed: autoconf2.1, automake1.10, binutils, GCC-
Core, GCC-, G ++, gcc4-core, gcc4-g ++, GDB, PCRE, PCRE-devel, gawk, make a total of 12 packages

10. Choose to install these packages, click skip, and convert it to the digital version format. Make sure that the bin item is converted to the cross sign, and the SRC item is the source code. This is unnecessary.

11. Test whether cygwin has been installed.

Run cygwin. In the displayed command line window, enter the cygcheck-C cygwin command to print the current cygwin version and running status. If the status is OK, cygwin runs normally.

Then input gcc-version, G ++ -- version, make-version, and GDB-version in sequence for testing. If both print the published version information and some description information, I am very happy to tell you, your cygwin installation is complete!

3. Configure ndk Environment Variables

1,

First, find the installation directory of cygwin and find a home \ <your username> \. bash_profile file. My files are: D: \ cygwin
\ Home \ Administrator \. bash_profile (Note: no trojans exist in my home folder during installation.
Method: first open the environment variable, delete the home variable in the user variable, and create a folder named administrator under the E: \ cygwin \ home folder.
(User name), and then copy D: \ cygwin \ etc \ skel \. bash_profile to the folder ).

2. Open the bash_profile file and add ndk =/cygdrive/<your drive letter>/<Android ndk directory> example: ndk =/cygdrive/D/android-ndk-r5b

Export ndk

The ndk name is random. For future convenience, select a short name and save it.

3. Open cygwin and enter CD $ ndk. If the/cygdrive/D/android-ndk-r5b information configured above is output, the environment variable is set successfully.

4. Use ndk to compile the program

1. Now let's compile a simple program with the installed ndk. Let's choose the built-in ndk example hello-JNI, my location is D: \ android-ndk-r5b \ samples \ hello-JNI (depending on your location ),

2. Run cygwin and enter the command CD/cygdrive/D/android-ndk-r5b/samples/Hello-JNI to enter the E: \ android-ndk-r5b \ samples \ hello-JNI directory.

3. Enter $ ndk/ndk-build. After successful execution, it will automatically generate a libs directory and put the compiled. So file in it. ($ Ndk is used to call the environment variable we configured previously, and ndk-build is used to call the Compilation Program of ndk)

4. Go to the libs directory of hello-JNI to check whether the. So file is generated. If yes, your ndk will run normally!

5. Integrate the C/C ++ development environment in eclipse

1. Install the C/C ++ environment plug-in of Eclipse: CDT. Here, install it online.

First, log on to http://www.eclipse.org/cdt/downloads.php and find the on-line installation address of your own cdtplug.

2. Click the Help menu and find the install new software menu.

3. Click the Add button and enter the obtained address. After the plug-in list is displayed, select all. Then, select next to complete the installation.

4. After the installation is complete, right-click a new project in eclispe. If the C/C ++ project appears, your CDT plug-in is successfully installed!

6. Configure the C/C ++ Compiler

1,

Open eclipse, import the hello-JNI example that comes with ndk, right-click the project name, and click Properties. The configuration page is displayed, and then click
Builders: the compilation tool list of the Project is displayed. Click new to add a new compiler. The add page is displayed. Select Program and click OK.

2. The add interface appears. First, name the compilation configuration, for example, c_builder.

Set
Set location to <your cygwin installation path> \ bin \ bash.exe program, for example, D: \ cygwin \ bin \ bash.exe.
Set working directory to <your cygwin installation path> \ bin directory, for example, D: \ cygwin \ bin

Set arguments

-- Login-c "CD/cygdrive/D/android-ndk-r5b/samples/Hello-JNI & $ ndk/ndk-build"

Upper
In the configuration above,/cygdrive/D/android-ndk-r5b/samples/Hello-JNI is the directory of the program you are currently compiling, $ ndk is the previous configuration
Set
The Dig command line program transmits parameters to go
Directory of the program to be compiled, and then run the ndk-build Compilation Program

3. Switch to the refresh tab and hook refresh resources upon completion

4. Switch to the build options tab and check the last three items.

5. Click specify resources, select the Resource Directory, and select your project directory.

6. Click "finish" and click "OK" to save the configuration. Note: If the compiler you configured is under another compiler, remember to set it to the first place by clicking the "up" button, otherwise, the compilation of C code is later than that of Java code, which may cause your C code to be compiled twice before you can see the latest modification.

7,

This process will be very tired, Now congratulations again, the compilation configuration is also configured, now to test whether it can be automatically compiled, open the project JNI directory of the hello-jni.c File
Hello from JNI! Change it to another text, for example, hello, my name is.
Alex., and then run your program in the simulator. If the simulator displays your latest modified text, then congratulations! You have all configured successfully!

Note:

《1Why can't I useNdkDevelopment:

1, No support for system event handling

2, No program lifecycle maintenance is provided

3, Not providedUIAspectAPI

 

 

《2UseNdkBenefits

1To use the application logic with high performance requirements.CDevelopment to improve efficiency

2To use the application logic that requires confidentialityCAfter allJavaAll packages can be decompiled.

 

 

《3"NdkSuitable for standalone scenarios with less memory and moreCPUResource processing, such as signal processing and Physical Simulation

 

 

《4"

Android. mkUsed to specifySource code

Application. mkUsed to specify the targetAndroidApplication,This fileAndroid SDKApplication Engineering and<Ndk>/sourcesAnd specify the directory of the application project that accepts the shared library.

 

 

《5"

The shared library should follow the standardUNIXNaming Conventions:Lib <something>. So

 

 

《6"

GDBDebug local code

 

 

《7"

NdkDevelopment procedure:

1,Run
Build/host-setup.shConfigure yourNdk

2,Place your code to the directorySources/<mysrc>

3,WriteSources/<mysrc>/Android. mkFile to describe your codeNdkCompilation System

4,WriteApps/<MyApp>/application. mkFile description you needNdkCompile System-compiled applications and local code

5,InNdkRun in the top-level directory"Make
APP = <MyApp>"Compile your local code (this step is inCygwin)

Note: If the compilation is successful, the last step is to copySoLibrary to the root directory of your application project, and then you can generate the finalAPKFile

 

 

 

 

《8"

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.