NDK development under AS (1) and ndk development under

Source: Internet
Author: User

NDK development under AS (1) and ndk development under

Summarize the NDK for a day.

It seems quite troublesome to develop ndk in Eclipse. I want to download Cygwin from my book and CDT from eclipse .. AS is much more convenient. Download android ndk. Install and configure the environment.

NDK (android native develop kits): android local development tool set, which helps developers quickly develop C or C ++ dynamic libraries and automatically package so and java files into apk, you can compile c/c ++-> into a binary file that can be executed in linux. in java code, you can use jni to call and execute binary files.

JNI: java local development interface. JNI is a protocol used to communicate java code with external local code (c/c ++ ). through this protocol, java code can call external c/c ++, and the external c/c ++ code can also call java code.

JNI development purposes: Native code efficiency, mathematical operations, real-time rendering of the game, audio and video processing (need for speed, opengl, ffmpeg and so on.

1. Configure the NDK Environment

Download NDK. There are many online downloads. After downloading it, decompress it. Then configure in AS, and click File> ProjectStructure:

After the configuration is complete, the: ndk. dir = D \: \ android-ndk-r10d is automatically added in the local. properties file under the project.

2. Create an app Project

1. Create a common android project

2. Declare native methods. native must be added to inform the program that this is a native method. When calling specific java code, just like calling other java methods, you can call them directly. Activity Code:

Import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. toast; public class NDK extends Activity {static {System. loadLibrary ("MyJni"); // import the generated link library file} public native String getStringFromNative (); // local method public native String getString_From_c (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_ndk);} public void onClick (View view) {System. out. println (getString_From_c (); Toast. makeText (this, getStringFromNative (), Toast. LENGTH_LONG ). show ();}}

System. loadLibrary ("MyJni"); When loading the database, note that the name of the loaded database is the name of the compiled database, removing the prefix lib and the suffix so.

Then make the project to compile it into the corresponding class file. Then, based on the generated class file, use javah to generate the corresponding. h header file.

3. Generate the. h header file

1. Click view> ToolsWindows> Terminal in,
Cd app \ src \ main, go to the src \ main \ directory:
2. Execute:
Javah-d jni-classpath F: \ android \ sdk \ platforms \ android-23 \ android. jar ;.. \.. \ build \ intermediates \ classes \ debug example. user. ndkdemo2.NDK
This command is very long and separated slowly. javah is the tool required to generate the header file.-d jni generates the jni directory in the project, then the C/C ++ source file starting with JNI will be created in this directory.
-Classpath F: \ android \ sdk \ platforms \ android-23 \ android. jar: the location of the file where android. jar is located in your SDK file. copy it.
.. \ Build \ intermediates \ classes \ debug path:

Example. user. ndkdemo2.NDK is the path name of NDKclass.

After executing this command, the jni directory and. h header file are automatically generated in the main folder.

You can start the file to see:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class example_user_ndkdemo2_NDK */#ifndef _Included_example_user_ndkdemo2_NDK#define _Included_example_user_ndkdemo2_NDK#ifdef __cplusplusextern "C" {#endif#undef example_user_ndkdemo2_NDK_BIND_ABOVE_CLIENT#define example_user_ndkdemo2_NDK_BIND_ABOVE_CLIENT 8L#undef example_user_ndkdemo2_NDK_BIND_ADJUST_WITH_ACTIVITY#define example_user_ndkdemo2_NDK_BIND_ADJUST_WITH_ACTIVITY 128L#undef example_user_ndkdemo2_NDK_BIND_ALLOW_OOM_MANAGEMENT#define example_user_ndkdemo2_NDK_BIND_ALLOW_OOM_MANAGEMENT 16L#undef example_user_ndkdemo2_NDK_BIND_AUTO_CREATE#define example_user_ndkdemo2_NDK_BIND_AUTO_CREATE 1L#undef example_user_ndkdemo2_NDK_BIND_DEBUG_UNBIND#define example_user_ndkdemo2_NDK_BIND_DEBUG_UNBIND 2L#undef example_user_ndkdemo2_NDK_BIND_IMPORTANT#define example_user_ndkdemo2_NDK_BIND_IMPORTANT 64L#undef example_user_ndkdemo2_NDK_BIND_NOT_FOREGROUND#define example_user_ndkdemo2_NDK_BIND_NOT_FOREGROUND 4L#undef example_user_ndkdemo2_NDK_BIND_WAIVE_PRIORITY#define example_user_ndkdemo2_NDK_BIND_WAIVE_PRIORITY 32L#undef example_user_ndkdemo2_NDK_CONTEXT_IGNORE_SECURITY#define example_user_ndkdemo2_NDK_CONTEXT_IGNORE_SECURITY 2L#undef example_user_ndkdemo2_NDK_CONTEXT_INCLUDE_CODE#define example_user_ndkdemo2_NDK_CONTEXT_INCLUDE_CODE 1L#undef example_user_ndkdemo2_NDK_CONTEXT_RESTRICTED#define example_user_ndkdemo2_NDK_CONTEXT_RESTRICTED 4L#undef example_user_ndkdemo2_NDK_MODE_APPEND#define example_user_ndkdemo2_NDK_MODE_APPEND 32768L#undef example_user_ndkdemo2_NDK_MODE_ENABLE_WRITE_AHEAD_LOGGING#define example_user_ndkdemo2_NDK_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L#undef example_user_ndkdemo2_NDK_MODE_MULTI_PROCESS#define example_user_ndkdemo2_NDK_MODE_MULTI_PROCESS 4L#undef example_user_ndkdemo2_NDK_MODE_PRIVATE#define example_user_ndkdemo2_NDK_MODE_PRIVATE 0L#undef example_user_ndkdemo2_NDK_MODE_WORLD_READABLE#define example_user_ndkdemo2_NDK_MODE_WORLD_READABLE 1L#undef example_user_ndkdemo2_NDK_MODE_WORLD_WRITEABLE#define example_user_ndkdemo2_NDK_MODE_WORLD_WRITEABLE 2L#undef example_user_ndkdemo2_NDK_DEFAULT_KEYS_DIALER#define example_user_ndkdemo2_NDK_DEFAULT_KEYS_DIALER 1L#undef example_user_ndkdemo2_NDK_DEFAULT_KEYS_DISABLE#define example_user_ndkdemo2_NDK_DEFAULT_KEYS_DISABLE 0L#undef example_user_ndkdemo2_NDK_DEFAULT_KEYS_SEARCH_GLOBAL#define example_user_ndkdemo2_NDK_DEFAULT_KEYS_SEARCH_GLOBAL 4L#undef example_user_ndkdemo2_NDK_DEFAULT_KEYS_SEARCH_LOCAL#define example_user_ndkdemo2_NDK_DEFAULT_KEYS_SEARCH_LOCAL 3L#undef example_user_ndkdemo2_NDK_DEFAULT_KEYS_SHORTCUT#define example_user_ndkdemo2_NDK_DEFAULT_KEYS_SHORTCUT 2L#undef example_user_ndkdemo2_NDK_RESULT_CANCELED#define example_user_ndkdemo2_NDK_RESULT_CANCELED 0L#undef example_user_ndkdemo2_NDK_RESULT_FIRST_USER#define example_user_ndkdemo2_NDK_RESULT_FIRST_USER 1L#undef example_user_ndkdemo2_NDK_RESULT_OK#define example_user_ndkdemo2_NDK_RESULT_OK -1L#undef example_user_ndkdemo2_NDK_MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS#define example_user_ndkdemo2_NDK_MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS 65534L#undef example_user_ndkdemo2_NDK_HONEYCOMB#define example_user_ndkdemo2_NDK_HONEYCOMB 11L#undef example_user_ndkdemo2_NDK_MSG_REALLY_STOPPED#define example_user_ndkdemo2_NDK_MSG_REALLY_STOPPED 1L#undef example_user_ndkdemo2_NDK_MSG_RESUME_PENDING#define example_user_ndkdemo2_NDK_MSG_RESUME_PENDING 2L/* * Class:     example_user_ndkdemo2_NDK * Method:    getStringFromNative * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_example_user_ndkdemo2_NDK_getStringFromNative  (JNIEnv *, jobject);/* * Class:     example_user_ndkdemo2_NDK * Method:    getString_From_c * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_example_user_ndkdemo2_NDK_getString_1From_1c  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

In this file: JNIEXPORT jstring JNICALL struct (JNIEnv *, jobject); this is the function definition, Java _ <packege_path >_< class_name >_< method_name> (JNIEnv *, jobject, <parameter_list> );

In the function definition, the two parameters: JNIEnv * And jobject are required. The parameters that need to be passed during function calling are as follows:

Java _ <packege_path >_< class_name >_< method_name> (JNIEnv*,Jint value1, jint value2); // What does jint mean. Jint indicates the int type in Java.

4. Create a C file and implement the native METHOD

Create a c file in the jni Directory: util. c is an empty file. This is because the NDK has a bug in windows. If no error occurs, you can skip this step.

C file:

# Include "example_user_ndkdemo2_NDK.h" // # include <android/log. h> // # define LOG_TAG "System. out "// # define LOGD (...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, _ VA_ARGS _) // # define LOGINFO (...) _ android_log_print (ANDROID_LOG_ERROR, LOG_TAG, _ VA_ARGS _) JNIEXPORT jstring JNICALL partition (JNIEnv * env, jobject) {// LOGINFO ("LOGINFO "); return (* env)-> NewStringUTF (env, "NDK test succeeded");} JNIEXPORT jstring JNICALL success (JNIEnv * env, jobject) {return (* env )). newStringUTF (env, "NDK from C file");} // Created by user on 2016/4/13. //

Finally, configure the ndk in defaultConfig of the build. gradle file.

Android. mk file location:

V. jni. h file

In the jni. h file, the local data type and object reference type are defined. When writing c code, note that the data types and object reference types of these definitions must be used.

Object Data:





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.