Using the NDK to enable cross-compilation of C and Java in Android

Source: Internet
Author: User

Java in Android based on Java Virtual machine, operating efficiency is relatively low, recently in the efficient large number multiplication, found that JNI or NDK can implement C and Java mixed, and reference C dynamic Library, and the C language in Android does not need to rely on the Java virtual machine, The efficiency is still very high.

First, the construction of the development environment

Java or eclipse will not change, 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 might want to add an environment variable for it to use directly. Other Linux/unix systems are similar and are not discussed here.

Second, the development process

The entire process is generally declared in Java with the C interface function, Javah command export. h header file--write C program---write android.mk, use Ndk-build compile C program for dynamic library file--android compilation

1. Life C interface function in Java

public class Func {static{system.loadlibrary ("Cfunc");//Pre-referenced library (not written before Lib)}private native String Jni_say ();// Declaring an interface function in a C program}

2. Export. h header File

Use terminal to enter the engineering catalogue and enter/bin/classes

Export header file with Javah + package name + class name

You will see an. h file in the current directory


3. Writing C Programs

First, create a folder named "JNI" in the project root directory and copy the. h file from the previous step and write it according to the. H. c/.cpp


The data type in the interface is not the same as that of C + +, and it needs to be learned in another way, the other functions are identical.

Refer to: http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html

4. Writing android.mk

For details, refer to http://www.2cto.com/kf/201310/253386.html

5. Compile C code into a dynamic library using Ndk-build

Use the Ndk-build command directly in the JNI directory

At this point, F5 Refresh, you can find in the Android project directory in the Libs will be one more Armeabi folder, which is the generated dynamic library.

6, write test procedures, Android-side compilation project, run debugging

Note that the JNI project can only be tested with a real machine, and the simulator will not run.

Iii. referencing the C language library

The code is written in the same way as C, mainly to modify the Android.mk to implement the compilation. Refer to the URL provided above for details.





other resources available:

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

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




Using the NDK to enable cross-compilation of C and Java in Android

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.