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

Source: Internet
Author: User

as a andoird java Program Ape, it is subject to the limitations of the Java language. Because a language that is a door-to-object cannot easily invoke hardware-related operations as a C + + + . So JNI builds a bridge that allows the Java and C + + languages to invoke each other.

As a Javaproject teacher, the C /C + + language is not very familiar, but only need to familiarize themselves with the principles and methods of the call between them, programming about C / D Language project teacher go ahead.

In this article, we mainly introduce Ndk/jin construction and basic usage.

First, The construction of the environment


Second, the main use

Steps:

(1) New Androidproject

(2) Declaring the native method in Java

(3) Create a new JNI directory in 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) Run "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 number of parameters in the method that will be called in C (Jnienv*,jobject,jint ... )

2. C calls Java

Call C with Java first. C 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 run 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

Suppose you are still reporting some errors in the compilation environment. Transfer project to D:\cygwin64\android-ndk-r8c\samples Basic can be passed, here I am not very clear why, is expected to be a path mapping problem, which the great 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.