Windows Android NDK programming

Source: Internet
Author: User

Last year, I had an ndk, at that time felt very troublesome, and never again, and now the company's projects need to use the NDK programming, and today, the side, encountered some problems, and share with you

.

I. Installation Cygwin

1. Download Cygwin http://cygwin.com/setup-x86.exe Double click to run the following screen, click Next

Select Install from the Internet, click Next

Select default Location C:\cygwin, click Next

Still the default location, continue to click Next

Default option direct Connection, continue to click Next

can choose NetEase 163 server, continue the next step

The following interface appears

Select the Cygwin component to install: Here we only need the following components: Binutils, gcc, Gcc-mingw, GDB, make, not all components;

For example, after you enter GCC, the components are automatically listed below, click on the word to the right of all until the following is installed, and then click Next to wait for the installation, after installation, click on the previous step back to the interface,

Several other components are installed in the same way.

This is the directory after installation

Double-click on the Cygwin.bat in the above directory to enter make-version in the Startup window, if a similar interface appears, the installation is successful.

Two. Download the NDK

1.- Windows version ndk:android-ndk-r9c-windows-x86.zip (32-bit), Android-ndk-r9c-windows-x86_64.zip (64-bit) This version is used on Windows Cygwin, can not directly run directly on Windows;

2. After download, unzip and put into c:\\cygwin\, the directory structure is as follows

Three. Create an Android project

1. Create an Android project and a new JNI folder in the project root directory,

2. Run the project to generate the class file in the Bin folder in the project, (class file is not visible in the bin in the project, you can open the Bin folder from the Explorer)

3. Generate the corresponding header file

Example can generate a header file in this way Javah-classpath f:/ndktest/bin/classes-d c:/-jni Com.example.ndktest.TestJNI

(F:\ndkTest\bin\classes is the directory where the package resides, c:/represents the location where the generated header file is stored,Com.example.ndktest.TestJNI is the package. Class name )
(Note that the console below is not a Windows command window, but a cygwin.bat in C:\cygwin)

Copy the com_example_ndktest_testjni.h generated in the C drive to the JNI folder in the Android project

Create a new. cpp file in the Jni folder, such as Main.cpp, and then use the include command to include the. h file, and copy the functions in the. h file to Main.cpp, remember to add parameters to the copied function, and change the end to {}, using NE in the example. Wstringutf creates a UTF8-encoded string and returns

Write the Android.mk file in the Jni folder Mk file Be careful, do not error after writing, but the call will throw an exception

get current file contents : $ (call My-dir) is a macro method in the compiler that calls the macro method and then the directory path before returning; Assignment Symbols : ": =" Yes Assignment Symbols, the first sentence is to return the current directory where the current file is located and assign this directory path to Local_path; initializing the compilation module parameters : $ (Clear_vars) function is to initialize the parameters of the compilation module, Local_module Local_src_files is also such a parameter; specifying the compilation module : local_module: = Hello, specifying the compiled so file name, after compiling the system will precede the name with "Lib", suffix plus ". So"; Specifies the compilation source file : local_src_files: = Main.cpp tells the compiled system source file, if there are more than one file, then write in the following sequence; compile to Static library : include $ (build_shared_library), the function is the high-speed system, compile the result compiled into. so suffix static library;
Static Library introduction : The NDK platform has a lot of ". A" end of the dynamic library, when we compile the dynamic library, we can introduce some static libraries;

In order to prevent manual input errors, directly put the code here, you can directly copy and modify the

Local_path: =$ (call My-dir)

Include $ (clear_vars)

Local_module: =testjni

Local_src_files: = main.cpp

Include $ (build_shared_library)

Create a new Application.mk file in the Android root directory and enter the following, noting that the App_modules value is the same as the Local_module value in Android.mk

Also put the code here, to make it easy for everyone to copy

app_project_path:=$ (call My-dir)
App_modules:=testjni

Four. Compiling

Start C:\\cygwin in Cygwin.bat, enter the CD your project is in absolute position my project in the F disk, the project is called Ndktest, so input CD f:/ndktest, pay attention to the downline direction

Input/android-ndk-r9c/ndk-build, my android-ndk-r9c is in C:\cygwin, so I can lose this directly see two. Download 2. Directory structure

After entering, enter, the following prompt appears, indicating that the build so library successfully

Refresh the Android project, see the generated Lib start in Libs Armeabi, the. So end of the library (right click on the Android project name, select Refresh to update the project)

Before invoking the declared native function, call System.loadlibrary (), call once, the parameter is the name of the class library, note that does not include the beginning of Lib and the end of. So

This is the end of the Android NDK program.

Five. Precautions

Some Android projects also have the armeabi-v7a folder, remember to copy the resulting so library to the folder in one copy.

MK file input errors will not error, until the call will not throw an exception, be sure to be careful

Remarks: Above in Windows7 flagship 32-bit SP1, compiled on i3 processor, tested on Android arm simulator, Xiaomi Note on test pass

If there is reprint please indicate the source thank you

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows Android NDK programming

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.