The JNI demo of the Android Jni simple demo (2) is written
first, build Cygwin environment : http://blog.csdn.net/androidolblog/article/details/25559013
Ii. the basic elements of JNI:
1. Write Java file with native method.
2, the file with Javah generated Cygwin compile the library to use the header file, both *.h file.
3, write *.c file, in the *.c file, implement the method in *.h.
4, write the Mk file.
5, configure the NDK environment.
6, compile the library file with Cygwin.
7. Copy library files to the JNI demo in Eclipse Engineering.
8. Call the native method in Jni.
Third, the JNI demo written in detail:
1 . Java class for writing native methods
Myjnimethod. JavaPackage Com.lin.myjin;
Public class Myjnimethod { static{system.loadlibrary ("Myjin"); }
Public native void Add (int add);
Public native void print (int i);
public native int getadd (int add);}
2, the file with Javah generated Cygwin compile the library to use the header file, both *.h files:
The build steps are as follows: The following text is found online. I think it's a pretty full story. Just put it on the right.
Suppose you are developing with eclipse, for example, in the Myjin directory of the D drive, you have a class Test.java with package paths, such as the packages cn.edu.xmu.zgy;
That is, your Test.java file will be stored in the D:\MyTest\src\cn\edu\xmu\zgy\ directory.
Then to generate the. h file, after switching to the D:\MyTest\bin\classes directory under the console and then entering Javah-jni cn.edu.xmu.zgy.Test, you can D:\MyTest\bin\classes\ The cn_edu_xmu_zgy_test.h header file can be found in the directory.
Principle: After the Android project under Eclipse is compiled, Java will generate a byte-code class file. Use the classes directory to jump directly to the compiled project, and then use the Java-jni *.class file name. Generate H file.
Note :
(1) The generated *.h must be the H file with the package name. In the directory where the *.class file is located, the file generated Javah-jni * is not with the package name.
(2) Here's javah-jni cn.edu.xmu.zgy.Test instead of javah-jni Cn.edu.xmu.zgy.Test.class
Here is the header file I generated: The project name is Myjin
The resulting com_lin_myjin_myjnimethod.h file is as follows:
/* Don't EDIT this file-it are machine generated * /#include <jni.h>/ * Header for class Com_lin_myjin_myjnimethod * /#ifndef _included_com_lin_myjin_myjnimethod #define _included_com_lin_myjin_myjnimethod #ifdef __cplusplusextern "C" {#endif/** Class:com_lin_myjin_myjnimethod* Method:add* Signature: (I) V */jniexport void Jnicall java_com_lin_myjin_myjnimethod_add (JNIEnv *, Jobject, jint);/** Class:com_lin_myjin_myjnimethod* Method:print* Signature: (I) V */jniexport void Jnicall java_com_lin_myjin_myjnimethod_print (JNIEnv *, Jobject, jint);/** Class:com_lin_myjin_myjnimethod* Method:getadd* Signature: (i) I */jniexport jint jnicall java_com_lin_myjin_myjnimethod_getadd (JNIEnv *, Jobject, jint);#ifdef __cplusplus}#endif#endif
3, write *.c file, in the *.c file, implement the method in *.h:
Here to:
jniexport jint jnicall java_com_lin_myjin_myjnimethod_getadd (JNIENV * env, Jobject obj, jint count) { return count+8; }as an example
COM_LIN_MYJIN_MYJNIMETHOD.CFile Contents:
#include "com_lin_myjin_myjnimethod.h"//#define LOG_TAG "Android-t"//#undef LOG//#include <android/log.h>//#define LOGD (a) __android_log_print (android_log_debug,log_tag,a)//#define LOGD2 (A, B) __android_log_print (android_log_debug,log_tag,a,b)//#define DBUG#include <sys/ioctl.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <termios.h>jniexport void Jnicall java_com_lin_myjin_myjnimethod_add(JNIENV * env, Jobject obj, jint count) { }/** Class:com_lin_myjin_myjnimethod* Method:print* Signature: (I) V */jniexport void Jnicall java_com_lin_myjin_myjnimethod_print(JNIENV * env, Jobject obj, jint count) { }/** Class:com_lin_myjin_myjnimethod* Method:getadd* Signature: (i) I */jniexport jint jnicall java_com_lin_myjin_myjnimethod_getadd (JNIENV * env, Jobject obj, jint count) { return count+8; }
4. Write the Mk file:about the Mk file. Can be understood by themselves. Do not repeat here.
Android.mk file: Content: Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = myjinlocal_src_files: = Com_lin_myjin _myjnimethod.cinclude $ (build_shared_library)
5. Configure the NDK environment:
Here, the NDK does not have to configure environment variables. The principle is to manipulate commands with absolute paths. Under Cygwin, jump to the directory of your C file H file Mk file. or copy the three files to a directory called JNI. The catalogue name can be taken casually. Then jump to the directory containing the three files, enter:
/cygdrive/d/android-ndk-r5b/ndk-buildEnter.
/cygdrive/d/This section changes depending on the directory where you store the android-ndk-r5b/ndk-build. As long as it can be directly accurate to android-ndk-r5b/ndk-build on the line. Rationale: The $ symbol takes the command or directory added in the environment variable.
Example:To customize a mycommand command, the implementation command is CD. . That is, jump to the upper directory. After you add mycommand successfully, you can implement the CD directly with $myCommand. So: the Ndk-build command on the Internet is actually/cygdrive/d/android-ndk-r5b/ndk-buildconvenient mode of operation. The premise is that you added the ndk-build environment variable. mycommand command:
6, compile the library file with Cygwin steps:
(1) Create a new JNI directory:
(2) put three files under the catalogue: jump to the appropriate directory input:/cygdrive/d/android-ndk-r5b/ndk-build return
Generated Library files:
6, copy the library file to Eclipse Engineering project, here is Myjin: This name is intended to play myjni. Everyone is good as a project name.
Attention:
(1) The library file name is generally lib**.so loaded when the name is **.so such as the above library files are libmyjin.so and loading is Myjin
(2) The file name does not have a library file suffix when loading the library. So
(3) Load library code block to load statically.
7. Call the native method in JNI:
The operating result is:
It can be seen that the addition of 12 is the result of 4+8. The description code has been called normally.
Project Demo Source Link: http://download.csdn.net/detail/linhuorongandroid/7332143
Conclusion: The code is very simple on demo. The goal is to run a JNI process. There is not enough, a lot of advice.