Search code examples
javaandroidjava-native-interfaceoverridingnative

Full Native Implementation


I am designing an application through JNI in order to prevent third party edits. I have moved forth towards registering native methods in order to remove jni bridge linkages, but as you can see, methods with override attribute still need to exist within the java code as native linked. Is there a way to fully port the remaining java code for this specific file?

Java:

@Override
protected native void onCreate(Bundle savedInstanceState);

CPP:

void onCreate(JNIEnv *env, jobject classObject, jobject bundle) {
    /**super.onCreate**/
    gObjects[0] = env->NewGlobalRef(classObject);
    gClasses[0] = env->GetObjectClass(gObjects[0]);
    jclass s_Class = env->GetSuperclass(gClasses[0]);
    jmethodID oc_ID = env->GetMethodID(s_Class, "onCreate", "(Landroid/os/Bundle;)V");
    env->CallNonvirtualVoidMethod(gObjects[0], gClasses[0], oc_ID, bundle);
}

extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *curVM_, void *reserved) {
    curVM = curVM_;
    curVM->GetEnv((void **) &environments[0], JNI_VERSION_1_6);

    /**Start of Native Method Registers**/
    JNINativeMethod natives[1];
    uint64_t pCount = sizeof(natives) / sizeof(natives[0]);
    jclass klass = environments[0]->FindClass("com/company/app/Activity");
    natives[0] = {"onCreate", "(Landroid/os/Bundle;)V", (void *)onCreate};
    environments[0]->RegisterNatives(klass , natives, pCount);\
    for (uint64_t i = 0; i < pCount; i++) natives[i] = {"", ""};\

    return JNI_VERSION_1_6;
}

Solution

  • It does seem quite silly to think I can simply remove the native linkage between the activity and lib. Instead, I will be utilizing BaseDexClassLoader to sideload my dex file during runtime. I will not be giving up my code, but further information about this can be founded at: https://developer.android.com/reference/dalvik/system/BaseDexClassLoader