Android hot Update-non-intrusive AOP framework, android intrusive aop framework

Source: Internet
Author: User

Android hot Update-non-intrusive AOP framework, android intrusive aop framework

After the Android client application is launched, if a Bug occurs, the general solution is to release a repair package to upgrade the application. This method is not only time-consuming, but more importantly, the user needs to frequently upgrade the version, resulting in poor experience, therefore, the optimization idea is to hot update without releasing a version, in order to improve the user experience.

Recently, GitHub has developed a new non-intrusive runtime AOP framework Dexposed. The following is a brief introduction to this framework, GitHub address.

Brief description:

This framework is based on the idea of AOP and supports typical AOP application scenarios. It can be used in logging, Performance Statistics, security control, transaction processing, Exception Processing, and other aspects.
For the Android platform, Dexposed supports function-level online hot updates. For example, for the host APK that has been released on the application market, when we find a function call bug on the crash statistics platform, this causes frequent crash. In this case, you can develop a local patch APK and release it to the server. After the host APK downloads the patch APK and integrates it, you can easily fix the crash.
Dexposed is a powerful non-invasive runtime AOP framework on the Android platform based on the prestigious open-source Xposed framework. Dexposed's AOP implementation is completely non-intrusive and does not use any annotation processor, braid, or bytecode rewriter.

Patch Principle

First, pull down the code from GitHub and pay attention to the following issues:

  • If you want to use it directly, You may be disappointed because the android app patchsample has no entry Activity, so you need to create it on your own.
  • Note how to add the Lib package. It cannot be directly added to the self-built file of the libs project. It should be placed in the lib file and manually added to the java build dependency.
  • Note that the usage process is not as concise as described on GitHub. You need to add the sunApkPatch code and add the exception capture operation as appropriate to prevent the Crash caused by the Patch package.
  • Patchsample is just a simple patch. If you need to add more complex patches, dependencies are required. To reduce the patch size, this dependency can only be partial, therefore, we recommend that you add a jar package to the class file to be patched to introduce dependencies.

Next let's take a look at the specific process:

First, we need to dynamically monitor the AOP environment.

runPatchApk();

Note the PatchMain. load (). The main purpose of this method is to load all the classes of the patch APK and add the classes that implement IPatch to the List, then, non-intrusive AOP is implemented by matching the loaded class or class method.

public void runPatchApk() {    if (android.os.Build.VERSION.SDK_INT == 21) {        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 practice
public class Activity extends BaseSherlockSubActivity implements    OnNewIconUIRefreshListener {    private void showDialog() {    final AlertDialog.Builder builder = new AlertDialog.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", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                }            }).create().show();    }}

If a text bug occurs in the bullet layer as shown in the code we launched, how can we hot update it.
In the Patch project, add the following code:

public class DialogPatch implements IPatch {    @Override    public void handlePatch(final PatchParam arg0) throws Throwable {        Class<?> cls = null;        try {            cls = arg0.context.getClassLoader().loadClass("com.android.activity.Activity");        } catch (ClassNotFoundException e) {            e.printStackTrace();            return;        }        DexposedBridge.findAndHookMethod(cls, "showDialog", new XC_MethodReplacement() {            @Override            protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {                final Activity mainActivity = (Activity) param.thisObject;                AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);                builder.setTitle("Fanli Dexposed sample").setMessage("The dialog is shown from patch apk!").setPositiveButton("ok", new OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {                        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 = new AlertDialog.Builder(mainActivity);                                builder.setTitle("Fanli Dexposed sample").setMessage("com.android.activity.OutObject is Worked!")                                        .setPositiveButton("ok", new OnClickListener() {                                            @Override                                            public void onClick(DialogInterface dialog, int which) {                                                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. In the main APK, download the patch apk to the specified directory, dynamically monitor the AOP environment, and loadPatch to implement hot update.
Next, if the application needs to be completed in the actual project, the following points should be completed:

Patch test results:

The testing environment based on the above implementation scheme includes:
Dalvik 4.0-4.4 has passed
Currently, ART 5.0 and later versions have not passed. (Native package and Jar package to be updated)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.