jeudi 9 septembre 2021

Android create Toast using class reflection?

I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:

import andhook.lib.HookHelper;

import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;

public class AndHookConfig {
    @HookHelper.Hook(clazz = BitmapFactory.class)
    private static Bitmap decodeFile(BitmapFactory thiz, String path) {
        Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
        return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
    }
}

I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire