Android's NDK Learning (1)

Source: Internet
Author: User

Android NDK Learning (1) before learning for a while, the NDK always feels like summarizing. The NDK makes it easy to communicate with Java and C + + code, and a reasonable grasp of the NDK can improve the efficiency of application execution, so the NDK is a must-have tool for those who learn anndroid development. Just beginning to learn is a little excited, a little afraid, excited because I learned the C + + language before, can learn things together, feel can make better things, fear is to listen to the big God around said Ndk in Android development is a very difficult content. But anyway, I still looked for a book, watched the video, found some electronic materials, and began to learn the path of the NDK!
One, the first program Hello World relatively speaking, using the NDK to implement a large number of native methods and to synchronize them with the Java class can easily become a tedious task. You first need to create a new Android project, and then declare a native method in the main function, the code is as follows
public class Mainactivity extends  Activity {public static native String test ();    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                }

Then use the command line, start-cmd, switch to the project's SRC directory and execute the command as follows:
Javah-d. /jni the package name. Mainactivity
When you refresh the project, you will find a more Jni folder with a. h file open, which is a C header file.
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_example_exercise_mainac tivity */#ifndef _included_com_example_exercise_mainactivity#define _included_com_example_exercise_mainactivity# ifdef __cplusplusextern "C" {#endif #undef com_example_exercise_mainactivity_mode_private#define com_example_ Exercise_mainactivity_mode_private 0l#undef Com_example_exercise_mainactivity_mode_world_readable#define Com_ Example_exercise_mainactivity_mode_world_readable 1l#undef Com_example_exercise_mainactivity_mode_world_ Writeable#define com_example_exercise_mainactivity_mode_world_writeable 2l#undef Com_example_exercise_ Mainactivity_mode_append#define com_example_exercise_mainactivity_mode_append 32768L#undef Com_example_exercise_ Mainactivity_mode_multi_process#define com_example_exercise_mainactivity_mode_multi_process 4L#undef Com_example_ Exercise_mainactivity_mode_enable_write_ahead_logging#define Com_example_exercise_mainactivity_mode_enable_ Write_ahead_logging 8l#undef Com_example_exercise_mainactivity_bind_auto_create#define Com_example_exercise_mainactivity_ Bind_auto_create 1l#undef Com_example_exercise_mainactivity_bind_debug_unbind#define Com_example_exercise_ Mainactivity_bind_debug_unbind 2l#undef Com_example_exercise_mainactivity_bind_not_foreground#define Com_example_ Exercise_mainactivity_bind_not_foreground 4l#undef com_example_exercise_mainactivity_bind_above_client#define com _example_exercise_mainactivity_bind_above_client 8l#undef Com_example_exercise_mainactivity_bind_allow_oom_ Management#define com_example_exercise_mainactivity_bind_allow_oom_management 16L#undef Com_example_exercise_ Mainactivity_bind_waive_priority#define com_example_exercise_mainactivity_bind_waive_priority 32L#undef Com_ Example_exercise_mainactivity_bind_important#define com_example_exercise_mainactivity_bind_important 64L#undef Com_example_exercise_mainactivity_bind_adjust_with_activity#define Com_example_exercise_mainactivity_bind_ Adjust_with_activity 128l#undef Com_example_exercise_mainactivity_context_include_code#define Com_example_exercise_mainactivity_ Context_include_code 1l#undef Com_example_exercise_mainactivity_context_ignore_security#define Com_example_ Exercise_mainactivity_context_ignore_security 2l#undef com_example_exercise_mainactivity_context_restricted# Define com_example_exercise_mainactivity_context_restricted 4l#undef Com_example_exercise_mainactivity_result_ Canceled#define com_example_exercise_mainactivity_result_canceled 0l#undef Com_example_exercise_mainactivity_ Result_ok#define Com_example_exercise_mainactivity_result_ok-1l#undef Com_example_exercise_mainactivity_result_ First_user#define Com_example_exercise_mainactivity_result_first_user 1l#undef Com_example_exercise_MainActivity_ Default_keys_disable#define com_example_exercise_mainactivity_default_keys_disable 0L#undef Com_example_exercise_ Mainactivity_default_keys_dialer#define Com_example_exercise_mainactivity_default_keys_dialer 1L#undef Com_ Example_exErcise_mainactivity_default_keys_shortcut#define Com_example_exercise_mainactivity_default_keys_shortcut 2L# undef Com_example_exercise_mainactivity_default_keys_search_local#define Com_example_exercise_mainactivity_ Default_keys_search_local 3l#undef Com_example_exercise_mainactivity_default_keys_search_global#define Com_    Example_exercise_mainactivity_default_keys_search_global 4l/* * class:com_example_exercise_mainactivity * Method: Test * Signature: () ljava/lang/string; */jniexport jstring jnicall java_com_example_exercise_mainactivity_test (jnienv *, jclass);/* * CLASS:COM_EXAMPLE_EX ercise_mainactivity * Method:updatefile * Signature: (ljava/lang/string;) V */<pre name= "code" class= "Java" >jnica LL Java_com_example_exercise_mainactivity_updatefile (jnienv *, Jclass, jstring)
#ifdef __cplusplus} #endif #endif

The code is long, but for the time being we just look
Jnicall java_com_example_exercise_mainactivity_updatefile  (jnienv *, Jclass, jstring)
This is a method generated based on the native method we defined at the beginning of the mainactivity. With the header file, we can start writing the. c file, i.e. implementing the file, creating a new file Main.c, and then entering the code as follows
#include <stdio.h> #include <stdlib.h>jniexport jstring jnicall java_com_example_exercise_mainactivity_ Test (JNIENV * env, Jobject obj) {return (*env)->newstringutf (env, "Hello world!");}

Returns a string that is the code that Java interacts with C. But now it is not possible to run directly, but also to create a new Android.mk file to configure the project, the code is as follows:
Local_path: =$ (call My-dir) include $ (clear_vars) local_module    : = mainlocal_src_files: = Main.cinclude $ (build_ Shared_library)

Okay, and then you use the method in Mainactivity, the code is as follows
public class Mainactivity extends  Activity {public static native String test ();     @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        TextView t = (TextView) Findviewbyid (R.id.jnitextview);        T.settext (Test ());           }    static {System.loadlibrary ("main");}    }

The import library is used
  static {System.loadlibrary ("main");}
Main is our android.mk in the configuration of a name, now everything is ready, only the compiler generated so file, we open cmd and switch to the project directory, execute Ndk-build, the middle is a minus, not underline, refresh the project can see Libs in a folder and inside a Libmai n.so file, you can run the project now! If there is no accident will appear HelloWorld on the mobile phone frequency screen. Second, print log print log is the only thing that must be mastered, so here is how to configure, the first is to configure the Android.mk file, add a line of code local_ldlibs + =-llog
The complete ANDROID.MK code is as follows
Local_path: =$ (call My-dir) include $ (clear_vars) local_module    : = mainlocal_src_files: = main.clocal_ldlibs    + = -lloginclude $ (build_shared_library)

Then add the header file in the implementation file #include<android/log.h> and the macro defines the type of log to print # define Logi (...) (void) __android_log_print (android_log_info, "native-activity", __va_args__))
#define LOGW (...) (void) __android_log_print (Android_log_warn, "native-activity", __va_args__))
Use Logi () or LOGW in your code to run the program, and then you can print the log! See the NDK chapter in the Java API documentation in more detail. Three, summed up just started to learn JNI, to configure this to configure the very trouble, but wrote a helloworld after the configuration is like that, everything at the beginning difficult Ah! Believe that the back of learning will become more and more difficult, but also more and more interesting, hope to continue to refuel!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android's NDK Learning (1)

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.