java 层
native 层
key.cpp
#include
#include "key.h"
#include
#define LOG_TAG "JNI_LOG"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
jstring Java_com_huben_key_KeyUtils_getKeyFromJni(JNIEnv *env, jobject clazz, jobject obj)
{
//char *str = "key from jni";
//return env->NewStringUTF(str);
return appenString(env, getPackname(env, clazz, obj), getSignature(env, clazz, obj));
}
jstring getPackname(JNIEnv *env, jobject clazz, jobject obj)
{
jclass native_class = env->GetObjectClass(obj);
jmethodID mId = env->GetMethodID(native_class, "getPackageName", "()Ljava/lang/String;");
jstring packName = static_cast(env->CallObjectMethod(obj, mId));
return packName;
}
jstring getSignature(JNIEnv *env, jobject clazz, jobject obj)
{
jclass native_class = env->GetObjectClass(obj);
jmethodID pm_id = env->GetMethodID(native_class, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject pm_obj = env->CallObjectMethod(obj, pm_id);
jclass pm_clazz = env->GetObjectClass(pm_obj);
// 得到 getPackageInfo 方法的 ID
jmethodID package_info_id = env->GetMethodID(pm_clazz, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;"); jstring pkg_str = getPackname(env, clazz, obj);
// 获得应用包的信息
jobject pi_obj = env->CallObjectMethod(pm_obj, package_info_id, pkg_str, 64);
// 获得 PackageInfo 类
jclass pi_clazz = env->GetObjectClass(pi_obj);
// 获得签名数组属性的 ID
jfieldID signatures_fieldId = env->GetFieldID(pi_clazz, "signatures", "[Landroid/content/pm/Signature;");
jobject signatures_obj = env->GetObjectField(pi_obj, signatures_fieldId);
jobjectArray signaturesArray = (jobjectArray)signatures_obj;
jsize size = env->GetArrayLength(signaturesArray);
jobject signature_obj = env->GetObjectArrayElement(signaturesArray, 0);
jclass signature_clazz = env->GetObjectClass(signature_obj);
jmethodID string_id = env->GetMethodID(signature_clazz, "toCharsString", "()Ljava/lang/String;");
jstring str = static_cast(env->CallObjectMethod(signature_obj, string_id));
char *c_msg = (char*)env->GetStringUTFChars(str,0);
LOGI("signsture: %s", c_msg);
return str;
}
jstring appenString(JNIEnv* env, jstring s1, jstring s2)
{ const char *s1x = (env)->GetStringUTFChars(s1, NULL);
const char *s2x = (env)->GetStringUTFChars(s2, NULL);
char *sall = new char[strlen(s1x) + strlen(s2x) + 1];
strcpy(sall, s1x);
strcat(sall, s2x);
jstring retval = (env)->NewStringUTF(sall);
(env)->ReleaseStringUTFChars(s1, s1x);
(env)->ReleaseStringUTFChars(s2, s2x);
free(sall);
return retval;
}
key.h
#include #include
#ifdef __cplusplusextern "C"
{
jstring Java_com_huben_key_KeyUtils_getKeyFromJni(JNIEnv *, jobject, jobject);
jstring getPackname(JNIEnv *, jobject, jobject);
jstring getSignature(JNIEnv *, jobject, jobject);
jstring appenString(JNIEnv *, jstring, jstring);}
#endif
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := key
LOCAL_SRC_FILES := key.cpp key.h
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := stlport_static
APP_ABI := all
build.gradle
defaultConfig下面
ndk {
moduleName "Key"
ldLibs "log"
abiFilters "armeabi", "armeabi-v7a", "x86"
}