本文主要是介绍Android AndFix 热修复框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/mackkill/article/details/51778202
http://blog.csdn.net/mackkill/article/details/51778202
Android AndFix 热修复框架

版权声明:本文为博主原创文章,未经博主允许不得转载。
今天带来的是阿里的热修复框架AndFix使用,废话不多说直接看下文。
项目Git地址:https://github.com/alibaba/AndFix
lib包:AndFixLib
补丁制作工具: apkpatch
ps:上面下载的包里面会发现.so文件和官方的不一样,这里要特殊说明下,由于在64位平台上使用时会报错,所以这里是通过Android studio添加依赖'com.alipay.euler:andfix:0.4.0@aar'
然后在C:\Users…gradle\cache.. 下面找到 andfix-0.4.0.aar,解压缩,从里面取出 .so文件,然后放在官方下载的Eclipse下的demo里面使用(这里很奇怪官方的demo为什么是eclipse的)。这个问题也是在issue里面有网友提供的。
下载来看看使用方式,如果想了解原理的可以上官网看看,这里就不说了。
使用
如果你用的是AS的话就直接添加依赖
<code class="hljs autohotkey has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-escape" style="box-sizing: border-box;">``</span><span class="hljs-escape" style="box-sizing: border-box;">` </span>dependencies { <span class="hljs-label" style="box-sizing: border-box;"> compile 'com.alipay.euler:andfix:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.4</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>@aar' }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
然后.so包可以使用上面下载的。
如果是使用的Eclipse的就直接全部使用上面的那个lib包就好了。
接下来在继承Application的类里面添加初始方法:
一、
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">初始化 patchManager = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PatchManager(context); patchManager.init(appversion);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//current version</span> patchManager.loadPatch();</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
二、在需要加载补丁的地方调用
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">patchManager.addPatch(path);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//path 是你补丁在本地存储的路径,一般修复线上项目得通过后台下载补丁到sd卡</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
三、生成补丁
知道使用方法了,接下来就是制作补丁了,下载上面提供的补丁制作工具apkpatch,然后解压到本地
通过命令行进入到该目录下,这里需要:有bug的apk包、修复后的apk包、密钥。(这里建议所有东西都放解压目录下比较方便)
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">apkpatch.bat –f new.apk –t old.apk –o output –k abc.keystore –p 111111 -a abc.keystore -e 111111-<span class="ruby" style="box-sizing: border-box;">f new.apk <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>指定新的版本apk </span>-<span class="ruby" style="box-sizing: border-box;">t old.apk <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>指定旧版本有bug的apk </span>-<span class="ruby" style="box-sizing: border-box;">o output <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>生成的补丁文件所放的文件夹 </span>-<span class="ruby" style="box-sizing: border-box;">k abc.keystore <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>打包密钥 </span>-<span class="ruby" style="box-sizing: border-box;">p <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">111111</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>打包密钥密码 </span>-<span class="ruby" style="box-sizing: border-box;">a abc.keystore <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>打包别名密钥 </span>-<span class="ruby" style="box-sizing: border-box;">e <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">111111</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>打包别名密钥密码(这样一般和密钥密码一致)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>
如果有多个补丁的话可以使用下面的命令进行补丁合并:
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">apkpatch -m <apatch_path...> -o <output> -k <keystore> -p <***> -a <alias> -e <***>-<span class="ruby" style="box-sizing: border-box;">a,--<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span>> keystore entry <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">alias</span>. </span> -<span class="ruby" style="box-sizing: border-box;">e,--epassword <***> keystore entry password. </span> -<span class="ruby" style="box-sizing: border-box;">k,--keystore <loc> keystore path. </span> -<span class="ruby" style="box-sizing: border-box;">m,--merge <loc...> path of .apatch files. </span> -<span class="ruby" style="box-sizing: border-box;">n,--name <name> patch name. </span> -<span class="ruby" style="box-sizing: border-box;">o,--out <dir> output dir. </span> -<span class="ruby" style="box-sizing: border-box;">p,--kpassword <***> keystore password.</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
最后会在指定目录下生成.apatch文件,将其放入sd卡中就可以测试啦。
好了以上就是基本的集成使用方式了,加上和之前使用的RocooFix框架做对比,发现AndFix覆盖面大一点(不过也有网友反映会报错),测试了三台手机RocooFix只有一台有效,AndFix三台都可以,不得不说阿里粑粑还是牛*的。总的来说现在的热修复框架还没达到很成熟的地步,以上两种只是能修改些类里面的方法,而不能添加类和其他文件,对于修复一些比较小的紧急bug还是可以的,但是覆盖面还有待考证。不过AndFix是基于jni来实现,在安全方面会更好些。
由于android热修复的缺陷,现在比较火热的还属React-Native、Weex。 有兴趣的同学可以去研究下。
以上是个人见解,如有不对还望指出。
这篇关于Android AndFix 热修复框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!