Access resources from the module itself

Search This thread

vin-ivar

Senior Member
Mar 4, 2011
129
58
Pune
Okay, so I have some resources in my module/app. Say, a Drawable. Unfortunately, when I try to use them (to create a UI, say) from within handleLoadPackage, well, I can't. I can access R.drawable.abc just fine; but for some reason, all I get is a strange white rectangle instead of the actual Drawable.

How do I work around this issue? Thank you! :)
 
  • Like
Reactions: Lawiusz

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,170
8,805
This is because your hook is not really part of your app - it runs in the hooked app. To access your own resources, you need to create a context for your app first.

You're going to need a general context first. If you can't get any from the class you're hooking, you can try AndroidAppHelper.currentApplication(). Then, use that context to create a context for your own app (see Context.createContext(...)). You can then access your resources using that context.
 

vin-ivar

Senior Member
Mar 4, 2011
129
58
Pune
This is because your hook is not really part of your app - it runs in the hooked app. To access your own resources, you need to create a context for your app first.

You're going to need a general context first. If you can't get any from the class you're hooking, you can try AndroidAppHelper.currentApplication(). Then, use that context to create a context for your own app (see Context.createContext(...)). You can then access your resources using that context.

I tried this:

Code:
Context c = tv.getContext().createPackageContext("package.name", Context.CONTEXT_IGNORE_SECURITY);
int resId = c.getResources().getIdentifier("ic_launcher", "drawable", "package.name");
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);

No dice. I'm sure using c.getResources.getDrawable would work, but my dialog method requires the ID, not the Drawable itself.
 

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,433
I tried this:

Code:
Context c = tv.getContext().createPackageContext("package.name", Context.CONTEXT_IGNORE_SECURITY);
int resId = c.getResources().getIdentifier("ic_launcher", "drawable", "package.name");
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);

No dice. I'm sure using c.getResources.getDrawable would work, but my dialog method requires the ID, not the Drawable itself.

Yeah, but that resource ID will always be resolved using the target app's resources. If you can't use the the drawable directly, you can try this:
Code:
private static String MODULE_PATH = null;
private int mFakeId = 0;

public void initZygote(StartupParam startupParam) throws Throwable {
	MODULE_PATH = startupParam.modulePath;
}

public void handleInitPackageResources(InitPackageResourcesParam resparam) throws Throwable {
	if (!resparam.packageName.equals("your.target.app"))
		return;

	XModuleResources modRes = XModuleResources.createInstance(MODULE_PATH, resparam.res);
	mFakeId = resparam.res.addResource(modRes, R.drawable.ic_launcher);
}

Then use mFakeId in your callback, which should run after the resources have been initialized. addResource() generates an ID for you an sets up a resource replacement for that non-existent ID to the item in your own resources.
 

vin-ivar

Senior Member
Mar 4, 2011
129
58
Pune
Yeah, but that resource ID will always be resolved using the target app's resources. If you can't use the the drawable directly, you can try this:
Code:
private static String MODULE_PATH = null;
private int mFakeId = 0;

public void initZygote(StartupParam startupParam) throws Throwable {
	MODULE_PATH = startupParam.modulePath;
}

public void handleInitPackageResources(InitPackageResourcesParam resparam) throws Throwable {
	if (!resparam.packageName.equals("your.target.app"))
		return;

	XModuleResources modRes = XModuleResources.createInstance(MODULE_PATH, resparam.res);
	mFakeId = resparam.res.addResource(modRes, R.drawable.ic_launcher);
}

Then use mFakeId in your callback, which should run after the resources have been initialized. addResource() generates an ID for you an sets up a resource replacement for that non-existent ID to the item in your own resources.

This works, thank you so much! Bit stupid of me, I should have inferred this from the tutorial.
 

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,170
8,805
I tried this:

Code:
Context c = tv.getContext().createPackageContext("package.name", Context.CONTEXT_IGNORE_SECURITY);
int resId = c.getResources().getIdentifier("ic_launcher", "drawable", "package.name");
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);

No dice. I'm sure using c.getResources.getDrawable would work, but my dialog method requires the ID, not the Drawable itself.
To add to what rovo said, you could've use getDrawable(…) instead of getIdentifier(…) here, which would've given you Drawable (that you can then use) instead of a resource ID that won't work in the hooked app.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 5
    I tried this:

    Code:
    Context c = tv.getContext().createPackageContext("package.name", Context.CONTEXT_IGNORE_SECURITY);
    int resId = c.getResources().getIdentifier("ic_launcher", "drawable", "package.name");
    dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);

    No dice. I'm sure using c.getResources.getDrawable would work, but my dialog method requires the ID, not the Drawable itself.

    Yeah, but that resource ID will always be resolved using the target app's resources. If you can't use the the drawable directly, you can try this:
    Code:
    private static String MODULE_PATH = null;
    private int mFakeId = 0;
    
    public void initZygote(StartupParam startupParam) throws Throwable {
    	MODULE_PATH = startupParam.modulePath;
    }
    
    public void handleInitPackageResources(InitPackageResourcesParam resparam) throws Throwable {
    	if (!resparam.packageName.equals("your.target.app"))
    		return;
    
    	XModuleResources modRes = XModuleResources.createInstance(MODULE_PATH, resparam.res);
    	mFakeId = resparam.res.addResource(modRes, R.drawable.ic_launcher);
    }

    Then use mFakeId in your callback, which should run after the resources have been initialized. addResource() generates an ID for you an sets up a resource replacement for that non-existent ID to the item in your own resources.
    1
    Okay, so I have some resources in my module/app. Say, a Drawable. Unfortunately, when I try to use them (to create a UI, say) from within handleLoadPackage, well, I can't. I can access R.drawable.abc just fine; but for some reason, all I get is a strange white rectangle instead of the actual Drawable.

    How do I work around this issue? Thank you! :)