andfix增量升级更新

项目地址:https://github.com/alibaba/AndFix


dexposed和andfix是alibaba的开源项目,都是apk增量更新的实现框架,目前dexposed的兼容性较差,只有2.3,4.0~4.4兼容,其他Android版本不兼容或未测试,详细可以去dexposed的github项目主页查看(https://github.com/alibaba/dexposed),而andfix则兼容2.3~6.0,所以就拿这个项目来实现增量更新吧。所以还是来研究下Andfix吧。

首先看官方说明是必须的,重点是后面的注意事项

AndFix

Download  

AndFix is a solution to fix the bugs online instead of redistributing Android App. It is distributed as Android Library.

Andfix is an acronym for "Android hot-fix".

AndFix supports Android version from 2.3 to 6.0, both ARM and X86 architecture, both Dalvik and ART runtime.

The compressed file format of AndFix's patch is .apatch. It is dispatched from your own server to client to fix your App's bugs.

Principle

The implementation principle of AndFix is method body's replacing,

Method replacing

AndFix judges the methods should be replaced by java custom annotation and replaces it by hooking it. AndFix has a native method art_replaceMethod in ART or dalvik_replaceMethod in Dalvik.

For more details, here.

Fix Process

Integration

How to get?

Directly add AndFix aar to your project as compile libraries.

For your maven dependency,

``` <dependency> <groupId>com.alipay.euler</groupId> <artifactId>andfix</artifactId> <version>0.4.0</version> <type>aar</type> </dependency> ``` 

For your gradle dependency,

``` dependencies { compile 'com.alipay.euler:andfix:0.4.0@aar' } ``` 

How to use?

  1. Initialize PatchManager,

    patchManager = new PatchManager(context); patchManager.init(appversion);//current version 
  2. Load patch,

    patchManager.loadPatch(); 

    You should load patch as early as possible, generally, in the initialization phase of your application(such asApplication.onCreate()).

  3. Add patch,

    patchManager.addPatch(path);//path of the patch file that was downloaded 

    When a new patch file has been downloaded, it will become effective immediately by addPatch.

Developer Tool

AndFix provides a patch-making tool called apkpatch.

How to get?

The apkpatch tool can be found here.

How to use?

  • Prepare two android packages, one is the online package, the other one is the package after you fix bugs by coding.

  • Generate .apatch file by providing the two package,

usage: apkpatch -f <new> -t <old> -o <output> -k <keystore> -p <***> -a <alias> -e <***> -a,--alias <alias> keystore entry alias. -e,--epassword <***> keystore entry password. -f,--from <loc> new Apk file path. -k,--keystore <loc> keystore path. -n,--name <name> patch name. -o,--out <dir> output dir. -p,--kpassword <***> keystore password. -t,--to <loc> old Apk file path. 

Now you get the application savior, the patch file. Then you need to dispatch it to your client in some way, push or pull.

Sometimes, your team members may fix each other's bugs, and generate not only one .apatch. For this situation, you can merge .apatch files using this tool,

usage: apkpatch -m <apatch_path...> -o <output> -k <keystore> -p <***> -a <alias> -e <***> -a,--alias <alias> keystore entry alias. -e,--epassword <***> keystore entry password. -k,--keystore <loc> keystore path. -m,--merge <loc...> path of .apatch files. -n,--name <name> patch name. -o,--out <dir> output dir. -p,--kpassword <***> keystore password. 

Running sample

  1. Import samplesI/AndFixDemo to your IDE, append AndFixDemo dependencies with AndFix(library project or aar).
  2. Build project, save the package as 1.apk, and then install on device/emulator.
  3. Modify com.euler.test.A, references com.euler.test.Fix.
  4. Build project, save the package as 2.apk.
  5. Use apkpatch tool to make a patch.
  6. Rename the patch file to out.apatch, and then copy it to sdcard.
  7. Run 1.apk and view log.

Notice

ProGuard

If you enable ProGuard, you must save the mapping.txt, so your new version's build can use it with "-applymapping".

And it is necessary to keep classes as follow,

  • Native method

    com.alipay.euler.andfix.AndFix

  • Annotation

    com.alipay.euler.andfix.annotation.MethodReplace

To ensure that these classes can be found after running an obfuscation and static analysis tool like ProGuard, add the configuration below to your ProGuard configuration file.

``` -keep class * extends java.lang.annotation.Annotation -keepclasseswithmembernames class * { native <methods>; } ``` 

Self-Modifying Code

If you use it, such as Bangcle. To generate patch file, you'd better to use raw apk.

Security

The following is important but out of AndFix's range.

  • verify the signature of patch file
  • verify the fingerprint of optimize file

API Documentation

The libraries javadoc can be found here.



官方说明都比较清楚了,很简单,相信大家看看都明白。Demo中有栗子,写的很清楚了。



重点来了:


1.andfix不支持布局资源等的修改


2.使用了apk加固时,发现在加固前要先apkpatch制作补丁,不能使用加固后的apk制作,否则补丁无法使用,但是在加固前制作的补丁可以很容易的被反编译出源码


3.如果app已经出现bug,需要加载补丁进行修补,官方的方法是先调用mPatchManager.init方法,然后。。。

andfix增量升级更新_第1张图片


那么问题来了,在我测试时发现:当补丁被加载过后,每次退出app,然后打开app都发现补丁中修复的方法还是被调用了。当删除SD卡中的out.apatch文件,退出应用,甚至是重启手机,app还是使用了补丁中修复的方法。那么当我更新版本后,第一次加载时,加载的是app中修复的方法,退出应用,第二次进入app后,又加载了之前补丁中修复的方法。只有当在应用管理器中清楚app应用内存时,才有效。这就见鬼了。

那我们更新版本修复的bug就无效了吗?一直加载补丁中的方法?只能看代码了。


andfix增量升级更新_第2张图片

当看到这个条件,一切都明朗了。shit !居然只有版本更新,第一次进入后,才会清除内存中的补丁。Fuck !这什么清楚,官方也不说清楚点。

解决办法:每次都先清除内存中的补丁


这样做之后,发现还是有问题,结果还是跟之前没清除一样。查看代码发现,很简单,原来是Andfix没有为我们清楚SD卡中的out.apatch文件导致的。

那么path.delete();搞定。

运行一下,很好。这就是我们想要的效果了。



上代码


官网告诉我们应该调用这个方法:

andfix增量升级更新_第3张图片

然后,为了严谨性,我们自己在patchManager.class中定义一个我们需要的方法:

andfix增量升级更新_第4张图片
改为调用这个方法。
另外,如果版本更新了,我们就删除out.apatch文件,不加载补丁,第二次进入在加入补丁。其实第二次肯定就没有补丁可加了,被我们删除了(奸笑中)

但是,当又有bug需要修复时,然后就开始循环我们之前说的补丁加载流程了。
 
 
是不是很简单啊,alibaba太强大了,太NB了,这种开源的代码,对我们帮助太大了,赶紧上淘宝、天猫淘几样东西回家,感谢马云,感谢习大大,感谢CCTV。

当然,具体的增量升级怎么做,就需要跟后台服务器人员配合了。
有问题欢迎留言讨论。
分享是一种快乐,See you !


相关资料:

1. https://mp.weixin.qq.com/s?__biz=MzI1MTA1MzM2Nw==&mid=400118620&idx=1&sn=b4fdd5055731290eef12ad0d17f39d4a&scene=1&srcid=1106Imu9ZgwybID13e7y2nEi#wechat_redirect

2. http://blog.csdn.net/lmj623565791/article/details/49883661



你可能感兴趣的:(版本更新,AndFix,热补丁修复,增量升级更新)