Android HotPatch Series-Patch the release pack

Source: Internet
Author: User

In the default debug package, the class is not confused, so patch writing is relatively simple, but the application at the time of release is the release package, the code will be confused, at this time, class name, field Name,method name will be confused, How do you write patches at this time?

In the release package, there are mapping.txt below the build/outputs/mapping/release/, which records one by one correspondence between the previous class and the confusing class (so called Mapping.txt), So you should keep the Mapping.txt file after each release so that you can write patches later.

In the dexposedexamples of the Https://github.com/fengcunhan/Hotpatch-Sample project, a new test class is defined below:

?
1234567891011 packagecom.taobao.model;publicclassTest {    publicString getHello(inta){        if(a==0){            return"PatchSuccess";        }        return"hello";    }}

This class is used in the Testfragment, after the release of the version, found the need to change the business logic to a<=0 all return to Patchsuccess, and then the pit Dad, can only publish a patch to do this thing, there are two ways of patch writing can achieve the above purpose.

1.xc_methodhook rewrite the Beforehookedmethod method in this, modify the parameters of the incoming method, if a<0, then set the a==0, do not change the original method of logic, you can achieve the purpose.

2. Replace the original method. Xc_methodreplacement, rewrite the Replacehookedmethod method to implement the logic you want in this method.

Here's a second way to write an example. To write a patch to replace the Gethello () method in the test class, first locate the test class under the release package, and then replace the Gethello method. Found in the Mapping.txt file the test class and the corresponding confusion of the new class name and method name:

?
12 com.taobao.model.Test -> com.taobao.a.a://类混淆了    java.lang.String getHello(int) -> a //方法名也混淆了

Testpatch is written as follows:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344 import android.util.Log;importcom.taobao.android.dexposed.DexposedBridge;importcom.taobao.android.dexposed.XC_MethodReplacement;/** * Created by renxuan on 15/9/8. */publicclassTestPatch implementsIPatch {    privatestaticfinalString TAG="TestPatch";    @Override    publicvoidhandlePatch(finalPatchParam patchParam) throwsThrowable {        {            Class<?> cls = null;            try{            //根据混淆以后的类来找到想要Patch的class                cls= patchParam.context.getClass().getClassLoader().loadClass("com.taobao.a.a");            catch(Exception e) {                e.printStackTrace();                return;            }            Log.e(TAG, "cls:"+ cls);            /**             * 修改逻辑,原先是==0的时候返回Patch success,现在改成<=0都是返回Patch Success             *注意添加的int.class,因为getHello是有参数的,如果没有传int.class,是找不到对应方法的。不知道为什么的人,可以去看看反射先             * */            DexposedBridge.findAndHookMethod(cls, "a",int.class,newXC_MethodReplacement() {                @Override                protected Object replaceHookedMethod(MethodHookParam methodHookParam) throwsThrowable {                    Log.e(TAG, "methodHookParam:"+ methodHookParam.method.getName());                    intmethodArgsLength=methodHookParam.args.length;                    if(methodArgsLength>0){                        inta=(int)methodHookParam.args[0];                        if(a<=0){                            return"PatchSuccess";                        }                    }                    return"hello";                }            });         }    }}

Attention:

1. If the test is under Debug, because test class is not confused, the class name on this side of the patch package should be com.taobao.model.Test, and the method name should be Gethello.

The signature of the 2.release package must be the same as the patch signature (online), without this detection, your application is very insecure, malicious attackers can load a patch in random, the consequences unthinkable.

3. There is a problem can join HotPatch QQ Group: 254384686

Android HotPatch Series-Patch the release pack

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.