I. Why should I use the source code to compile the dll for Calling c into the so library?
Android is a Linux kernel-based mobile terminal system, while dll is a c library generated and called in a windows environment. Therefore, it cannot be called directly for the android system.
Ii. Installation
(I) install cygwin
1. Download cygwin
Cygwin can provide a virtual unix compiling environment on windows, as well as many built-in linux editors and other tools for installation. In this project, cygwin is mainly used to compile and generate the so library. Therefore, install the gcc and make packages.
2. Select "install from Internet"
3. Select the installation directory
4. Select the download storage directory
5. Select "Direct Connection"
6. Select an image point for download.
At the beginning, I selected a strong mirrors.163.com (with a speed of about 51%-K), and downloaded it to (about 1 hour ), later, it was a little slow for me to switch back to mirrors.kernel.org, but the installation was successful (around 40 K, I went out for a stroll, I don't know how long it took, and I came back at noon ).
7. In the "Category" column, click "All" on the right to change Default to "install". Other packages are Default. Install it.
8. Test. On the desktop, open and modify the program and enter the make-v and gcc-v commands to test the program.
(II) install ndk
1. Download ndk({{zip}}, for example, android-ndk-r8-windows.zip
2. Decompress to a directory such as D: \ android \ android-ndk-r8
3. Open home \ Administrator \. bash_profile under cygwin installation directory, and append
NDK =/cygdrive/d/android/android-ndk-r8
Export NDK
(It is said that we should open it with vim or nodepad. Otherwise cygwin won't recognize it. I used vim originally, so I have never tried to open it with txt directly ).
4. Restart and enter cd $ NDK and press Enter. The interface should be as follows:
5. In cygwin, you need to add/cygdrive/d first to change cd to drive d.
3. The first Android project that calls so
1. Create an android Project (MyFirstJNI)
2. Modify the Code as follows:
[Java]
Public class JniTest extends Activity
{/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. main );
TextView TV = new TextView (this );
TV. setText ("" + intFromJNI ());
SetContentView (TV );
}
Public static native int intFromJNI (); // Port
Static
{
System. loadLibrary ("MyFirstJNI ");
}
}
IntFromJNI is the function to be called from so. We need to generate a. h file for the so file first.
3. Use cygwin to jump to the bin \ classes directory, and then enter the javah command to compile the. class file. For example:
Javah-jni com. will. myfirstjni. MyFirstJni
After compilation is successful, A. H file is generated in the classes directory, for example:
Com_will_myfirstjni_MyFirstJni.h
4. Create a folder jni in the project, copy the. h file to it, and add a. c file with the same name. Copy the function in. h to. c and add the function parameters. As follows.
[Cpp]
# Include <string. h>
# Include <jni. h>
JNIEXPORT jint JNICALL Java_com_will_myfirstjni_MyFirstJni_intFromJNI (JNIEnv * k, jclass j)
{
Return (jint) 100000;
}
5. Copy the Android. mk file to the jni directory of the project under the sample \ hello-jni \ jni directory of the ndk directory.
Modify the following two lines of code:
[Html]
LOCAL_MODULE: = MyFirstJNI
LOCAL_SRC_FILES: = com_will_myfirstjni_MyFirstJni.c
LOCAL_MODULE -- project name
LOCAL_SRC_FILES -- c file name
6. In cygwin, jump to the jni directory and enter the $ NDK/ndk-build command to compile the file. The libs \ armeabi \ libMyFirstJNI. so file is generated. If not, right-click refresh.
7. Start the simulator. The effect is as follows. ,