Skip to content

Instantly share code, notes, and snippets.

@ccentauri
Forked from wujinyuan/com_example_gist_SignatureChecker.cpp
Last active November 16, 2021 13:05
Show Gist options
  • Save ccentauri/2a39aa00d02e4ccd51762a4637234980 to your computer and use it in GitHub Desktop.
Save ccentauri/2a39aa00d02e4ccd51762a4637234980 to your computer and use it in GitHub Desktop.
Get app package signature without JNICALL
jobject getContext(JNIEnv *env) {
jclass jAppAppGlobalsClass = env->FindClass("android/app/AppGlobals");
jmethodID jGetInitialApplication = env->GetStaticMethodID(jAppAppGlobalsClass, "getInitialApplication",
"()Landroid/app/Application;");
jobject jApplicationObject = env->CallStaticObjectMethod(jAppAppGlobalsClass, jGetInitialApplication);
return jApplicationObject;
}
/**
*
* @param env - JNIEnv instance, shared to the whole application
* @return
*/
const char *getSignature(JNIEnv *env) {
jobject thiz = getContext(env);
// Context
jclass native_context = env->FindClass("android/content/Context");
// Context#getPackageManager()
jmethodID methodID_func = env->GetMethodID(native_context, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject package_manager = env->CallObjectMethod(thiz, methodID_func);
jclass pm_clazz = env->GetObjectClass(package_manager);
// PackageManager#getPackageInfo()
jmethodID methodId_pm = env->GetMethodID(pm_clazz, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// Context#getPackageName()
jmethodID methodID_packagename = env->GetMethodID(native_context, "getPackageName", "()Ljava/lang/String;");
jstring name_str = static_cast<jstring>(env->CallObjectMethod(thiz, methodID_packagename));
jobject package_info = env->CallObjectMethod(package_manager, methodId_pm, name_str, 64);
jclass pi_clazz = env->GetObjectClass(package_info);
// PackageInfo#signatures
jfieldID fieldID_signatures = env->GetFieldID(pi_clazz, "signatures", "[Landroid/content/pm/Signature;");
jobject signatur = env->GetObjectField(package_info, fieldID_signatures);
jobjectArray signatures = reinterpret_cast<jobjectArray>(signatur);
// PackageInfo#signatures[0]
jobject signature = env->GetObjectArrayElement(signatures, 0);
jclass s_clazz = env->GetObjectClass(signature);
// PackageInfo#signatures[0].toCharString()
jmethodID methodId_ts = env->GetMethodID(s_clazz, "toCharsString", "()Ljava/lang/String;");
jstring ts = (jstring) env->CallObjectMethod(signature, methodId_ts);
return env->GetStringUTFChars(ts, 0);
}
@caobug
Copy link

caobug commented Mar 25, 2019

hello, the static_cast and reinterpret_cast not working in c. how to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment