jni方法签名以及注意事项以及实战ShowToast

方法签名参考地址
oracle地址 文档点击跳转

Java VM Type Signatures
Type Signature  Java Type
Z   boolean
B   byte
C   char
S   short
I   int
J   long
F   float
D   double
L 完整类名 ;    完整类名包名之间用/ 而分割不是.
[ type  type[]
( arg-types ) ret-type  method type
For example, the Java method:

long f (int n, String s, int[] arr);
has the following type signature:

(ILjava/lang/String;[I)J

关于是否需要加分号的问题上面已经说明了,如果遇到全类型签名就应该叫;
而且 不管多少个都需要记上,否则也会报错,这里如果把最后那个字符串的;后去掉也会报错

static JNINativeMethod gMethods[] = {
        {"a7",  "([I)Ljava/lang/String;", (void *) b_1},
        {"dd",  "(Landroid/content/Context;Ljava/lang/String;)V", (void *) showD},
};

基本类型直接连开,而对象则需要分开分割.

摘取c++代码演示

   env->DeleteLocalRef(context_class);
    jclass pack_manager_class = env->GetObjectClass(package_manager_object);
    methodId = env->GetMethodID(pack_manager_class, "getPackageInfo",
                                "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
    env->DeleteLocalRef(pack_manager_class);
    jobject package_info_object = env->CallObjectMethod(package_manager_object, methodId,
                                                        package_name_string, 64);
    if (package_info_object == NULL) {
        LOGI("getPackageInfo() Failed!");
        return NULL;
    }

Toast JNI实现
java代码

    public static native void showToast(Context context, String content);

C++代码


extern "C"
JNIEXPORT void JNICALL
Java_cn_qssq666_redpacket_qq694886526_JNIPro_showToast(JNIEnv *env, jclass type,
                                                      jobject context, jstring str) {
    jclass tclss = env->FindClass("android/widget/Toast");
    jmethodID mid = env->GetStaticMethodID(tclss, "makeText",
                                           "(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;");
    jobject job = env->CallStaticObjectMethod(tclss, mid, context, str);
    jmethodID showId = env->GetMethodID(tclss, "show", "()V");
    env->CallVoidMethod(job, showId, context, str);
}

我本人因为无奈不得不把能转换为c代码的东西全部转换为c代码提高一些难度,总的来说也是无奈,java的逆向太简单了,傻瓜式 手机端 一大把。

你可能感兴趣的:(jni方法签名以及注意事项以及实战ShowToast)