1. 字段描述符
JNI字段描述符是一种对函数返回值和参数的编码。这种编码叫做JNI字段描述符(JavaNative Interface FieldDescriptors)。
JNI方法描述符,主要就是在括号里放置参数,在括号后面放置返回类型,如下:
(参数描述符)返回类型
- 一个数组int[],就需要表示为这样"[I"。
- 如果多个数组double[][][]就需要表示为这样 “[[[D”。也就是说每一个方括号开始,就表示一个数组维数。多个方框后面,就是数组的类型。
- 如果以一个L开头的描述符,就是类描述符,它后紧跟着类的字符串,然后分号“;”结束。比如"Ljava/lang/String;"就是表示类型String;
- "[Ljava/lang/Object;"就是表示Object[]。
- 不需要返回参数类型时,使用”V”来表示。
"([B)V"就是表示void String(byte[] bytes); - "()Ljava/lang/String;"表示String f();
- "(ILjava/lang/Class;)J"表示long f(int i, Class c);
Java 类型 | 符号 |
---|---|
Boolean | Z |
Byte | B |
Char | C |
Short | S |
Int | I |
Long | J |
Float | F |
Double | D |
Void | V |
objects对象 | 以"L"开头,以";“结尾,中间是用”/" 隔开的包及类名。比如:Ljava/lang/String;如果是嵌套类,则用 来 表 示 嵌 套 。 例 如 " ( L j a v a / l a n g / S t r i n g ; L a n d r o i d / o s / F i l e U t i l s 来表示嵌套。例如 "(Ljava/lang/String;Landroid/os/FileUtils 来表示嵌套。例如"(Ljava/lang/String;Landroid/os/FileUtilsFileStatus;)Z" |
2. 常用方法
2.1 获取Application上下文
jobject getApplication(JNIEnv *env) {
jobject application = NULL;
jclass activity_thread_clz = env->FindClass("android/app/ActivityThread");
if (activity_thread_clz != NULL) {
jmethodID get_Application = env->GetStaticMethodID(activity_thread_clz,
"currentActivityThread",
"()Landroid/app/ActivityThread;");
if (get_Application != NULL) {
jobject currentActivityThread = env->CallStaticObjectMethod(activity_thread_clz,
get_Application);
jmethodID getal = env->GetMethodID(activity_thread_clz, "getApplication",
"()Landroid/app/Application;");
application = env->CallObjectMethod(currentActivityThread, getal);
}
return application;
}
return application;
}
2.2 获取包名
jstring getPackageName(JNIEnv *env) {
jobject context = getApplication(env);
if (context == NULL) {
LOGD("context is null!");
return NULL;
}
jclass activity = env->GetObjectClass(context);
jmethodID methodId_pack = env->GetMethodID(activity, "getPackageName", "()Ljava/lang/String;");
jstring name_str = static_cast<jstring >( env->CallObjectMethod(context, methodId_pack));
return name_str;
}
2.3 获取签名
char *getSha1(JNIEnv *env, jobject context_object) {
//上下文对象
jclass context_class = env->GetObjectClass(context_object);
//反射获取PackageManager
jmethodID methodId = env->GetMethodID(context_class, "getPackageManager",
"()Landroid/content/pm/PackageManager;");
jobject package_manager = env->CallObjectMethod(context_object, methodId);
if (package_manager == NULL) {
LOGD("package_manager is NULL!!!");
return NULL;
}
//反射获取包名
methodId = env->GetMethodID(context_class, "getPackageName", "()Ljava/lang/String;");
jstring package_name = (jstring) env->CallObjectMethod(context_object, methodId);
if (package_name == NULL) {
LOGD("package_name is NULL!!!");
return NULL;
}
env->DeleteLocalRef(context_class);
//获取PackageInfo对象
jclass pack_manager_class = env->GetObjectClass(package_manager);
methodId = env->GetMethodID(pack_manager_class, "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
env->DeleteLocalRef(pack_manager_class);
jobject package_info = env->CallObjectMethod(package_manager, methodId, package_name, 0x40);
if (package_info == NULL) {
LOGD("getPackageInfo() is NULL!!!");
return NULL;
}
env->DeleteLocalRef(package_manager);
//获取签名信息
jclass package_info_class = env->GetObjectClass(package_info);
jfieldID fieldId = env->GetFieldID(package_info_class, "signatures",
"[Landroid/content/pm/Signature;");
env->DeleteLocalRef(package_info_class);
jobjectArray signature_object_array = (jobjectArray) env->GetObjectField(package_info, fieldId);
if (signature_object_array == NULL) {
LOGD("signature is NULL!!!");
return NULL;
}
jobject signature_object = env->GetObjectArrayElement(signature_object_array, 0);
env->DeleteLocalRef(package_info);
//签名信息转换成sha1值
jclass signature_class = env->GetObjectClass(signature_object);
methodId = env->GetMethodID(signature_class, "toByteArray", "()[B");
env->DeleteLocalRef(signature_class);
jbyteArray signature_byte = (jbyteArray) env->CallObjectMethod(signature_object, methodId);
jclass byte_array_input_class = env->FindClass("java/io/ByteArrayInputStream");
methodId = env->GetMethodID(byte_array_input_class, "<init>", "([B)V");
jobject byte_array_input = env->NewObject(byte_array_input_class, methodId, signature_byte);
jclass certificate_factory_class = env->FindClass("java/security/cert/CertificateFactory");
methodId = env->GetStaticMethodID(certificate_factory_class, "getInstance",
"(Ljava/lang/String;)Ljava/security/cert/CertificateFactory;");
jstring x_509_jstring = env->NewStringUTF("X.509");
jobject cert_factory = env->CallStaticObjectMethod(certificate_factory_class, methodId,
x_509_jstring);
methodId = env->GetMethodID(certificate_factory_class, "generateCertificate",
("(Ljava/io/InputStream;)Ljava/security/cert/Certificate;"));
jobject x509_cert = env->CallObjectMethod(cert_factory, methodId, byte_array_input);
env->DeleteLocalRef(certificate_factory_class);
jclass x509_cert_class = env->GetObjectClass(x509_cert);
methodId = env->GetMethodID(x509_cert_class, "getEncoded", "()[B");
jbyteArray cert_byte = (jbyteArray) env->CallObjectMethod(x509_cert, methodId);
env->DeleteLocalRef(x509_cert_class);
jclass message_digest_class = env->FindClass("java/security/MessageDigest");
methodId = env->GetStaticMethodID(message_digest_class, "getInstance",
"(Ljava/lang/String;)Ljava/security/MessageDigest;");
jstring sha1_jstring = env->NewStringUTF("SHA1");
jobject sha1_digest = env->CallStaticObjectMethod(message_digest_class, methodId, sha1_jstring);
methodId = env->GetMethodID(message_digest_class, "digest", "([B)[B");
jbyteArray sha1_byte = (jbyteArray) env->CallObjectMethod(sha1_digest, methodId, cert_byte);
env->DeleteLocalRef(message_digest_class);
//转换成char
jsize array_size = env->GetArrayLength(sha1_byte);
jbyte *sha1 = env->GetByteArrayElements(sha1_byte, NULL);
char *hex_sha = new char[array_size * 2 + 1];
for (int i = 0; i < array_size; ++i) {
hex_sha[2 * i] = hexcode[((unsigned char) sha1[i]) / 16];
hex_sha[2 * i + 1] = hexcode[((unsigned char) sha1[i]) % 16];
}
hex_sha[array_size * 2] = '\0';
LOGD("hex_sha %s ", hex_sha);
return hex_sha;
}
2.4 获取meta-data
jstring getMetaData(JNIEnv *env, jobject context_object, jstring name) {
jstring package_name = getPackageName(env);
jclass context_class = env->GetObjectClass(context_object);
jmethodID methodId = env->GetMethodID(context_class, "getPackageManager",
"()Landroid/content/pm/PackageManager;");
jobject package_manager = env->CallObjectMethod(context_object, methodId);
jclass package_manager_class = env->GetObjectClass(package_manager);
methodId = env->GetMethodID(package_manager_class, "getApplicationInfo",
"(Ljava/lang/String;I)Landroid/content/pm/ApplicationInfo;");
jfieldID fieldId = env->GetStaticFieldID(package_manager_class, "GET_META_DATA", "I");
jint flags = env->GetStaticIntField(package_manager_class, fieldId);
jobject application_info = env->CallObjectMethod(package_manager, methodId, package_name,
flags);
jclass application_info_class = env->GetObjectClass(application_info);
fieldId = env->GetFieldID(application_info_class, "metaData", "Landroid/os/Bundle;");
jobject metaData = env->GetObjectField(application_info, fieldId);
if (metaData == NULL) {
LOGE(name + "不能为空,请在Manifest中配置metadata!");
return env->NewStringUTF("");
}
jclass bundle_class = env->GetObjectClass(metaData);
methodId = env->GetMethodID(bundle_class, "getString",
"(Ljava/lang/String;)Ljava/lang/String;");
jstring value = (jstring) env->CallObjectMethod(metaData, methodId, name);
if (value == NULL) {
LOGE(name + "不能为空,请在Manifest中配置metadata!");
return env->NewStringUTF("");
}
return value;
}