Original article: Android calls the JNI process to facilitate subsequent operations.
I personally recorded the entire process of calling JNI, and I forgot to save it. Ah.
In some cases, the ndk environment and ndk source code must be prepared. cygwin must be installed with a profile. Java requires environment variables.
1. Create a new project and create a class to declare the method that calls native.
Package com. Stay. JNI;
Public class callnative {
Public callnative (){
}
Static {// remove the preceding lib from the imported lib name
System. loadlibrary ("Skytech ");
}
Public native byte [] readcmd (string path );
Public native int writecmd (byte [] cmd, string path );
}
2. Build the project, open cmd, CD to the bin directory of the project,
D: \> Cd D: \ Android \ rayn \ testjni \ bin
D: \ Android \ rayn \ testjni \ bin> javah-JNI com. Stay. JNI. callnative
Then a. h file will appear in the bin directory. This is the declared header file.
3. What we need is the method declared in it. Just change the c file we want to write to the same method name. (I wrote the implementation into the. h file and kept compiling with the. h file .. =)
4. Create a JNI directory in the project and copy the changed C file and MK file (copy one file at will, change the path and Lib name) to the directory.
5. Compile the program according to the ndk method:
- Place your native sources under
<Project>/JNI /...
- Create
<Project>/JNI/Android. mk
To describe your native sources to the ndk build system
- Optional: Create
<Project>/JNI/application. mk
.
- Build your native code by running the 'ndk-build' script from your project's directory. It is located in the top-level ndk directory:
CD <project> <ndk>/ndk-build
The build tools copy the stripped, shared libraries needed by your application to the proper location in the application's
I personally recorded the entire process of calling JNI, and I forgot to save it. Ah.
In some cases, the ndk environment and ndk source code must be prepared. cygwin must be installed with a profile. Java requires environment variables.
1. Create a new project and create a class to declare the method that calls native.
Package com. Stay. JNI;
Public class callnative {
Public callnative (){
}
Static {// remove the preceding lib from the imported lib name
System. loadlibrary ("Skytech ");
}
Public native byte [] readcmd (string path );
Public native int writecmd (byte [] cmd, string path );
}
2. Build the project, open cmd, CD to the bin directory of the project,
D: \> Cd D: \ Android \ rayn \ testjni \ bin
D: \ Android \ rayn \ testjni \ bin> javah-JNI com. Stay. JNI. callnative
Then a. h file will appear in the bin directory. This is the declared header file.
3. What we need is the method declared in it. Just change the c file we want to write to the same method name. (I wrote the implementation into the. h file and kept compiling with the. h file .. =)
4. Create a JNI directory in the project and copy the changed C file and MK file (copy one file at will, change the path and Lib name) to the directory.
5. Compile the program according to the ndk method:
- Place your native sources under
<Project>/JNI /...
- Create
<Project>/JNI/Android. mk
To describe your native sources to the ndk build system
- Optional: Create
<Project>/JNI/application. mk
.
- Build your native code by running the 'ndk-build' script from your project's directory. It is located in the top-level ndk directory:
CD <project> <ndk>/ndk-build
The build tools copy the stripped, shared libraries needed by your application to the proper location in the application's