Basic introduction to "Android Advanced" NDK/JNI programming technology

Source: Internet
Author: User

      andoird java programmer, will be java The limitations of the language because the language as a door to the object cannot be like c/c++ The is so easy to invoke hardware-related operations. therefore jni to build such a bridge, so java and C/C++ Languages can be called each other. As a java engineer to c/c++ language is not very familiar, but only to be familiar with the principles and methods of invocation between them, about c/c++ programming is given to c language engineer go ahead.

In this article, we mainly introduce the Ndk/jin construction and basic use method.

First, The construction of the environment


Second, the basic use

Steps:

(1) New Android Project

(2) Declaring the native method in Java

(3) Create a new JNI folder in the project (use Javah test to generate the header file, write C code)

(4) Write the Android.mk file (a configuration file that tells the NDK how to compile C code)

   Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = test-jnilocal_src_files: = Test-jni.clocal_ Ldlibs+=-lloginclude $ (build_shared_library)

(5) Execute "ndk-build" to generate a dynamic library (you can also generate a dynamic library using run in Eclipse)

(6) Java code "load" Dynamic library, invoke dynamic library


1. Java calls C

Java calls native method (Class param)

The parameters in the method that are called in C (Jnienv*,jobject,jint ... )

2. C calls Java

Call C,c first with Java and then callback

jniexport void Jnicall java_com_ndk2_test_providebean_callhi (jnienv *env,jobject obj) { Char*classname = "Com/ndk2/test/providebean"; Jclass Clazz;clazz = (*env)->findclass (env, classname); if (clazz = = 0) { Logi ("Can not find Class");} else {Logi ("Find the Class");} Jmethodid mid = (*env)->getmethodid (env, Clazz, "Showhi", "() V"), if (mid = = 0) {Logi ("Can not Find Method");} else {LOG I ("Find Method");} (*env)->callvoidmethod (env, obj, mid);} Jniexport void Jnicall java_com_ndk2_test_providebean_calladd (jnienv *env,jobject obj) {char*classname = "com/ndk2/  Test/providebean "; Jclass Clazz;clazz = (*env)->findclass (env, classname); if (clazz = = 0) {Logi (" Can not find Class ");} else {Logi ("Find the Class");} Jmethodid mid = (*env)->getmethodid (env, Clazz, "Showadd", "(II) V"), if (mid = = 0) {Logi ("Can not Find Method");} else { Logi ("Find Method");} (*env)->callvoidmethod (env, obj, Mid, 6, 7);} 
You also configure the environment before you execute the project:

C + + Bulid Build command:bash D:\cygwin64\android-ndk-r8c\ndk-build

C + + general/paths and Symbols/gnu C add D:\cygwin64\android-ndk-r8c\platforms\android-14\arch-arm\usr\include

If still reporting some compilation environment errors, the project transfer to D:\cygwin64\android-ndk-r8c\samples Basic can pass, here I also is not very clear is why, estimate is the problem of path mapping, which big God knows, can share under.


Basic introduction to "Android Advanced" NDK/JNI programming technology

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.