Use steps
first, define the local method,Typically, a class should be defined separately to encapsulate all native methods
/** class for storing native methods */
public class mynativemethods { private static mynativemethods memployee; Private mynativemethods () { }Public static mynativemethods getinstance () { if (memployee = = null) { Memployee = new mynativemethods (); } return memployee; } //is equivalent to defining an interface in Java code and then implementing this interface in C language Public native String HELLOFROMC (); Public native int PASSWORDFROMC (int x, int y);}
|
Second, create a "Jni folder" in the project root, create a ". c file" in the Jni folder, and implement the method defined above in C code.
#include <stdio.h> #include <stdlib.h> #include <jni.h>//header files that must be added jstring java_com_bqt_hellofromc_ Mynativemethods _HELLOFROMC(jnienv* env, jobject obj) { //"Return value" "Method name" "Parameter list" The return value type Jstring is a string in Java Char* cstr = "Hello from C"; //char* can be used to represent a string in C. Note that there is absolutely no Chinese jstring jstr = (*env)->newstringutf (env, CSTR); return jstr;} jniexport jint jnicall java_com_bqt_hellofromc_ Mynativemethods _PASSWORDFROMC(jnienv *env, Jobject obj, jint A, jint b) { return A + B + 10000; the number of bytes in the//c may be different in different environments, possibly 0-65535, so a slightly larger number (level 100,000) has to be double } |
third, compile C + + code into "dynamic link library. So file"
1, in the project JNICatalogueCreate a "Android.mk file" in which the "name of the library" and the corresponding "C code filename" are defined
Local_path: = $ (call My-dir) # C + + code in the directory, which is our JNI directory, do not have to modify include $ (clear_vars) local_module:= Hello # corresponding to the name of the function library, the compiler will automatically precede the Lib, followed by the. So, the end result is libhello.so local_src_files:= hello.c # The file name of the corresponding C code, i.e. hello.c
include $ (build_shared_library) |
2. Build with tool compilationdynamic link library. So filecan useCygwin tool compilation, where the third step is the Android project root directory
Cd.. /.. CD cygdrive/ CD D/USERS/ANDROID_WORKSPACE/HELLOFROMC
Ndk-build
|
or compile directly with CMD, note that you need to add the path to the NDK tool in the system variable path first, such as "D:\Android\android-ndk-r10d"
cd/d D:\Users\Android_workspace\HelloFromC Ndk-build |
Process:
A "libhello.so" file is generated in the project Libs/armeabi directory after successful compilationthe meaning of Ps,eabi is: Embedded application binary Interface (embedded application binary interface)
3. To support platforms outside of ARM, you need to add "application.mk" files to the JNI directory, plus the following:
App_abi: = Armeabi armeabi-v7a x86 the purpose of #Application. mk file is to describe all required modules (i.e. static or dynamic libraries) in your applicationThe value of the #APP_ABI is distinguished by a space, which represents the schema to be supported, and the default value is "Armeabi". Other architectures, ARMv7 "armeabi-v7a"; IA-32 "x86 "#每增加一个架构, a corresponding folder will be generated in the Lib directory after compilation, and the files under the folder will have the same name. So file (of course, the file content is different) |
After the compilation succeeds, multiple folders and. So files are generated in the Lib directory
4, careful observation will find that, in addition to the automatic generation of the Libs directory in the. so file, but also produced a much larger than the Libs directory of the obj directory, and its structure and content is very similar
The information found online is interpreted as: as part of the build process procedure, the files in the Libs folder has been stripped peeling of symbols symbols and debugging in Formation. So you'll want to keep and copies of each of the Your. So Files:one from the Libs folder to install on the Android device, an D one from the obj folder to installation for GDB a debug tool to get symbols from. In a nutshell, with the sign and debug information under Obj, Lib is a dynamic link library file that is stripped of these huge information and installed in On Android devices, only the. So file in the Libs directory is required.
5, when the completion of this step, if we do not need to recompile later, we can directly delete the JNI directory and the obj directory
Iv. load the. So class library in Java code, and then you can call the local method.
static { System. LoadLibrary("Hello"); //In Java code, the library function under the Libs directory is introduced, and the file name is "Libhello.so". Note that the file name at the time of introduction should be removed from the previous Lib and later. So } String STRINGFROMC = mynativemethods. getinstance (). HELLOFROMC (); int INTFROMC = mynativemethods. GetInstance(). PASSWORDFROMC (1, 2);
|
The above tests are perfectly passed
From for notes (Wiz)
List of attachments
JNI detailed use steps to get started example