Getting Started with JNI-learning notes
可执行文件 Windows - *.exe | linux - *.elfc函数库文件 Windows - *.dll | linux - *.so批处理文件 Windows - *.bat | linux - *.sh
Tools
NDK--Native developer kits
Cygwin--Linux system simulator
- Installation process points:--Installation can be networked installation can also be installed through a local file (if there is a local file)--select Packages: No need to install all, install devel and shells both--after installation, double-click the Desktop shortcut icon to enter the Cygwin command console directive: CD. | CD ... | ls | CD cygdrive
- To facilitate the execution of instruction Ndk-build in any directory, the configuration of the environment variable is required: The installation directory that comes to Cygwin. /cygwin/etc/profile, open the profile file, modify the value of path, and separate the multiple environment variables with a colon (:). For example: Path= "...:/cygdrive/c/android-ndk-r7b: ..." If not successful, you can also configure system environment variables under Windows to run with cmd
CDT--to facilitate the development of C code in Eclipse let C code highlight
- Full Name: cdt:c/c++ developer Tools adt:android Developer Tools
- Installation steps: Eclipse--and help--Install new software----name:cdt| Location:adt.zip--and next. At this time there are two plug-ins for the selection of CDT Main Features and CDT Optional Features, only select the former, contact all update sites ... This option is a network update, can not be selected, next, installed after the eclipse will have a C/s view
HelloWorld of JNI Development
First step: Create a new Android project, define a local method
public class MainActivity extends Activity { // 定义一个本地方法,相当于c的接口,需要用c代码对这个方法做实现 public native String helloFromC(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } ...}
The second step: Create a new folder under the project called JNI, it must be the name of the file, and then create a new file under the folder, such as called hello.c, in the hello.c file to write C code, this method name is relatively long, Quick Build Mode: CMD--CD to Project src directory, input command Javah com.example.helloworldformc.MainActivity (full class name), A. h header file will be generated next to the SRC directory, and the file will be copied to the JNI directory, and the file can be opened with the method of removing the semicolon plus {}.
#include <stdio.h>#include <jni.h>// Java+全类名+方法名,用_连接每个单词jstring Java_com_example_helloworldformc_MainActivity_helloFromC(JNIEnv* env,jObject obj){ return (*env)->NewStringUTF(env,"hellofromc");}
Step Three: create a android.mk file under the Jni folder
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)# 对应打包成函数库的名字,你想打包成什么文件名,这里就写成什么,LOCAL_MODULE := hello# 对应C代码的文件名LOCAL_SRC_FILES := Hello.cinclude $(BUILD_SHARED_LIBRARY)
Fourth Step:
Package C code into function library:cmd--> CD to Android project directory--Ndk-build (cross-compile command), refresh the project, in Libs/armeabi there will be a. So file, the file name is Lib+hello ( The name of the library as defined in MK), and there will be more than one obj folder in the project, mainly in intermediate files
Fifth Step: introducing a library of functions into Java code
// 使用静态代码块在Java代码中引入函数库static{ System.loadLibrary("hello"); }
Sixth step:
public void method(){ // 调用 String str = helloFromC(); Toast.makeText(getApplicationContext(), str, 1).show();}
* * NOTE * *
- When referencing someone else's. So library, you need your own native method the package name of the corresponding class is the same as the package name when someone else packaged it into a. So function library.
If you need to print the log in the C language, you need to introduce a header file
Eg: #include <android/log.h>
But the log method name is too long to remember, we need to do the next processing
Eg: #define LOGTAG "Clog" #define LOGD (...) androidlogprint (AndroidlogDEBUG, logTAG, __vaARGS) #define Logi (...) androidlogprint (AndroidlogINFO, logTAG, _VAARGS)
Also add a sentence in the Mk file: Local_ldlibs + =-llog
In C code like this call: Logd ("x =%d", x);
C Call Java
Principle: C By Reflection method call Java method C has similar to get class object, get Methodid, call five return value method Callvoidmethod and so on method above method can go to the head file jni.h to find, Jni.h directory: Android-ndk-r7b\platforms\android-8\arch-arm\usr\include You can implement C code calling Java code through these methods Get method Signature: The full class name of the class where the Javap-s method is located under the Classes folder on the CD to the bin directory
C
头文件 *.h库文件 *.so/*.o两者关系的比喻:头文件相当于Java中的接口文件,库文件相当于Java中的实现类文件
工具下载:http://pan.baidu.com/s/1mg2zvN6
Getting Started with JNI-learning notes