How to Use Android ndk on Windows

Source: Internet
Author: User
Tags manual writing windows 12

How to Use Android ndk on Windows 12:38:37

Tags: Windows

JNI
Native
Android
The original ndk file can be reprinted. During reprinting, you must mark the article as a hyperlink.
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://yuchen.blog.51cto.com/2739238/623472

In view of the fact that some students want to learn about ndk, it is difficult to find an article on the Internet that is clear and can learn ndk smoothly by following the steps, so I wrote this tutorial.

I. prerequisites for this tutorial:

1. Understand the concept, benefits, and usage of JNI in Java. I will not repeat it here for many introductions on the Internet.

2. Your computer has configured the Android Application development environment, namely, Java, Eclipse, Adt, and Android SDK.

2. Preparation

Cygwin, a compiler that requires C and C ++ code in Android ndk and windows

Android-ndk-r6-windows.zip: http://u.115.com/file/clvyxr0b

Cygwin: http://u.115.com/file/clvy46eb

Iii. Configuration

1. Decompress ndk and cygwin and configure cygwin environment variables.

For example, if I decompress the package to the root directory of drive D, add "D: \ cytwin \ bin;" to the path of the environment variable ;",

As shown in.

2. enter the cygwin directory in the command line and run cygwin. bat. If you are not using the Administrator account to log on to the system, a new folder named after your login name will be generated in the cygwin \ home \ folder.

3. Modify the new file ". bash_profile" in the folder, open it in a text editor such as UE or editplus, and add:

Ndk =/cygdrive/D/android-ndk-r6

Export ndk

Save and close.

D/android-ndk-r6 indicates that the ndk is on the d disk. If your ndk is placed in another drive letter or path, for example, in the E: \ SDK directory, change it to E/SDK/android-ndk-r6.

(If you log on with the Administrator account, directly modify the files in the administrator folder .)

4. Start Using

1. Enter the cygwin directory in the command line window and execute cygwin. bat

2. Go to a project folder in the samples folder in Android-ndk. Here, hello-JNI is used as an example.

The way to enter the folder is similar to the MS-DOS, with CD + space + drive letter + path, see the example

3. Execute $ ndk/ndk-build to compile

4. Import the example to eclipse after compilation, compile and deploy the test.

5. How to Use ndk in your project

The examples in Android-ndk will be compiled. What should I do if I want to call a local method in my existing project?

1. Declare the native method in Java code,

2. Generate the header file

Then the source code of C or C ++ needs to be generated for Java to call.

You can follow the naming rules of JNI or use the JDK javah command to generate a file. We recommend that you use javah to avoid errors caused by manual writing.

For how to use the javah command, see the help of the javah command.

Here we take my test project as an example. After compiling the native method, compile the project and eclipse will generate it in the bin directory. class file, enter the bin directory in the command line, execute javah + package name + class name, a header file will be generated under the bin directory.

This file is the interface for Java to interact with local code. Developers of C or C ++ develop this header file.

3. Compile local methods

Create a JNI folder under the root directory of your project, and copy an android file from the hello-JNI folder in Android-ndk \ samples. MK file, paste it to the JNI folder, and use the File generated by javah. copy the H header file to the JNI folder.

First, create a. c file to implement the methods in the header file,

Edit the Android. mk file and modify the following two lines. The first line is changed to your project name, and the second line is changed to your. C or. cpp file name.

Local_module: = ex25_jni

Local_src_files: = org_yuchen_demo_mainactivity.c

4. Load the so file in Java code

Load the so file using static blocks in Java code. Remove the "lib" and ". So" extensions of the so file.

5. Use the local method

Next, you can call the local method in Java for testing. Follow the methods described above to compile, package, and deploy the test as follows:

This article from the "Rain Chen columns" blog, please be sure to keep this source http://yuchen.blog.51cto.com/2739238/623472

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.