热更新实现方式

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 里已经存在的类,在加载前可以进行判断是否需要从下发的文件读取。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335