Android NDK notes and androidndk notes

Source: Internet
Author: User

Android NDK notes and androidndk notes

**************************************** **************************** **********************************
NDK development steps
1. Create a project
2. Create a JNI directory
3. Compile the nativejava Layer Method
4. Generate the JNI header file
(1) Compile javaNative code
(2) Introduce a command javah
(3) Add the android. jar package to the Environment Variable


Declare An native METHOD

Create a JNI directory

Go to the current project directory on the console

Enter the command
E: \ android workspace \ HelloNDK_1> javah-classpath bin/classes; D: \ android _
Sdk_windows \ platforms \ android-19 \ android. jar-d jni com. example. hellondk_1.MainA
Ctivity
Generate the JNI header file (where, D: \ android \ android_sdk_windows \ platforms \ android-19 \ android. jar,
Is the directory file under the android sdk directory. To make it simple, you can add the android. jar file directory to the path;
Com. example. hellondk_1.MainActivity is the package name and class name)

Refresh the project and you can see that there is an additional. h file under the jni directory.

Create a. c file and import the corresponding header file
(
For example, create a hello. c file.
# Include <stdio. h>
# Include <stdlib. h>
# Include "com_example_hellondk_1_MainActivity.h"

JNIEXPORT jstring JNICALL Java_com_example_hellondk_11_MainActivity_getStringFromC
(JNIEnv * env, jclass ){

Return (* env)-> NewStringUTF (env, "Hello from C !.... ");
}
The method Java_com_example_hellondk_11_MainActivity_getStringFromC (JNIEnv *, jclass) in the header file is implemented );
)

Add the mk file in the jni directory. (You can import the mk file under the ndk instance and modify it .)
LOCAL_PATH: = $ (call my-dir)

Include $ (CLEAR_VARS)

LOCAL_MODULE: = hello
LOCAL_SRC_FILES: = hello. c

Include $ (BUILD_SHARED_LIBRARY)
Where,
LOCAL_MODULE indicates the name of The so generated by compilation.
LOCAL_SRC_FILES indicates the. c file to be compiled

Finally, run the ndk-build command in the project path.
After the execution, refresh the project. You can see that there is an additional. so file in the obj directory under the project.
Then add a static code block to the java file. Add the. so file.

Finally, you can see the result by running the code.
**************************************** **************************** **********************************
JNIEnv * env indicates the environment of the current thread
Jobject thiz indicates the current instance
Jint si indicates an int type variable.
JfieldID fid indicates a field or a java attribute.
Jclass cls indicates a class or instance
JmethodID mid indicates the id of a method in a class

Cls = (* env)-> GetObjectClass (env, thiz );
Mid = (* env)-> GetStaticMethodID (env, cls, "callback", "() V") // The third parameter represents a static method name.

Fid = (* env)-> GetStaticFieldID (env, cls, "si", "I"); // The last two indicate the variable name and data type.

Si = (* env)-> GetStaticIntFieldId (env, cls, fid); // obtain the si data in the fid field and assign it to si.


LOCAL_LDLIBS + =-llog // Add a logstore to the mk File

# Include <android/long. h> // Add this header file to the c file and add it to the log developed by android
_ Android_log_print (ANDROID_LOG_INFO, "stringFromJNI ()", "xxx"); // print information
_ Android_log_print (ANDROID_LOG_INFO, "stringFromJNI ()", "var =", var); // print the variable information

Java ----> jni -----> c/c ++ data conversion Sequence

JNI Array Processing

Many methods can be found in the D: \ android \ android-ndk-r9c \ platforms \ android-16 \ arch-arm \ usr \ include \ h = jni. h header file there are also official documentation

**************************************** **************************** **********************************
Eclipse configuration ndk development (see video tutorial: http://www.imooc.com/video/8028)
1. Select the ndk directory in Eclipse (Window ---> Preferences ---> Android ----> NDK ----> select the NDK directory Location in the ndk Location)

Ii. Configure and generate javah options
Select External Tools (the one next to the running button) -----> right-click Program new -----> enter Name (for example, generate_c ++ _ header_file)
-----> Select Variables under the Main tab (select System_path and add a colon: Add javah) ------> then select Working Directory
Add Variables (select project_loc and add \ jni for example "$ {project_loc} \ jni") ------> then add the Arguments Parameter
(-Classpath $ {project_loc} \ bin \ classes; D: \ android \ android_sdk_windows \ platforms \ android-19 \ android. jar-d
$ {Project_loc} \ jni-jni $ {java_type_name}) where $ xxx can be selected in Eclipse. -----> At last, run displays
Ignore errors.
Create a jni directory under the project. Add the native method to the java file, select the java file, and select the run button.
Select the configured generate_c ++ _ header_file as the green button next to it and click Run. Then, refresh it to see that the. h file is generated under the jni directory.
(Note: do not include spaces in the working directory. Otherwise, some errors may occur ).


3. Configure eclipse to add native support for jni method code prompts
Right-click the working directory and choose android Tools ---> add native support
Right-click the project and choose properties> C ++ General ---> Path and Symbols ---> add under des> File System
(Select include in the directory where ndk is located: for example, "D: \ android \ android-ndk-r9c \ platforms \ android-19 \ arch-arm \ usr \ include ")
Click the application, and a new pair of des folder is added under the project.
Then you can press ctrl + alt or alt +/to get the code prompt when writing jni code.

4. Configure the javap generation Option
Similar to javah configuration,
Location is $ {system_path: javap}
Working Directory is $ {project_loc}
Arguments is-classpath $ {project_loc} \ bin \ classes-s-p $ {java_type_name}

You can also configure the ndk-build command

**************************************** **************************** **********************************
JNI Data Types
(
Reference Official Website: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/types.html#wp428
Or blog: http://blog.csdn.net/conowen/article/details/7523145
)
**************************************** **************************** **********************************

 

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.