Use NDK to implement cross-compiling between C and Java in Android.

Source: Internet
Author: User

Use NDK to implement cross-compiling between C and Java in Android.

Java in android relies on Java virtual machines, and its running efficiency is relatively low. Recently, in the efficient multiplication of large numbers, we found that JNI or NDK can implement mixed compilation of C and java, in addition, the dynamic library of c is referenced, and the c language in android does not need to rely on the Java virtual machine, and the efficiency is very high.

I. Establishment of Development Environment

Java development is still eclipse unchanged, in addition to download android-ndk-r9d-linux-x86_64.tar.bz2 (linux), installation method:

$ cd android-ndk-r9d/$ build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.8 --platform=android-19 --system=linux-x86_64 --install-dir=$HOME/android-19-arm
You may need to add environment variables for them for direct use. Other linux/unix systems are similar. I will not repeat them here.

Ii. Development Process

The whole process is to declare the interface functions with C in java-> export the javah command. h header file> c program> Android. mk-> Use ndk-build to compile C Programs for dynamic library files-> android Compilation

1. Life C interface functions in java

Public class func {static {System. loadLibrary ("cfunc"); // pre-Reference Library (do not write lib before write. so)} private native String jni_say (); // declare the interface functions in the C program}

2. Export the. h header file

Use the terminal to enter the project directory and enter/bin/classes

Use javah + package name + class name to export the header file

A. H file is added to the current directory.


3. Write c Programs

First, create a folder named "jni" under the project root directory, copy the. h file generated in the previous step, and compile. c/. cpp according to. h.


Other c/c ++ functions can be referenced in the interface. Note that the data types of interfaces interacting with java are different from those of c/c ++, the others are exactly the same.

See: http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html

4. Compile Android. mk

Android. mk is similar to a Makefile file and stores the compilation information.

See http://www.2cto.com/kf/201310/253386.html for details

5. Use ndk-build to compile c code into a dynamic library

Run the ndk-build command in the jni directory.

At this time, F5 refresh and you can see that there will be an armeabi folder in libs under the android project directory, which is the generated dynamic library.

6. Compile the test program, compile the project on Android, and run and debug the program.

Note that the jni project can only be tested on a real machine, and the simulator cannot run.

Iii. Reference C language library

The code is written in the same way as the C language, mainly by modifying Android. mk for compilation. For more information, see the URL provided above.





Other references:

Http://zzbupt.iteye.com/blog/1876496nd

Ndk-build usage: http://blog.csdn.net/smfwuxiao/article/details/8523087





How to Use android ndk for cross-Compiling

This is completely feasible. android itself can bypass JNI to run c Programs. For example, many mobile phone virus programs are written in c, which are directly pushed to android and then run directly in java, you can enter the group if you don't understand it: 25093009

How to build a C/C ++ cross-compiling environment for android

The plagiarism below is too disgusting and the format is not sorted out ..
Android is a lite version of linux. You need to download a gcc or g ++ installation package and download it to the memory card.
Then install a terminal program and busybox on the mobile phone and call busybox to enable the telenet and ftp services.
Then compile the ndk code on your linux machine, either ftp or data line copy, and then gcc.

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.