Android HotPatch Series-Patch the release pack

Source: Internet
Author: User

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:

Package Com.taobao.model;public class Test {public String gethello (int a) {if (a==0) {return ' PATCHSU        Ccess ";    } 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:

Com.taobao.model.Test-A com.taobao.a.a://class confused with java.lang.String Gethello (int), A//method name also confused

Testpatch is written as follows:

import android.util.log;import com.taobao.android.dexposed.dexposedbridge;import  com.taobao.android.dexposed.xc_methodreplacement;/** * created by renxuan on 15/ 9/8. */public class testpatch implements ipatch {    private  static final string tag= "Testpatch";     @Override      public void handlepatch (Final patchparam patchparam)  throws Throwable  {        {             Class<?> cls = null;             try {            // Find the class               that you want to patch based on the class that was confused   cls= patchparam.conteXt.getclass (). getClassLoader (). LoadClass ("com.taobao.a.a");             } catch  (exception e)  {                 e.printstacktrace ();                 return;        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E ( tag,  "CLS:"  + cls);             /**              *  Modify Logic, originally ==0 back to patch  success, now change to <=0 is return patch success              * note the added Int.class, because Gethello is a parameter, if there is no Int.class, the corresponding method is not found. Do not know why the person can go to see reflection first              * */             dexposedbridge.findandhookmethod (cls,  "A", int.class,new xc_methodreplacement ()  {                  @Override                  protected  Object replacehookedmethod (Methodhookparam methodhookparam)  throws Throwable {                     &NBSP;LOG.E (tag,  "Methodhookparam:"  + methodhookparam.method.getname ());                     int  methodArgsLength=methodHookParam.args.length;                      if (methodargslength>0) {                         int a= (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.