### NDK development process under Eclipse

Eclipse first, AS later. The tools are different.

### Specific process

  1. Write Java layer Native methods
  2. The javah command generates the header file
  3. Create the JNI directory
  4. Configure the NDK path, add native support, and configure the header files required by ADT
  5. Implement the functions defined in the header file
  6. Compile to generate. So dynamic library
  7. Loading the Dynamic library

To create a simple file encryption solution APP as an example:

#### first, write Java layer Native method

We created a Cryptor class specifically for file encryption and decryption:

public class Cryptor {

	public static native void cryptFile(String src , String dest);

	public static native void decryptFile(String src , String dest);

}
Copy the code

Here write two native methods, respectively for file encryption and decryption.

#### 2. Run the javah command to generate the corresponding header file

From the command line, use the CD command to go to the project’s SRC directory (code directory) and then execute the following command:

Javah Full class nameCopy the code

The corresponding header file is generated.

#### create the jNI directory

Create it directly from Eclipse and put in the header file we just generated.

#### 4. Configure the NDK path, add local support add Native support, and configure the header file required by ADT

If the NDK path is not configured, you need to configure it first:

Windows – > Preference > Android — – > the NDK:

To add Native Support, right-click the project, select Android Tools, select Add Native Support, and then enter the name of Lib. The default prefix is lib.

Some files are then automatically generated in the JNI directory:

Where application. mk is the so dynamic library file we added ourselves, which is used to specify the output architecture. Application.mk is as follows:

# armeabi armeabi-v7a arm64-v8a mips mips64 x86 x86_64
APP_ABI := armeabi
Copy the code

The first line is a comment.

To configure the NDK header files that need to be included in ADT, right click on Project –> Properties –>C/C++ General –> Paths and Symbols and add the following directories for NDK:

G: \ android - the NDK - r10 \ toolchains \ arm - Linux - androideabi - 4.9 \ prebuilt \ Windows - x86_64 \ lib \ GCC \ arm - Linux - androideabi \ 4.9 \ includ e G: \ android - the NDK - r10 \ toolchains \ arm - Linux - androideabi - 4.9 \ prebuilt \ Windows - x86_64 \ lib \ GCC \ arm - Linux - androideabi \ 4.9 \ includ e-fixed G:\android-ndk-r10\platforms\android-L\arch-arm\usr\includeCopy the code

###### Note: my version may not be the same as yours, but it’s similar

The configured figure is as follows :(some of the directories were generated later)

#### implement the functions defined in the header file

Let’s implement it in crypt.c, which was originally a CPP file, so we’ll change it to C for now:

#include "com_example_ndk_file_crypt_utils_Cryptor.h" #include <stdlib.h> #include <stdio.h> #include <string.h> char password[] = "I AM MI MA"; JNIEXPORT void JNICALL Java_com_example_ndk_1file_1crypt_utils_Cryptor_cryptFile( JNIEnv * env, jclass jclz, jstring src, jstring dest) { const char* c_src = (*env)->GetStringUTFChars(env, src, NULL); const char* c_dest = (*env)->GetStringUTFChars(env, dest, NULL); FILE* f_read = fopen(c_src, "rb"); FILE* f_write = fopen(c_dest, "wb"); Whether it is right to open the if / / judgment documents (f_read = = NULL | | f_write = = NULL) {printf (" the file open field "); return; } // read one character at a time int ch; int i = 0; int pwd_len = strlen(password); while ((ch = fgetc(f_read)) ! = EOF) {fputc(ch ^ password[I % pwd_len], f_write); i++; } // Close the file fclose(f_read); fclose(f_write); } JNIEXPORT void JNICALL Java_com_example_ndk_1file_1crypt_utils_Cryptor_decryptFile( JNIEnv * env, jclass jclz, jstring src, jstring dest) { const char* c_src = (*env)->GetStringUTFChars(env, src, NULL); const char* c_dest = (*env)->GetStringUTFChars(env, dest, NULL); FILE* f_read = fopen(c_src, "rb"); FILE* f_write = fopen(c_dest, "wb"); Whether it is right to open the if / / judgment documents (f_read = = NULL | | f_write = = NULL) {printf (" the file open field "); return; } // read one character at a time int ch; int i = 0; int pwd_len = strlen(password); while ((ch = fgetc(f_read)) ! = EOF) {fputc(ch ^ password[I % pwd_len], f_write); i++; } // Close the file fclose(f_read); fclose(f_write); }Copy the code

Then remember to configure the Android.mk file and change the value of LOCAL_SRC_FILES to crypt.c (originally CPP).

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := crypt
LOCAL_SRC_FILES := crypt.c

include $(BUILD_SHARED_LIBRARY)
Copy the code

#### 6. Compile and generate. So dynamic library

Just make project.

#### 7. Load dynamic library

Load the dynamic library. So file using the system. loadLibrary method.

Public class Cryptor {static {// load dynamic library. So file, note not to write lib prefix, System. LoadLibrary ("crypt"); } public static native void cryptFile(String src , String dest); public static native void decryptFile(String src , String dest); }Copy the code

The rest is to write two buttons in the Android layout, test it, remember to add SD card access:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Copy the code

Finally, if you want to adapt to all platforms, you need to modify application. mk to:

APP_ABI := armeabi armeabi-v7a arm64-v8a mips mips64 x86 x86_64
Copy the code

Since the build process is cross-compiled, it is very slow, about five minutes, so if the C code does not change, we can do the following configuration without compiling the C code:

Just take the two ticks off.

If you feel that my words are helpful to you, welcome to pay attention to my public number:

My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.