(創(chuàng)建于2017/12/2)
1.編寫native方法
package com.example.ndk_file_encrpty;
public class Cryptor {
static{
System.loadLibrary("ndk_file_encrpty");
}
public native static void encrypt(String normal_path,String crypt_path);
public native static void decrypt(String crypt_path,String decrypt_path);
}
2.進入eclipse項目src目錄下(cd 進入到src目錄下,使用命令生成頭文件 javah 包名+類型(如 com.renzhenming.utils.JniUtils)),如果是進入的bin目錄下使用此命令行的話,得到的頭文件只有一些聲明清寇,沒有生成相應(yīng)的jni方法
bin目錄下生成的是這樣的
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ndk_file_encrpty_Cryptor */
#ifndef _Included_com_example_ndk_file_encrpty_Cryptor
#define _Included_com_example_ndk_file_encrpty_Cryptor
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
src目錄下生成的才是我們需要的
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ndk_file_encrpty_Cryptor */
#ifndef _Included_com_example_ndk_file_encrpty_Cryptor
#define _Included_com_example_ndk_file_encrpty_Cryptor
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_ndk_file_encrpty_Cryptor
* Method: encrypt
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_example_ndk_1file_1encrpty_Cryptor_encrypt
(JNIEnv *, jclass, jstring, jstring);
/*
* Class: com_example_ndk_file_encrpty_Cryptor
* Method: decrypt
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_example_ndk_1file_1encrpty_Cryptor_decrypt
(JNIEnv *, jclass, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
有一點需要注意的是:對于像這樣包名這樣命名的情況因為存在下劃線,所以如果你直接按照平常的那種寫法去手寫,比如寫成這樣
JNIEXPORT void JNICALL Java_com_example_ndk_file_encrpty_Cryptor_encrypt
是錯誤的,正確的寫法是如上邊生成的
JNIEXPORT void JNICALL Java_com_example_ndk_1file_1encrpty_Cryptor_encrypt
在個別下劃線的前邊加了數(shù)字1,目前我也不知道為何廉侧,所以遇到這種包名页响,建議使用命令行生成
3.創(chuàng)建jni目錄(eclipse工程是直接在項目根目錄創(chuàng)建,將頭文件復(fù)制進去段誊,然后編寫c文件闰蚕,將對應(yīng)的方法名復(fù)制進去,引入需要的頭文件)
4.添加native支持(注意如果此時连舍,jni下尚且沒有c或者cpp文件add native support這個選項不會出現(xiàn)没陡,系統(tǒng)必須在檢測到j(luò)ni下的c/cpp文件之后才會有這個選項,所以索赏,先編寫出這個文件)(右鍵工程如圖盼玄,點擊add native support后彈出對話框(創(chuàng)建出頭文件之后),設(shè)置c文件名稱潜腻,o確定即可,然后會自動給你生成Android.mk文件埃儿,c文件,然后創(chuàng)建Application.mk文件)
5.確認(rèn)你的eclipse已經(jīng)配置了ndk融涣,注意童番,ndk的版本會有可能發(fā)生問題,這樣看你的eclipse最多能支持多大版本的ndk了
如果ndk版本過高威鹿,會導(dǎo)致你配置目錄之后仍然提示你有問題剃斧,如圖not a valid NDK directory,所以需要降低版本,我設(shè)置了r10之后可以了
6.添加ndk相關(guān)頭文件的支持(右鍵項目打開properties忽你,發(fā)現(xiàn)里邊由C/C++General選項幼东,這兩個選項是你添加了native surpport之后才有的)
在右邊的add選項,添加這三個目錄進去(一定要是你ndk中存在的目錄,不同版本有不同)
D:\application\java\android-ndk-r10e\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.8\include
D:\application\java\android-ndk-r10e\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.8\include-fixed
D:\application\java\android-ndk-r10e\platforms\android-18\arch-arm\usr\include
7.補充c文件的方法實現(xiàn)
#include <jni.h>
#include <com_example_ndk_file_encrpty_Cryptor.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char password[] = "renzhenming";
//加密
JNIEXPORT void JNICALL Java_com_example_ndk_1file_1encrpty_Cryptor_encrypt(
JNIEnv *env, jclass jcls, jstring normal_path_jstr,jstring crypt_path_jstr) {
//jstring -> char*
const char* normal_path = (*env)->GetStringUTFChars(env,normal_path_jstr,JNI_FALSE);
const char* crypt_path = (*env)->GetStringUTFChars(env,crypt_path_jstr,JNI_FALSE);
//打開文件
FILE *normal_fp = fopen(normal_path, "rb");
FILE *crypt_fp = fopen(crypt_path, "wb");
//一次讀取一個字符
int ch;
int i = 0; //循環(huán)使用密碼中的字母進行異或運算
int pwd_len = strlen(password); //密碼的長度
while ((ch = fgetc(normal_fp)) != EOF) { //End of File
//寫入(異或運算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//關(guān)閉
fclose(crypt_fp);
fclose(normal_fp);
}
//解密
JNIEXPORT void JNICALL Java_com_example_ndk_1file_1encrpty_Cryptor_decrypt(
JNIEnv * env, jclass jcls, jstring crypt_path_jstr, jstring decrypt_path_jstr) {
const char* crypt_path = (*env)->GetStringUTFChars(env,crypt_path_jstr,JNI_FALSE);
const char* decrypt_path = (*env)->GetStringUTFChars(env,decrypt_path_jstr,JNI_FALSE);
//打開文件
FILE *normal_fp = fopen(crypt_path, "rb");
FILE *crypt_fp = fopen(decrypt_path, "wb");
//一次讀取一個字符
int ch;
int i = 0; //循環(huán)使用密碼中的字母進行異或運算
int pwd_len = strlen(password); //密碼的長度
while ((ch = fgetc(normal_fp)) != EOF) { //End of File
//寫入(異或運算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//關(guān)閉
fclose(crypt_fp);
fclose(normal_fp);
}