Search code examples
javaandroidc++capktool

Injecting class into the JNIEnv in android jni


C++ code:

extern "C" JNIEXPORT void JNICALL
Java_com_example_afl_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    
    // env->DefineClass(...)
}

I'm calling the above function from Java side code:

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        stringFromJNI();  // call cpp function


    }

    public native String stringFromJNI();
}

My question is about the env->DefineClass(...) in cpp code.
As you see the android VM passes JNIEnv *env to my native function, so by using env pointer i have access to all of my java classes and i can use them (i have access to all of my java side classes and i can create instance object and do everything).
But how can access to a class which is in another apk and it is in another package name ?
I wanna decompile the target apk and copy that class and inject that class to my env using the env->DefineClass function but i don't know how can i complete this task.
Thanks for any reply :)


Solution

  • Impossible. Android does not implement DefineClass:

    All JNI 1.6 features are supported, with the following exception:
    DefineClass is not implemented. Android does not use Java bytecodes or class files, so passing in binary class data doesn't work.

    Even if that worked, your app's user very probably does not have access rights to another applications .apk.