JNI 动态注册

java代码:

//Application类,壳入口
public class MainApplication extends Application {
    
    public native void Decrypt(Context context);
    public native Object DymLoad(Context context);
    
    static{
        System.loadLibrary("AndriodShell");
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        
        //调用JNI,解密DEX
        Decrypt(base);
        
        //调用JNI,动态加载DEX
        DymLoad(base);
    }
}

JNI代码:

/***************************************************************************************
*
* 定义一个全局数组,保存Java到JNI的映射关系
*
****************************************************************************************/
// Java和JNI绑定表(JIN动态注册)
static JNINativeMethod method_table[] = {
            { "Decrypt", "(Landroid/content/Context;)V", (void*) sub_1111 },
            { "DymLoad","(Landroid/content/Context;)Ljava/lang/Object;", (void*) sub_2222 }
        };


/***************************************************************************************
*
* 函数实现
*
****************************************************************************************/
//动态库入口
JNIEXPORT
jint
JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
    {
        return result;
    }

    //动态注册JNI
    register_ndk_load(env);

    return JNI_VERSION_1_4;
}


//解密dex
JNIEXPORT
void
JNICALL
sub_1111(JNIEnv* env, jobject thiz, jobject context)
{
    
}


//动态加载DEX
JNIEXPORT
jobject
JNICALL
sub_2222(JNIEnv* env, jobject thiz, jobject context)
{
    return get_get_obj;
}


//动态注册JNI
int
register_ndk_load(JNIEnv *env)
{
    //动态注册JNI
    return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table));
}


//动态注册JNI
static
int
registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;
    clazz = env->FindClass(className);
    if (clazz == NULL)
    {
        return JNI_FALSE;
    }

    //调用JNIEnv函数,动态注册JNI
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0)
    {
        return JNI_FALSE;
    }

    return JNI_TRUE;
}

 

posted @ 2015-05-03 18:01  luzhiyuan  阅读(...)  评论(... 编辑 收藏