public Drawable loadApkResources(String apkPath) {
try {
//获取apk的资源 最终都要通过AssetManager 获取, getAssets() 获取的AssetManager是获取的本身Apk的
//获取其他Apk的资源需要实例化一个AssetManager,并把该AssetManager的加载路径修改为被 加载的Apk的路径
AssetManager assetMAnager = AssetManager.class.newInstance();
AssetManager.class.getDeclaredMethod("addAssetPath", String.class).invoke(assetMAnager, apkPath);
Resources resources = new Resources(assetMAnager, getResources().getDisplayMetrics(), getResources().getConfiguration());
Drawable drawable = resources.getDrawable(resources.getIdentifier("abc", "drawable", "com.mz.testapk"));
return drawable;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
被加载的Apk的包名为com.mz.testapk ,drawable下有一张名为abc.png图片