Development of ANDROIDNDK under Windows

Source: Internet
Author: User

This article is I wrote a year ago, was just written Word document, today will be the steps in accordance with the document again, or quite detailed, will not have no relationship, as long as carefully refer to the tutorial, then the Windows ANDROIDNDK development is not a dream, OK, see the tutorial below.

The NDK Development Foundation for Android apps is to call C with Java files in android apps File

1. First you need to install the Cygwin software that simulates the Linux environment under Windows , as follows:

1. download setup.exe

To official website www.cygwin.com in the center of the screen is a black-and-green two-color "C" logo, titled "Install or update now." . Click on the link, which will download a small program setup.exe(250KB). This installer will boot the installation or update process of the CYGWIN environment on your machine.  

2. run the setup.exe for installation, such as:
Figure 1. Installation Wizard for Cygwin environment


Click the Next button.  
Select Download location
On this page, select Install from the Internet.

Figure 2. Select the installation type


Always click on the Next button until the interface appears:


Select a download site. Because these sites are duty-mirrored, choosing a download point is a bit risky (usually they are available, but sometimes not). Usually, try the site closer to you. (with one http://mirrors.163.com the fastest)

Click the Next button.  
Selecting Packages for installation
On this page, you will select the package to install. By default, theCygwin Basic Package will not have GCCinstalled,so you must modify the default settings.
Move the mouse over the plus sign (+) on the devel side and click it to expand the Devel class.


This step, we choose to download the installed components package, in order to enable our installation of Cygwin to compile the program, we need to install the gcc compiler, by default,gcc and will not be installed, we need to select it to install. In order to install gcc, we use the mouse to click on the "Devel" branch in the component list, where there are many components, we must:

Binutils
Gcc
Gcc-mingw
Gdb

Make

Mouse click on the front of the component of the loop button, the build date will appear, we choose the latest version of the installation, is selected after the four categories of components:

Binutils Components


GCC components


GCC-MINGW Components


GDB components


Note There are also make components

After the selection, we choose the next step, enter the installation process, as shown in,


Until the download is complete, OK !

3. test whether the installation is successful

Run Cygwin, enter make-v and gcc-v If you can find, the installation is successful.

Installing the NDK

Installing the ndk is simple, simply extracting the downloaded ndk file into the specified directory.

Configuring the NDk

Modify the . Bash_profile file under the Cygwin directory /home/username , Add the following code to the end of the file:

path to the Ndkroot=/cygdrive/ndk file ( for example , ndkroot=/cygdrive/d/android/ndk)
Export Ndkroot

( Note:thendkroot name can be its own,thendk file path is the path to the NDK after you unzip , paying special attention to replacing the slash with a backslash and Remove D: in: (windows and Linux path difference problem))

2. Open Eclipse, create a new android project hello-jni, and create a new jni at the root of the project folder

A. The key code in Hellojni.java is as follows

TextView  TV = new TextView (this), Tv.settext (Stringfromjni ()), Setcontentview (TV);p ublic native String  Stringfromjni ();//This is the local method of the declaration public native String  Unimplementedstringfromjni (); Static fast loading libhello-jni.so file static {        system.loadlibrary ("Hello-jni");}

B. start, run cmd,cd into the hellojni.java Directory of the HELLO-JNI project ,javac hellojni.java generate byte code

C. Enter the bin directory of the Hello-jni project and use javah to generate . h file

Javah-classpath. -jni the package name . class name

3. Copy the function header file of the generated . h file to the new hello-jni.c file, and note that the hello-jni.c file needs to be introduced #include <jni.h> file,hello-jni.c file is as follows:

#include <string.h> #include <jni.h>/* trivial JNI example where we use a native method * to return a New VM String. See the corresponding Java source * file located at: * *   apps/samples/hello-jni/project/src/com/example/hellojni/ Hellojni.java */jstring//This is a javah-generated head java_com_example_hellojni_hellojni_stringfromjni (jnienv* env,                                                  jobject Thiz) {#if defined (__arm__)  #if defined (__arm_arch_7a__)    #if defined (__arm_neon__)      #define ABI " Armeabi-v7a/neon "    #else      #define ABI" armeabi-v7a "    #endif  #else   #define ABI" Armeabi "  # Endif#elif defined (__i386__)   #define ABI "x86" #elif defined (__mips__)   #define ABI "MIPS" #else   #define ABI "Unknown" #endif    return (*env)->newstringutf (env, "Hello from JNI!  ) Compiled with Abi "Abi".);
4. Create a new Android.mk file

local_path:= $ (call My-dir)

include $ (clear_vars)

local_module:= Hello-jni

local_src_files:= hello-jni.c

include $ (build_shared_library)

Simply explain

Local_path: = $ (call My-dir)

Local_path indicates that the current directory address is returned at the root of the project directory (call My-dir)

Local_module: = Newjni

Local_module is used to differentiate each module in a android.mk. The file name must be one that cannot have spaces

It's you. The file name compiler will automatically prefix lib and suffix. So.

Local_src_files: = Com_demo_jni_jni.c

Include $ (build_shared_library)

Build_shared_library means building a library of dynamic shared libraries


2. Copy the written hello-jni.c files and android.mk files to the jni folder

3. Right-click Project,Properties

Click Builders----->new------->program



The location parameter is the bash.exe file under the bin folder under the Cygwin installation path

The working Directory parameter is the bin folder path under the Cygwin installation path

the parameters of the Arguments are:

--login-c "Path && $NDK/ndk-buildof the project created by cd/cygdrive/"

The path to the project I created here is:F/codethree/hello-jni ( Note that the F - drive character is not: that is, not f:)

The $NDK is the C:\cygwin64\home\Administrator under the Cygwin installation path when installing Cygwin the . bash_profile file configuration of the ndk 's decompression path, as follows :

ndk=/cygdrive/f/android/androidndk/android-ndk-r9d-windows-x86_64/android-ndk-r9d

Export NDK




Note in the Tick specify working set of relevant resources when you specify a resource to select your project's JNI directory .

When the above steps are correctly configured, the C-related code in the JNI directory should be automatically compiled and the corresponding. So library files are automatically created after the configuration is saved to the Libs directory of the project. The obj directory is also created automatically

The compile-time console output resembles the following:


Run the project again and the results are correct.

After the C code changes, save will automatically trigger the compilation, saving time and effort.


How to compile the generated . so file in code, as follows:

Following


Iv. Configuring NDK Path settings

You can modify it via vim in Cygwin, or you can modify home\< your user name >\.bash_profile file in the Windows installation directory to add the environment variable last

Ndk=/cygdrive/e/andriod/develop/android-ndk-r4

Export NDK

Which ndk=/cygdrive/< your drive letter >/<android ndk directory >, "NDK" the name casually up, often used not too long.

Restart Cygwin, enter:

CD $NDK

You can enter the NDK corresponding directory description settings OK.

V. Compiling and demonstrating the project

Now it's time to compile the relevant NDK project (some programs written in C + +) and compile the method as follows:

1. Under the Cygwin command line, go to the Android Project working directory:

CD $NDK

2. Enter the Samples\hello-jni directory in turn, (Full path example: E:\Andriod\develop\android-ndk-r4\samples\hello-jni)

3. Operation

$NDK/ndk-build

Note: the command $NDK and/ndk-build are connected together, there is no space in the middle, the actual use is to break into the full $ndk/ndk-build, do not just play ndk-build.

4. After the normal compilation, the corresponding. So file will be generated in your project Libs/armeabi directory.


At this point, the development environment of the window under the ANDROIDNDK and simple development case is over.






Development of ANDROIDNDK under Windows

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.