当前位置:网站首页>Hot fix sophix multi-channel patch solution

Hot fix sophix multi-channel patch solution

2022-06-25 00:26:00 a10615

1 background

lately , hold Sophix Integrated into a multi-channel project . but , The first time I used it, I encountered a pit . The code uses BuildConfig.FLAVOR To determine the current channel , Such as :

There are two channels :taobao and tianmao
Test.java class BuildConfig.FLAVOR:

if ("taobao".equals(BuildConfig.FLAVOR)){
    toast("abc");
} else {
    toast("hello");
}

Now we need to ”abc” Switch to ”xyz”.

After replacement , Want to use hot fixes to achieve . About the steps :

  1. When using old packages mapping, Generate new package ( You can generate packages for only one channel )
  2. Old and new packages using the same channel , Generate patch file
  3. Upload patches , Grayscale Publishing

In grayscale Publishing , Find out taobao channel APP Fixed .

and ,tianmao Channel APP, Because they belong to the same version , It will definitely be patched . but , little does one think ,toast The content of , Not the original “hello”, But it became “abc”.

If you use tianmao Channel to generate patches , that tianmao normal . but taobao After the patch is completed , But it became “hello”.

2 Problem solving path

Later, I asked Ali for technical support , Technical support said that the method of using white list . Because I didn't find mapping Obfuscated mapping relationship under file , I just put BuildConfig The original package name and class name of the class are added to the white list :
com.ali.six.BuildConfig

result , It is the same as the original effect . Technical support also provides ideas : Is it compiled into constants .
After decompilation, it is found that : Before compiling BuildConfig, After compiling, there is no , The code uses BuildConfig A place worth , Are replaced by constant values .

see BuildConfig After the class , It is found that this kind is final class , All internal variables are member variables , And are all public static final modification . such , After compiling , Where they are used , It must become a constant value .

Patch , It is generated by comparing the differences between old and new packages . therefore , Modified code , It must be within the range of differences , And in this code BuildConfig.FLAVOR, Always a constant value , It is determined by the channel that generates the patch .

such , It explains the problems encountered before .

Last , Use version upgrade to update .

3 Solution

But my heart is not willing to , So like Sophix, It can't be used ? These ideas provided through technical support , Still found a solution : hold BuildConfig Encapsulate into a class MyBuildConfig, Let all the original use to BuildConfig The place of , Use this class instead ; And add this class to the white list . specific working means :
1、 The new class MyBuildConfig( Interface is OK , As the case may be )
2、 Use expressions , Or method , Provide external call interface , Such as :MyBuildConfig.IS_FLAVOR_TAO_BAO. This class , stay taobao After channel compilation , This class will only leave a = “taobao”.equals(“taobao”) This expression .

public class MyBuildConfig {
    
    /** *  Direct constants are all encapsulated in classes , Only methods are provided externally 、 Or variables of expressions  *  No directly usable constants are provided , as a result of : * public static final String FLAVOR = BuildConfig.FLAVOR; *  Compile time ,BuildConfig.FLAVOR Is a constant value , All use MyBuildConfig.FLAVOR The place of , Will also become a constant value , such , Or does it affect the code  */
    private static final String FLAVOR_TAO_BAO = "taobao";
    private static final String FLAVOR_TIAN_MAO = "tianmao";
    public static final boolean IS_FLAVOR_TAO_BAO = FLAVOR_TAO_BAO.equals(BuildConfig.FLAVOR);
}

3、 According to the baseline package mapping, find MyBuildConfig The path of the class after confusion , Add to the white list , Such as :com.ali.six.c
4、 Generate patch file

such , This ensures that the logic of the code will be affected due to different channels . If , The place where the code is fixed has nothing to do with the channel , Then you don't have to use MyBuildConfig

原网站

版权声明
本文为[a10615]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210547079395.html