Android黑科技动态加载(三)之动态加载资源

Android黑科技动态加载(三)之动态加载资源

2023年7月7日发(作者:)

Android⿊科技动态加载(三)之动态加载资源⽬录Android⿊科技动态加载(⼀)之Java中的ClassLoaderAndroid⿊科技动态加载(⼆)之Android中的ClassLoaderAndroid⿊科技动态加载(三)之动态加载资源Android⿊科技动态加载(四)之插件化开发我们的认识我们都知道, 在Android中我们获取⼀个资源只需要使⽤ource().getXXXX()就可以获取到对应的资源⽂件. 那么如果我们想要加载其他应⽤的res内容, 那么就应该构造出他们环境的Resource. 有了Resource还不⾏, 我们还需要获取资源⽂件的ID, 其中ID我们可以通过⽂件通过反射获取.所以我们的⽬标就是(分别对于已安装的应⽤和未安装的应⽤):构造出Resource获取资源的IDResourceBundle就是我们的资源包, 其中只有两张图⽚android_resource_bundle_roid_resource_bundle_roid_resource_bundle_已经安装的应⽤获取Resource对于已经安装的应⽤, 获取Resource的⽅法很简单, 只要获取到Context就可以获取对应环境下的Resource了, 其中有⼀个⽅法PackageContext(String packageName, int flags)可以根据包名获取已经安装应⽤的Context.⾸先我们建⼀个Bean来存储已经加载的资源public class LoadedResource { public Resources resources; public String packageName; public ClassLoader classLoader;}然后我们就可以写加载的⽅法/** * 获取已安装应⽤资源 *

* @param packageName */public LoadedResource getInstalledResource(String packageName) { LoadedResource resource = (packageName); // 先从缓存中取, 没有就去加载 if (resource == null) { try { Context context = PackageContext(packageName, T_INCLUDE_CODE | T_IGNORE_SECURITY); resource = new LoadedResource(); eName = packageName; ces = ources(); oader = ssLoader(); (packageName, resource); // 得到结果缓存起来 } catch (Exception e) { tackTrace(); } } return resource;}⾄此, 我们就能获取到了Resource获取资源ID根据上⾯的思路, 我们使⽤反射区获取. ⼤概看⼀下R⽂件的结构package cebundle;public final class R { public static final class attr { } public static final class drawable { public static final int image=0x7f020000; public static final int image1=0x7f020001; } public static final class mipmap { public static final int ic_launcher=0x7f030000; } public static final class string { public static final int app_name=0x7f040000; } ...}对应的资源类型都有⼀个静态内部类, 那么我们就可以使⽤反射⽆获取对应的数值/** * 获取资源ID *

* @param packageName 包名 * @param type 对应的资源类型, drawable mipmap等 * @param fieldName * @return */public int getResourceID(String packageName, String type, String fieldName) { int resID = 0; LoadedResource installedResource = getInstalledResource(packageName); // 获取已安装APK的资源 if (installedResource != null) { String rClassName = packageName + ".R$" + type; // 根据匿名内部类的命名, 拼写出R⽂件的包名+类名 try { Class cls = ass(rClassName); // 加载R⽂件 resID = (Integer) ld(fieldName).get(null); // 反射获取R⽂件对应资源名的ID } catch (Exception e) { tackTrace(); } } else { Log.w(TAG, "resource is null:" + packageName); } return resID;}现在我们加载已经安装APK的资源的编码就已经完成.调⽤getDrawable("cebundle", "image1")未安装的应⽤我们先看⼀下getDrawable⽅法是怎么去获取资源的public Drawable getDrawable(@DrawableRes int id, @Nullable Theme theme) throws NotFoundException { final TypedValue value = obtainTempTypedValue(); try { final ResourcesImpl impl = mResourcesImpl; ue(id, value, true); return awable(this, value, id, theme, true); } finally { releaseTempTypedValue(value); }}上⾯代码我们可以看到, 资源其实是通过impl代理去拿到的, 继续...void getValue(@AnyRes int id, TypedValue outValue, boolean resolveRefs) throws NotFoundException { boolean found = ourceValue(id, 0, outValue, resolveRefs); if (found) { return; } throw new NotFoundException("Resource ID #0x" + tring(id));}然后再通过assets去代理获取, 继续看看assets从哪⾥设置的public ResourcesImpl(@NonNull AssetManager assets, @Nullable DisplayMetrics metrics, @Nullable Configuration config, @NonNull DisplayAdjustments displayAdjustments) { mAssets = assets; efaults(); mDisplayAdjustments = displayAdjustments; updateConfiguration(config, metrics, patibilityInfo()); StringBlocks();}再寻找ResourcesImpl的构造函数从哪⾥调⽤public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) { this(null); mResourcesImpl = new ResourcesImpl(assets, metrics, config, new DisplayAdjustments());}最后, 我们回到Resource的构造函数中, 也就是说正确调⽤Resources的构造函数, 那么我们就能构造出正确的Resource那么我们现在就可以编码了/** * 加载未安装应⽤资源包 *

* @param resourcePath * @return */public LoadedResource loadResource(String resourcePath) { LoadedResource loadResource = null; PackageInfo info = queryPackageInfo(resourcePath); // 获取未安装APK的PackageInfo if (info != null) { // 获取成功 loadResource = (eName); // 先从缓存中取, 存在则直接返回, 不重复添加. 否则就搜索添加 if (loadResource == null) { try { AssetManager assetManager = tance(); // 创建AssetManager实例 Class cls = ; Method method = hod("addAssetPath", ); (assetManager, resourcePath); // 反射设置资源加载路径 Resources resources = new Resources(assetManager, ources().getDisplayMetrics(), ources().getConfiguration()); // 构造出正确的Resource loadResource = new LoadedResource(); ces = resources; eName = eName; oader = new DexClassLoader(resourcePath, mDexDir, null, ssLoader()); // 设置正确的类加载器, 因为需要去加载R⽂件 (eName, loadResource); // 缓存 Log.w(TAG, "build resource:" + resourcePath); } catch (Exception e) { tackTrace(); } } } Log.w(TAG, "load resource:" + resourcePath); return loadResource;}/** * 获取未安装应⽤PackageInfo *

* @param resourcePath * @return */private PackageInfo queryPackageInfo(String resourcePath) { return kageManager().getPackageArchiveInfo(resourcePath, _ACTIVITIES);}既然我们现在已经获取了Resource, 那么下⾯获取资源⽂件就与上⾯是⼀样的LoadedResource loadResource = loadResource("/storage/sdcard0/");Drawable drawable = getDrawable(eName, "image");最后代码ckage ;import ces;public class LoadedResource { public Resources resources; public String packageName; ckage ;import ;import ;import p;import ;import t;import eInfo;import eManager;import anager;import ces;import le;import ;import ssLoader;public class ResourceManager { private static final String TAG = "ResourceManager"; private ResourceManager() { } public static void init(Context context) { (context); (context); } public static UnInstalled unInstalled() { return er; } public static Installed installed() { return er; } /** * 针对于未安装应⽤ */ public static class UnInstalled { static final UnInstalled sManager = new UnInstalled(); private Context mContext; private Map mRescources = new HashMap(); private String mDexDir; private UnInstalled() { } /** * 初始化 * * @param context */ public void init(Context context) { mContext = licationContext(); File dexDir = ("dex", _PRIVATE); if (!()) { (); } mDexDir = olutePath(); } /** * 获取未安装应⽤资源的ID * 获取未安装应⽤资源的ID * * @param packageName * @param fieldName * @return */ public int getResourceID(String packageName, String type, String fieldName) { int resID = 0; LoadedResource recource = getUnInstalledRecource(packageName); String rClassName = packageName + ".R$" + type; Log.w(TAG, "resource class:" + rClassName + ",fieldName:" + fieldName); try { Class cls = ass(rClassName); resID = (Integer) ld(fieldName).get(null); } catch (Exception e) { tackTrace(); } return resID; } /** * 获取未安装应⽤Drawable * * @param packageName * @param fieldName * @return */ public Drawable getDrawable(String packageName, String fieldName) { Drawable drawable = null; int resourceID = getResourceID(packageName, "drawable", fieldName); LoadedResource recource = getUnInstalledRecource(packageName); if (recource != null) { drawable = wable(resourceID); } return drawable; } /** * 加载未安装应⽤资源包 * * @param resourcePath * @return */ public LoadedResource loadResource(String resourcePath) { LoadedResource loadResource = null; PackageInfo info = queryPackageInfo(resourcePath); // 获取未安装APK的PackageInfo if (info != null) { // 获取成功 loadResource = (eName); // 先从缓存中取, 存在则直接返回, 不重复添加. 否则就搜索添加 if (loadResource == null) { try { AssetManager assetManager = tance(); // 创建AssetManager实例 Class cls = ; Method method = hod("addAssetPath", ); (assetManager, resourcePath); // 反射设置资源加载路径 Resources resources = new Resources(assetManager, ources().getDisplayMetrics(), ources().getConfiguration()); // 构造出正确的Resource loadResource = new LoadedResource(); ces = resources; eName = eName; oader = new DexClassLoader(resourcePath, mDexDir, null, ssLoader()); // 设置正确的类加载器, 因为需要去加载R⽂件 (eName, loadResource); // 缓存 Log.w(TAG, "build resource:" + resourcePath); } catch (Exception e) { tackTrace(); } } } Log.w(TAG, "load resource:" + resourcePath); return loadResource; } /** * 获取未安装应⽤PackageInfo * * @param resourcePath * @return */ private PackageInfo queryPackageInfo(String resourcePath) { return kageManager().getPackageArchiveInfo(resourcePath, _ACTIVITIES); } /** * 获取未安装应⽤LoadResource * * @param packageName * @return */ public LoadedResource getUnInstalledRecource(String packageName) { LoadedResource loadResource = (packageName); if (loadResource == null) { Log.w(TAG, "resource " + packageName + " not founded"); } return loadResource; } } /** * 针对于已安装应⽤ */ public static class Installed { static final Installed sManager = new Installed(); private Context mContext; private Map mResources = new HashMap(); private Installed() { } /** * 初始化 * * @param context */ public void init(Context context) { mContext = licationContext(); } /** * 获取已安装应⽤资源 * * @param packageName */ public LoadedResource getInstalledResource(String packageName) { LoadedResource resource = (packageName); // 先从缓存中取, 没有就去加载 if (resource == null) { try { Context context = PackageContext(packageName, T_INCLUDE_CODE | T_IGNORE_SECURITY); resource = new LoadedResource(); eName = packageName; ces = ources(); oader = ssLoader(); (packageName, resource); // 得到结果缓存起来 } catch (Exception e) { tackTrace(); } } return resource; } /** * 获取资源ID * * @param packageName * @param type * @param fieldName * @return */ public int getResourceID(String packageName, String type, String fieldName) { int resID = 0; LoadedResource installedResource = getInstalledResource(packageName); // 获取已安装APK的资源 if (installedResource != null) { String rClassName = packageName + ".R$" + type; // 根据匿名内部类的命名, 拼写出R⽂件的包名+类名 try { Class cls = ass(rClassName); // 加载R⽂件 resID = (Integer) ld(fieldName).get(null); // 反射获取R⽂件对应资源名的ID } catch (Exception e) { tackTrace(); } } else { Log.w(TAG, "resource is null:" + packageName); } return resID; } /** * 获取已加载应⽤Drawable * * @param packageName * @param fieldName * @return */ public Drawable getDrawable(String packageName, String fieldName) { Drawable drawable = null; int resourceID = getResourceID(packageName, "drawable", fieldName); LoadedResource installedResource = getInstalledResource(packageName); if (installedResource != null) { drawable = wable(resourceID); } return drawable; } }}activity_

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信