java层传入app的Context,ndk内检查包名和签名特征

java native 方法

1
public static native String getSignIfo(Context context);

c语言调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
JNIEXPORT jstring JNICALL
Java_com_example_hehe_utils_StringUtils_getSignIfo(JNIEnv *env, jclass type, jobject obj) {


// 获得Context类
jclass cls = (*env)->GetObjectClass(env, obj);
// 得到getPackageManager方法的ID
jmethodID mid = (*env)->GetMethodID(env, cls, "getPackageManager", "()Landroid/content/pm/PackageManager;");

// 获得应用包的管理器
jobject pm = (*env)->CallObjectMethod(env, obj, mid);

// 得到getPackageName方法的ID
mid = (*env)->GetMethodID(env, cls, "getPackageName", "()Ljava/lang/String;");
// 获得当前应用包名
jstring packageName = (jstring)(*env)->CallObjectMethod(env, obj, mid);

// printf("%s",packageName);
// const jbyte *str = (*env)->GetStringUTFChars(env, packageName, NULL);
// __android_log_print(ANDROID_LOG_INFO, "JNITag","string From Java To C : %s", str);

// 获得PackageManager类
cls = (*env)->GetObjectClass(env, pm);
// 得到getPackageInfo方法的ID
mid = (*env)->GetMethodID(env, cls, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// 获得应用包的信息
jobject packageInfo = (*env)->CallObjectMethod(env, pm, mid, packageName, 0x40); //GET_SIGNATURES = 64;
// 获得PackageInfo 类
cls = (*env)->GetObjectClass(env, packageInfo);
// 获得签名数组属性的ID
jfieldID fid = (*env)->GetFieldID(env, cls, "signatures", "[Landroid/content/pm/Signature;");
// 得到签名数组
jobjectArray signatures = (jobjectArray)(*env)->GetObjectField(env, packageInfo, fid);
// 得到签名
jobject sign = (*env)->GetObjectArrayElement(env, signatures, 0);

// 获得Signature类
cls = (*env)->GetObjectClass(env, sign);
// 得到toCharsString方法的ID
mid = (*env)->GetMethodID(env, cls, "toCharsString", "()Ljava/lang/String;");


jstring signString=(jstring)(*env)->CallObjectMethod(env, sign, mid);
// const jbyte *singn = (*env)->GetStringUTFChars(env, signString, NULL);
// __android_log_print(ANDROID_LOG_INFO, "JNITag","string From Java To C : %s", singn);



return packageName;
}

参考链接