Android Study Notes: Summary of ndk

Source: Internet
Author: User

1. Installation

After downloading the ndk package, put it in the directory of the same level as the SDK and configure the environment variables. After the configuration is completed, enter the ndk-build command on the terminal to test the installation. If the following occurs, the installation is successful.

Android NDK: Could not find application project directory !    Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.    /home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85: *** Android NDK: Aborting    .  Stop.

 

2. Engineering and code

A. Create an android project, write Java code, write the required local method, and load the c file name.

B. Generate the. h c file based on the written Java code. The steps for generating a. h file are as follows:

1. Go to the project directory

Braincol @ Ubuntu :~ $ CD workspace/Android/ndk/Hello-JNI/

2. ls viewing project files

Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ ls
Androidmanifest. xml assets bin default. properties Gen res SRC

3. Create a JNI folder under the project directory:

Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ mkdir JNI

Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ ls
Androidmanifest. xml assets bin default. properties Gen JNI res SRC

4. Execute the following statement to generate the corresponding. h file:

Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ javah-classpath bin-d jni com. example. hellojni. hellojni

C. Compile the corresponding. c file based on the. h file generated in B, and implement the method in the. h file according to your own needs.

D. Compile the Android. mk file in the following format:

Local_path: = $ (call my-DIR) include $ (clear_vars) local_module: = hello-JNI // the target object to be compiled. The system will generate 'libello-JNI. so 'file for the Java file to call local_src_files: = hello-jni.c // compiled source file, the system will generate the target object include $ (build_shared_library) based on the file)

E. generate. so shared library file, enter the directory of the project in the terminal, enter the ndk-build command, you can generate in the lib directory. to run the project.

 

3. debug statement output

In ndk, printf () cannot be output, so we need to use the log library to output the content in our C code library through the Java console. Call function _ android_log_print () to view the related output information in eclipse.

 

Therefore, in the compiling of the C file, Android/log. h file must be introduced:

# Include <Android/log. h>

At the same time, declare the printing method:

# DEFINE _ android_log_print (...); for example:

#include <string.h>#include <stdio.h>#include <jni.h>#include <android/log.h>#define LOG_TAG "MYJNI"#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)static char s_string[] = "My god, I did it!";jstringJava_com_jpf_myjni_MyJNI_stringFromJNI( JNIEnv* env,jobject thiz ){       LOGI("MyJNI is called!");       return (*env)->NewStringUTF(env, s_string);} 

 

Because printing is a linked log library, add the following sentence to the Android. mk file:

Local_ldlibs + =-llog

Re-run ndk-build and run the project. You can find the print statement in the C file in logcat.

 

Refer:

Http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html

Http://blog.csdn.net/xuxinyl/article/details/6409030

 

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.