Android NDK Notes

Source: Internet
Author: User

Gorgeous Split-line ******************************************************** ******
NDK Development Steps
1. Create a project
2. Create a JNI directory
3. Writing the Nativejava layer method
4. Generating JNI header files
(1) Writing javanative code
(2) Introduction of an order Javah
(3) Add Android.jar package to environment variable


Declaring a native method

Create a JNI directory

The console enters the current project directory

Input command
E:\android Workspace\hellondk_1>javah-classpath bin/classes;d:\android\android_
sdk_windows\platforms\android-19\android.jar-d JNI Com.example.hellondk_1.MainA
Ctivity
Generate JNI header files (where, D:\android\android_sdk_windows\platforms\android-19\android.jar,
is the directory file in the Android SDK directory, in order to be simple, you can add the Android.jar file directory to the path;
The last com.example.hellondk_1.MainActivity is the package name plus class name)

Refresh the project to see one more. h file under the JNI directory

Create a. c file to import the appropriate header file
(
Such as: 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 jclass) {

Return (*ENV)->newstringutf (env, "Hello from C!....");
}
Which implements the method Java_com_example_hellondk_11_mainactivity_getstringfromc in the header file (jnienv *, jclass);
)

Add the Mk file under the JNI directory. (This can be imported into the NDK under the example of the Mk file and then modify it) as follows
Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_module: = Hello
Local_src_files: = hello.c

Include $ (build_shared_library)
which
Local_module is the name of the build-generated so name
Local_src_files indicates which of the. c files to compile is

Finally, execute the ndk-build command under the project path.
Refresh the project after execution you can see that there is one more. so file in the obj directory below the project.
Then add the static code block in the Java file. Add the. so file.

You can see the result by running the code at the end.
Gorgeous Split-line ******************************************************** ******
jnienv* env indicates the environment of the current thread
Jobject Thiz represents the current instance
Jint SI represents a variable of type int
Jfieldid FID represents a field or a Java property.
Jclass CLS represents a class or an instance
Jmethodid mid indicates the ID of a method inside 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 following two represents the variable name and data type

Si= (*env)->getstaticintfieldid (ENV,CLS,FID);//Get FID the SI data in this field is assigned to SI


local_ldlibs+=-llog//Adding a log library to the Mk file

#include <android/long.h>//Add this header file to the C file to join the Android-developed log
_android_log_print (Android_log_info, "Stringfromjni ()", "xxx");//Printing information
_android_log_print (Android_log_info, "Stringfromjni ()", "var=", Var);//print variable information

Java---->JNI----->c/c++ Data Conversion sequence

JNI array Processing

There are many ways to see the d:\android\android-ndk-r9c\platforms\android-16\arch-arm\usr\include\h=jni.h header file and official documentation.

Gorgeous Split-line ******************************************************** ******
Eclipse Configuration NDK Development (see video Tutorial: http://www.imooc.com/video/8028)
First, select the NDK directory in Eclipse (Window--->preferences--->android---->ndk----> select the NDK directory location in the NDK locations)

Ii. configuration Generation Javah options
Select External Tools (the one next to the Run button)-----> select Program Right-click New-----> enter name (such as Generate_c++_header_ File)
-----> select variables under the Main tab (select System_path and then colon: add Javah)------> and select Working Directory
Add variables (select Project_loc and add \jni for example "${project_loc}\jni")------> then add 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 within Eclipse. -----> Last Run the
error that occurred with Eclipse is not in the pipeline.
Use to create a new JNI directory underneath the project. Then add the native method to the Java file, then select the Java file and select the green button next to the Run button
to select our configured Generate_c++_header_file click to run, Then refresh to see the JNI directory generated under the. h file
(note: The working directory is best not to take a space otherwise there will be some errors).


Iii. configuring Eclipse Add native support Jni method code hints
Right-click on the working directory to select Android Tools--->add native support
Select the project right-click Properties---->c++general--->path and Symbols--->includes add---->file System
(select the Include in the directory where the NDK is located: for example, "D:\android\android-ndk-r9c\platforms\android-19\arch-arm\usr\include")
Then click Apply, and then the project has one more includes folder under it
You can then press Ctrl+alt or alt+/to get code hints when you write JNI code.

Iv. Configuration Generation JAVAP Options
Similar configuration Javah,
Location is ${SYSTEM_PATH:JAVAP}
Working directory is ${project_loc}
Arguments to-classpath ${project_loc}\bin\classes-s-P ${java_type_name}

You can also configure the Ndk-build command

Gorgeous Split-line ******************************************************** ******
JNI data Types Explained
(
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
)
Gorgeous Split-line ******************************************************** ******

Android NDK Notes

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.