热更新实现方式

2020-02-29  本文已影响0人  anloney

SDK 动态加载替换资源和类文件可以有几种实现方式,一种是可以资源文件和代码分开进行加载与替换,例如加载图片资源文件使用下发的资源文件,然后使用第三方图片加载框架(Glide)通过文件路径读取对应图片并加载显示。获取 string 和 color 类型的资源可以使用下发 Json 文件并解析的方式获取,或者使用字符串包含特定的分隔符将不同string或color资源综合到一起直接下发。动态加载代码使用类加载器 DexClassLoader 去加载 jar 或dex 文件,读取其中对应的 class 类然后获取新的代码类并执行后续操作。

另一种方式是将新增或替换资源与代码放到同一个工程里并打包成 apk 文件下发到 sdk,sdk 通过读取插件 apk 并获取 apk 的 resource 对象,实现读取插件里资源的功能,通过发射获取需要的类文件来实现更新代码的功能。

首先需要知道Android有两种加载器: DexClassLoader : 可以加载文件系统上的dex、jar和apk PathClassLoader : 只可以加载已经安装好了的apk文件,在/data/app/<packagename>/目录下。 DexClassLoader只有一个构造函数,其参数如下: 类加载器.png

以下分别阐述动态加载的不同实现方式:

1. 资源和类分别加载

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">./dx --dex --output=/Users/anjingshuai/Desktop/dex/classes.dex /Users/anjingshuai/Desktop/dex/classes.jar </pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n51" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //如果没有读取到文件或者不需更新
if (!LOAD_FROM_ASSETS) {
Class dynamicLoader = Class.forName("com.ushareit.fixbuglib.Utils").newInstance();
} else {
File file = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "classes.dex");
//优化后的dex文件输出目录,应用必须具备读写权限
String optimizedDirectory = getDir("dex", MODE_PRIVATE).getAbsolutePath();
DexClassLoader mCustomClassLoader = new DexClassLoader(file.getAbsolutePath(), optimizedDirectory, null, getClassLoader());
Class dynamicLoader = mCustomClassLoader.loadClass("com.ushareit.fixbuglib.Utils");
}</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n53" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> String sdkDataDirLocation = context.getFilesDir().getPath() + File.separator + "classes.dex";
InputStream is = context.getAssets().open("classes.dex");
OutputStream os = new FileOutputStream(sdkDataDirLocation);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
is.close();
os.flush();
os.close();
File optimizedLibraryPath = context.getDir("optimized", 0);
return new DexClassLoader(adsSdkDataDirLocation, optimizedLibraryPath
.getPath(), null, getClassLoader());</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n57" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">ImageView imageView = findViewById(R.id.image_view);// 加载资源文件 assets 下的图片 image_test.png
String path = "file:///android_asset/image_test.png";
Glide.with(this).load(path).into(imageView);

// 或者加载SD卡根目录的test.jpg 图片 ,通过Flie文件读取
File file = new File(Environment.getExternalStorageDirectory(), "image_test.png);
Glide.with(context).load(file).into(imageView);</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n63" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> // 创建AssetManager实例
AssetManager assetManager = AssetManager.class.newInstance();
Class cls = AssetManager.class;
Method method = cls.getMethod("addAssetPath", String.class);
// 反射设置资源加载路径
method.invoke(assetManager, resourcePath);
// 构造出正确的Resource
Resources mResources = new Resources(assetManager, mContext.getResources().getDisplayMetrics(), mContext.getResources().getConfiguration());
//获取apk插件的类加载器
File dexDir = mContext.getDir("dex", Context.MODE_PRIVATE);
if (!dexDir.exists()) {
dexDir.mkdir();
}
String mDexDir = dexDir.getAbsolutePath();
DexClassLoader mDexClassLoader = new DexClassLoader(resourcePath, mDexDir, null, mContext.getClassLoader());</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n67" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> public int getResourceID(String type, String fieldName) {
int resID = 0;
String packageName = mContext.getPackageManager().getPackageArchiveInfo(resourcePath, PackageManager.GET_ACTIVITIES).packageName;
String rClassName = packageName + ".R$" + type;
try {
Class cls = mDexClassLoader.loadClass(rClassName);
resID = (Integer) cls.getField(fieldName).get(null);
} catch (Exception e) {
e.printStackTrace();
}
return resID;
}</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n69" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> /**

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n73" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> Class clazz = mDexClassLoader.loadClass("com.ushareit.fixbuglib.Utils");</pre>

然后根据新加载的类去做相应的操作。

然后设置图片、颜色或字符串内容时直接根据资源名称获取对应的在插件里的值,然后对相应的控件设置即可。

然后想要获取文字、图片和颜色可以使用如下方法,使用新建的 mResource 和 mDexClassLoader 来获取对应名称的资源。

然后是动态获取资源id

2. 使用下发 apk 形式同时解析资源和代码

然后同样方法获取对应需要替换的类文件。

或者使用读取 asset 目录下的 dex 文件来生成 DexClassLoader:

读取过程代码示例如下,其中 classes.dex 为刚刚转换得到的 .dex 代码插件,以下代码分别使用从 sd 卡目录读取和从项目的 assets 文件夹下读取。

转成 dex 文件后可以下发此文件到对应应用,应用下载或本地保存到手机 sd 卡目录或者项目的 assets 目录下。通过类加载器去加载此文件然后读取需要替换的类文件,用来替换之前 sdk 里已经存在的类,在加载前可以进行判断是否需要从下发的文件读取。

上一篇下一篇

猜你喜欢

热点阅读