美文网首页
热更新实现方式

热更新实现方式

作者: anloney | 来源:发表于2020-02-29 21:20 被阅读0次

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. 资源和类分别加载

  • 动态加载更新的代码,与 Facebook 的广告动态读取 audience_network.dex 文件并解析获取对应的加载广告接口类的实现方法类似。

    发现有 bug 的类并修改后或新增接口方法后,可以在重新生成一个 aar 文件,打开 aar 文件可以找到里边的 classes.jar 文件,里边包含所有的修改过的最新代码。 原始的 class 打包的 jar 文件是不能直接使用的,需要使用官方提供的dx工具,这个工具是在 /androidSDK/build-tools/<versioncode>/下的dx.bat 工具,再次路径下执行命令行终端使用 dx 指令即可生成。 指令为 dx --dex --output=输出的dex文件完整路径 (空格) 要打包的完整class文件所在目录,如:

<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;"> /**

  • 获取未安装资源Drawable
  • @param fieldName 资源名
  • @return
    /
    public Drawable getDrawable(String fieldName) {
    Drawable drawable = null;
    int resourceID = getResourceID("drawable", fieldName);
    return mResource..getDrawable(resourceID);
    }

    /
    *
  • 获取未安装资源String
  • @param fieldName 资源名
  • @return
    /
    public String getString(String fieldName) {
    String string = null;
    int resourceID = getResourceID("string", fieldName);
    return mResource.getString(resourceID);
    }

    /
    *
  • 获取未安装资源color
  • @param fieldName 资源名
  • @return
    */
    public int getColor(String fieldName) {
    int color = 0;
    int resourceID = getResourceID("color", fieldName);
    return mResource..getColor(resourceID);
    }</pre>

<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>

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

  • 动态加载更新的代码

    加载代码与第一部分加载 .dex 文件的代码类似,即用生成的DexClassLoader 对象去加载对应的类

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

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

然后是动态获取资源id

  • 加载资源文件

    将修复或新增的功能完善后可以编译成一个 apk 并下发给 sdk,通过 ClassLoader 获取apk 中资源的 .R 并返回资源 id,创建 apk 插件的 Resources 对象设置资源 id 得到具体的图片、文字和颜色。 其中 resourcePath 表示下发的 apk 存储到手机里的目标目录。

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

  • 加载资源文件

    资源文件中 string 和 color 类型的资源可以通过下发 json 字符串并解析获取, 图片资源可以下发压缩包,包含对应的图片,sdk 获取后解压到目标目录,然后在加载显示图片时使用第三方图片加载框架去读取,例如使用 Glide 加载 assets 目录下的图片:

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

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

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

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

相关文章

  • 热更新实现方式

    SDK 动态加载替换资源和类文件可以有几种实现方式,一种是可以资源文件和代码分开进行加载与替换,例如加载图片资源文...

  • iOS热更新实现方式

    苹果静止热更新,可惜我的是企业app,没有这些约束了,随便用。(当然有些热更新已经可以通过苹果审核了,比如JSPa...

  • Android热更新实现方式

    弄完iOS热更新实现方式,空闲时间想去弄弄Android的热更新,寻找一些资料后暂时得出以下热更新方法。尝试几中方...

  • 热更新的几种实现方式

    热更新的几种实现方式 [TOC] 一. 动态库 使用 OC / Swift 原生实现 把需要集成的功能模块,打包成...

  • iOS热更新-8种实现方式

    一、JSPatch 热更新时,从服务器拉去js脚本。理论上可以修改和新建所有的模块,但是不建议这样做。 建议 用来...

  • iOS热更新-8种实现方式

    一、JSPatch 官网:http://www.jspatch.com 怎么使用官网文档写的很清楚。 热更新时,从...

  • ReactNative App更新下载(Android+iOS)

    APP涉及到版本更新(非热更新),版本检测下载App,Android和iOS实现方式不同 1.Android直接和...

  • Android Sophix 补丁生成工具使用

    为了实现阿里Sophix 热更新功能实现 这里记录下 Sophix 补丁生成工具的使用方式 1. 工具下载地址 下...

  • 【ReactNative】热更新(实战篇)

    写在前面:这里只讲实现热更新的增量更新.想要了解更新的其他方式或者是不同方式的比较请自行研究. 我的学习思路: 一...

  • Weex

    认识有限,不对的地方一起探索 :) 热更新框架的实现方式: Hybird : RN&Weex: Flutter:...

网友评论

      本文标题:热更新实现方式

      本文链接:https://www.haomeiwen.com/subject/dfpmhhtx.html