Android Basic---->NDK use

Source: Internet
Author: User

The release of the NDK enabled the development of "Java+c" to become officially supported by the official development approach. NDK will be the beginning of the Android platform to support c development, and today we begin the NDK learning.

NDKA brief description of

What the ndk is:

 The Native Development Kit (NDK) is a set of tools which allow you to leverage C and C + + code in your Android apps. You can use the it either to build from your own source code, or to take advantage of existing prebuilt libraries.

The NDK is not a appropriate for most novice Android programmers, and have little value for many types of Android apps. It is often not worth the additional complexity it inevitably brings to the development process. However, it can be useful in cases in which you need to:

    • Squeeze Extra performance out of a device for computationally intensive applications like games or physics simulations.
    • Reuse your own or other developers ' C or C + + libraries.

What scenarios can be applied to the NDK:

    • Code protection, because the Java Layer Code of the apk is easy to decompile, and the C + + library is difficult to decompile
    • Call third-party C/S libraries in the NDK, because most of the open source libraries are written in C + + code
    • Easy to migrate, libraries written in C/C + + can be reused on other embedded platforms
    • Improve the execution efficiency of the program. Use c development for application logic that requires high performance to improve application execution efficiency.

Using the NDK's Android project

First, download the NDK jar package, Address:Http://192.168.10.75/android-ndk-r10e-windows-x86_64.exe.

Click the direct decompression, get android-ndk-r10e folder, configure the path system variable:D:\android\ndk\android-ndk-r10e;

Test in CMD: The following indicates that the environment variable is configured successfully

Second, we build an Android project, like a normal Android project. The IDE we use here is Eclipse.

We create the jni folder:

Add the native method to the mainactivity , at this point the mainactivity content is as follows:

 Packagecom.example.firstndk;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity; Public classMainactivityextendsappcompatactivity {//define a native method     Public Static nativeString getstringfromndk (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }}

Second, according to mainactivity generated header file:

On the command line, do the following:

C:\USERS\LINUX>CD J:\android\andridtest\eclipseTools\-4-\FIRSTNDK// Local directory of projects C:\Users\Linux>j:j:\android\andridtest\eclipsetools\-4-13 \firstndk>javah-classpath bin/classes;d:\android\ndk\jar\android.jar;d:\android\ndk\jar\android -support-v7-appcompat.jar;d:\android\ndk\jar\android-support-v4.jar-D JNI Com.example.firstndk.MainActivity

the above effect is as follows, in the JNI folder to generate the com_example_firstndk_mainactivity.h file, the contents are as follows:

command-line Format description: Javah-classpath the jar package path required to compile the project-the object class compiled by the-d compilation file (relative path JNI)

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class com_example_firstndk_mainactivity*/#ifndef _included_com_example_firstndk_mainactivity#define_included_com_example_firstndk_mainactivity#ifdef __cplusplusextern "C" {#endif#undefCom_example_firstndk_mainactivity_bind_above_client#defineCom_example_firstndk_mainactivity_bind_above_client 8L#undefCom_example_firstndk_mainactivity_bind_adjust_with_activity#defineCom_example_firstndk_mainactivity_bind_adjust_with_activity 128L#undefCom_example_firstndk_mainactivity_bind_allow_oom_management#defineCom_example_firstndk_mainactivity_bind_allow_oom_management 16L#undefCom_example_firstndk_mainactivity_bind_auto_create#defineCom_example_firstndk_mainactivity_bind_auto_create 1L#undefCom_example_firstndk_mainactivity_bind_debug_unbind#defineCom_example_firstndk_mainactivity_bind_debug_unbind 2L#undefCom_example_firstndk_mainactivity_bind_important#defineCom_example_firstndk_mainactivity_bind_important 64L#undefCom_example_firstndk_mainactivity_bind_not_foreground#defineCom_example_firstndk_mainactivity_bind_not_foreground 4L#undefCom_example_firstndk_mainactivity_bind_waive_priority#defineCom_example_firstndk_mainactivity_bind_waive_priority 32L#undefCom_example_firstndk_mainactivity_context_ignore_security#defineCom_example_firstndk_mainactivity_context_ignore_security 2L#undefCom_example_firstndk_mainactivity_context_include_code#defineCom_example_firstndk_mainactivity_context_include_code 1L#undefcom_example_firstndk_mainactivity_context_restricted#defineCom_example_firstndk_mainactivity_context_restricted 4L#undefCom_example_firstndk_mainactivity_mode_append#defineCom_example_firstndk_mainactivity_mode_append 32768L#undefCom_example_firstndk_mainactivity_mode_enable_write_ahead_logging#defineCom_example_firstndk_mainactivity_mode_enable_write_ahead_logging 8L#undefCom_example_firstndk_mainactivity_mode_multi_process#defineCom_example_firstndk_mainactivity_mode_multi_process 4L#undefCom_example_firstndk_mainactivity_mode_private#defineCom_example_firstndk_mainactivity_mode_private 0L#undefCom_example_firstndk_mainactivity_mode_world_readable#defineCom_example_firstndk_mainactivity_mode_world_readable 1L#undefCom_example_firstndk_mainactivity_mode_world_writeable#defineCom_example_firstndk_mainactivity_mode_world_writeable 2L#undefCom_example_firstndk_mainactivity_default_keys_dialer#defineCom_example_firstndk_mainactivity_default_keys_dialer 1L#undefCom_example_firstndk_mainactivity_default_keys_disable#defineCom_example_firstndk_mainactivity_default_keys_disable 0L#undefCom_example_firstndk_mainactivity_default_keys_search_global#defineCom_example_firstndk_mainactivity_default_keys_search_global 4L#undefCom_example_firstndk_mainactivity_default_keys_search_local#defineCom_example_firstndk_mainactivity_default_keys_search_local 3L#undefCom_example_firstndk_mainactivity_default_keys_shortcut#defineCom_example_firstndk_mainactivity_default_keys_shortcut 2L#undefCom_example_firstndk_mainactivity_result_canceled#defineCom_example_firstndk_mainactivity_result_canceled 0L#undefCom_example_firstndk_mainactivity_result_first_user#defineCom_example_firstndk_mainactivity_result_first_user 1L#undefCom_example_firstndk_mainactivity_result_ok#definecom_example_firstndk_mainactivity_result_ok-1l#undefCom_example_firstndk_mainactivity_honeycomb#defineCom_example_firstndk_mainactivity_honeycomb 11L#undefcom_example_firstndk_mainactivity_msg_really_stopped#definecom_example_firstndk_mainactivity_msg_really_stopped 1L#undefCom_example_firstndk_mainactivity_msg_resume_pending#defineCom_example_firstndk_mainactivity_msg_resume_pending 2L/** class:com_example_firstndk_mainactivity * METHOD:GETSTRINGFROMNDK * Signature: () ljava/lang/string;*/jniexport jstring jnicall java_com_example_firstndk_mainactivity_getstringfromndk (JNIEnv*, Jclass); #ifdef __cplusplus}#endif#endif
Com_example_firstndk_mainactivity.h

The main useful content is to declare a java_com_example_firstndk_mainactivity_getstringfromndk method:

jniexport jstring jnicall java_com_example_firstndk_mainactivity_getstringfromndk   *, Jclass);

Third, under the jni folder, create a C file, our command is:hellondk.c

#include <stdio.h>"com_example_firstndk_mainactivity.h"jniexport Jstring jnicall java_com_example_firstndk_mainactivity_getstringfromndk  * env, Jclass jclass) {     return"Hello, NDK. " " );}

Create a android.mk file in jni (you can copy it from an official document and modify it).

Local_path: = $ (call my-dir) include $ (clear_vars) local_module    := hellondklocal_src_files:= Hellondk.cinclude $ (build_shared_library)

Local_src_files is the source file hellondk.c for the C code we created, and local_module is hellondk. The others do not need to be changed.

Iv. Add a call to JNI in mainactivity , at which point the Mainactivity.java content is as follows:

 Packagecom.example.firstndk;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.TextView; Public classMainactivityextendsappcompatactivity {PrivateTextView TextView; //define a native method     Public Static nativeString getstringfromndk (); Static{system.loadlibrary ("Hellondk"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.textview);    Textview.settext (GETSTRINGFROMNDK ()); }}

Compile the project on the command line:ndk-build, generate the obj folder

J:\android\andridtest\eclipseTools\-4-\firstndk>ndk-buildandroid ndk: Warning:app_platform Android- is one in./androidmanifest.xml[ Armeabi] Compile thumb  <= Hellondk.c[armeabi] sharedlibrary  : Libhellondk.so[armeabi] Install        = libs/armeabi/libhellondk.so

The resulting obj content is roughly as follows:

V. Run the project in the simulator and get the result:

The final project diagram:

Friendship Link

Specific video address: http://pan.baidu.com/s/1dEEO5Mh

Android Basic---->NDK use

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.