Android Hot Update-non-intrusive AOP framework

Source: Internet
Author: User

Android client application on-line, once the bug, the general solution is to send a repair package upgrade application, this method is not only time-consuming, more important is the user needs frequent upgrade version, experience is not good, so the idea of optimization is in the case of non-release version of the hot update, in order to improve the user experience.

A recent github new non-intrusive runtime AOP framework dexposed, here's a quick look at this framework, GitHub address.

Brief description:

Based on AOP, the framework supports classic AOP usage scenarios and can be applied to logging, performance statistics, security control, transaction processing, and exception handling.
For Android platforms, dexposed supports online hot updates at the function level, such as a host apk that has been released on the marketplace, and when we find a bug in a function call from the crash statistics platform that leads to a recurring crash, a patch apk can be developed locally, and publish to the server, the host apk download this patch apk and after integration, it is easy to fix this crash.
Dexposed is a powerful non-intrusive runtime AOP framework implemented on an Android platform based on the prestigious open source xposed framework. The AOP implementation of dexposed is completely non-intrusive and does not use any annotation processor, braid or bytecode rewrite.

Patch principle

First pull down the code from GitHub there are several pits to note:

    • If you want to use it directly, you may be disappointed because Patchsample has no entry activity for the Android program, so you need to create a new one yourself.
    • Notice how the Lib package is added and cannot be added directly to the Libs project, which should be placed in the Lib file and manually add the Java build dependency
    • Note that using the process is not as concise as that described on GitHub, you need to add the Sunapkpatch code yourself and add the appropriate catch exception action to prevent patch packets from causing crash.
    • Patchsample is just a simple patch, if you need to add more complex patches, need to have dependencies, in order to reduce the patch volume, this dependency can only be part of the dependency, so the recommended way is to use a patch of class file to hit the jar package to rely on.

Next we look at the specific process:

First, we need to dynamically monitor the AOP environment

runPatchApk();

Note here is Patchmain.load (), the main purpose of this method is to load all the classes of the patch apk, and add the implementation of the Ipatch class to the list, and then implement the non-intrusive AOP by matching the loaded class or class method.

 Public void runpatchapk() {if(Android.os.Build.VERSION.SDK_INT = = +) {return; }if(! Dexposedbridge.candexposed ( This) {LOG.D ("HotPatch","This device doesn ' t support dexposed!");return; } File Cachedir = Getexternalcachedir ();if(Cachedir! =NULL) {String FullPath = Cachedir.getabsolutepath () + File.separator +"patch_name.apk"; Patchresult result = Patchmain.load ( This, FullPath,NULL);if(Result.issuccess ()) {LOG.E ("HotPatch","Patch success!"); }Else{LOG.E ("HotPatch","Patch error is"+ Result.geterrorinfo ()); }    }}
Patch Practices
 Public  class Activity extends basesherlocksubactivity  implements  Onnewiconuirefreshlistener {        Private void ShowDialog() {FinalAlertdialog.builder Builder =NewAlertdialog.builder ( This); Builder.settitle ("Dexposed Sample"). Setmessage ("Please clone Patchsample project to generate APK, and copy it to \"/android/data/package_name/cache/patch_name.apk\ " "). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {}}). Create (). Show (); }}

If the code in our line is shown above, there is a copy bug in the shell, then how to hot update.
Code repair operation in the patch project, add the following code:

 Public  class dialogpatch implements ipatch {    @Override     Public void Handlepatch(FinalPatchparam arg0)throwsThrowable {class<?> CLS =NULL;Try{cls = Arg0.context.getClassLoader (). LoadClass ("Com.android.activity.Activity"); }Catch(ClassNotFoundException e) {E.printstacktrace ();return; } dexposedbridge.findandhookmethod (CLS,"ShowDialog",NewXc_methodreplacement () {@Override            protectedObjectReplacehookedmethod(Methodhookparam param)throwsThrowable {FinalActivity mainactivity = (activity) Param.thisobject; Alertdialog.builder Builder =NewAlertdialog.builder (mainactivity); Builder.settitle ("Fanli dexposed Sample"). Setmessage ("The dialog is shown from Patch apk!"). Setpositivebutton ("OK",NewOnclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {class<?> clsinner;Try{Clsinner = Arg0.context.getClassLoader (). LoadClass ("Com.android.activity.OutObject"); }Catch(ClassNotFoundException e) {E.printstacktrace ();return; }Try{Outobject Outobject = (outobject) clsinner.newinstance ();if(Outobject.callfromoutmethod ()) {Alertdialog.builder Builder =NewAlertdialog.builder (mainactivity); Builder.settitle ("Fanli dexposed Sample"). Setmessage ("Com.android.activity.OutObject is worked!"). Setpositivebutton ("OK",NewOnclicklistener () {@Override                                             Public void OnClick(Dialoginterface Dialog,intwhich) {Dialog.dismiss ();                            }}). Create (). Show (); }                        }Catch(Instantiationexception e)                        {E.printstacktrace (); }Catch(Illegalaccessexception e)                        {E.printstacktrace (); }}). Create (). Show ();return NULL;    }        }); }}

Then upload the patch apk to the server, download the patch apk to the specified directory in the main apk, and then dynamically monitor the AOP environment and Loadpatch to implement the hot update.
Next, if the application to the actual project needs to be perfected in the following points:

    1. Dynamic monitoring of the AOP environment (Android Server active monitoring patch package)
    2. Load patch files dynamically (update pathc the first time after patch is loaded)
Patch test Results:

The environments that are based on the above implementation scenarios are:
Dalvik 4.0-4.4 have passed
Currently ART 5.0 and above are not yet approved. (Native package and jar package to be updated)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Hot Update-non-intrusive AOP framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.