在jni中获取应用程序签名哈希码

时间:2015-11-18 22:00:42

标签: android java-native-interface native

我希望使用以下代码获取原生应用签名:(source

// For class Context
    jclass native_clazz = (*env)->GetObjectClass(env, context);

    // Get the getPackageManager method ID
    jmethodID methodID_func = (*env)->GetMethodID(env, native_clazz,
            "getPackageManager", "()Landroid/content/pm/PackageManager;");

    // Access manager application package
    jobject package_manager = (*env)->CallObjectMethod(env, thiz, methodID_func);

    // For class PackageManager
    jclass pm_clazz = (*env)->GetObjectClass(env, package_manager);

    // Get the getPackageInfo method ID
    jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz,
            "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");

    // To obtain the application package information
    jobject package_info = (*env)->CallObjectMethod(env, package_manager,
            methodID_pm, (*env)->NewStringUTF(env, "com.example.hellojni"), 64);

但我得到了这个:

error: 'context' undeclared (first use in this function)

我如何获得原生语境? 我如何将签名哈希码作为字符串返回?

1 个答案:

答案 0 :(得分:0)

您可以在参数中将上下文发送到JNI,例如

jstring Java_packagename_stringFromJNI( JNIEnv* env, jobject thiz , jobject context)

当您在活动中调用该方法时:

public native String stringFromJNI(Context context);

...

Log.d(TAG, "Receive: " + stringFromJNI(this));