The best ndk entry-level project in history

Source: Internet
Author: User

Objectives:

Use ndk to generate the so library, use the so library for JNI calling, create a file in Android sdcard, and write data.

Tools:

Ndk1.5 R1, Android sdk1.5 R1, sdcard, Eclipse, ADT 0.9, eclipse Galileo for C/C ++, cygwin 1.5.

There are many tools. I operate on Windows XP. If I operate on Ubuntu or other Linux systems, go to step 2. See the steps below:

1. Install cygwin

For cygwin installation, Baidu or Google. We recommend that you use the local installation package to download it because of the network speed:Http://www.gougou.com/search? Search = cygwin & id = 2Download a 38.2 M version. I use this version.

2. Install ndk

Go to the topic to install ndk, first open cygwin, and then run the CD command to enter your ndk root directory (do not use the Linux Command ?? Google go) and then run: build/host-setup.sh

If the above information appears, it indicates that the installation is correct. If GCC is found, enter the GCC command to view it. If the toolchain does not find information, try to modify the first line of the build/host-setup.sh FileCode, Will :#! /Bin/sh #! /Bin/bash, which needs to be modified on Ubuntu

After installation, use the make command to compile the built-inProgramEnter make APP = hello-JNI and hello-JNI as the name of the hello-JNI folder under apps. The command will first find the application under apps \ hello-JNI. MK file, find the directory source \ samples \ hello-JNI, and find android. the configuration information in the file MK is compiled. Figure:

Because I have already compiled it, use make APP = hello-JNI-B to re-compile it.

The above information indicates that the compilation is successful. The so file is in the apps/Hello-JNI/project/libs/armeabi folder.

3. Write the Java Native Interface

Next we enter the actual stage and write code.

Create an android1.5 project. ndk does not support versions earlier than 1.5

JNI. Java code:

Jnitest. Java code:

4. Generate the JNI header file

The JNI header file is generated below. Use the javah command to generate the JNI .. Java file!

Run cmd to enter the bin directory of the android project, and then run the javah-classpath.-JNI cc. androidos. JNI. JNI command. Don't forget the package name! (^_^)

Generate a cc_androidos_jni_jni.h

Open and check:

BothC LanguageHeader file Declaration

5. Write C code and Data

In the C language code stage below, you can study it if it is not C.

I am using eclipse for C/C ++ for development. You can use Notepad for simple code.

The project is as follows:

Cc_androidos_jni_jni.h content unchanged

Content of cc_androidos_jni_jni.c:

The code here will be written to the androidos.cc.txt file to sdcardat runtime. Then write the AAAA data.

6. Compile the so file

Compile the so file:

Create a myjni folder under the apps directory of ndk, and create an application. mk file in the myjni folder. The content of the file is:

Create the myjni folder under the ndksources directory, create an android. mk file in the myjni file, and copy the cc_androidos_jni_jni.h and cc_androidos_jni_jni.c files in:

The content in Android. mk is as follows:

For the keyword meanings in the content, view the docs \ overview. txt file.

Compile:

7. Add to the android project for running

Copy the entire apps \ myjni \ libs folder to the android project:

Run the android project and use the ADB shell to view the information:

Go to sdcard to view data.

Note: sdcard has the system permission. If your directory has the root permission, it cannot be written successfully. Unless the real machine has the excessive permission, the simulator will not succeed, the debug information is displayed in eclipse logcat.

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.