Real-world NDK entry-level projects required by Android Developers (1)

Source: Internet
Author: User

In this article, we will talk about how to use NDK to generate the SO library, use the SO library for JNI calls, create files 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

About Cygwin installation please Baidu or google, because of speed reasons, it is recommended that you use the local installation package for download: http://www.gougou.com/search? Search = cygwin & id = 2 download a 38.2 M version.

2. Install NDK

Go to the topic, install NDK, first open cygwin, and then use the cd command to enter your NDK root directory. will not use the linux Command ?? Google go) and then run: build/host-setup.sh

 

If the preceding information is displayed, the installation is correct. If GCC is found, enter the gcc command to view the information. If the toolchain is not found, please try to modify the first line of code in the build/host-setup.sh file, will :#! /Bin/sh #! /Bin/bash, which needs to be modified on Ubuntu

After the installation, run the make command to compile the built-in program. Enter make APP = hello-jni, and hello-jni as the name of the hello-jni folder under apps, this 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:

 

Header file declaration in C Language


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.