In the hands of the work, a set of internal solutions, to the online apk patch, directly fix the bug, without the client upgrade, there are serious online bugs, find problems, write a patch, push the line to solve the problem, used all agreed. Some time ago, they finally put this set of things the Android side open source, dexposed, concrete can see detailed project introduction. Its function is to be able to load the class, replace the original apk inside the method of a class, or before the method, what to do after the method, such as before the method is executed, modify the parameters of the passed method, after the method executes, modify the return value of the method, or completely replace the original method.
There is a sample in the project that describes how to load an external APK package and then replace the method that hosts the class inside the APK. In fact, this has already done 80% of the work, I just do the work of the patch get from the local load changed to go to the server to get, and add the patch APK checksum (MD5 and signature information).
The HotPatch workflow is:
1. Implement an interface to get patch information from the server side (upload the local client version and other information, the server based on this information to determine whether there is a corresponding patch package), and inject this information to Hotpatchmanager.
2. Detect if the client can support it. (Android L and Android M) currently not supported, minimum support 2.3
3. If the client supports it, call the first step injected patch package to get the class and get the corresponding patch information. Download the patch apk file.
4. Calibration. Get the downloaded patch apk MD5 signature information and the MD5 value of the patch information given by the server, and then verify that the patch apk and host apk signatures are identical. The checksum is to prevent the patch apk from being tampered with, and if load is in a third-party patch, it is snake into. Must not remove the calibration, can not be removed, can not be removed, the important thing to say three times!!!
5. Verification is successful, just load Patch apk.
Here is the time to paste the code:
Public class hotpatchmanager { private static hotpatchmanager instance=new hotpatchmanager (); private hotpatchmanager () { } public static hotpatchmanager getinstance ( Ipatchinforequest request) { requestmanager.getinstance (). Setipatchinforequest (Request); return instance; } public boolean init (FINAL CONTEXT CTX) { boolean issupport = dexposedbridge.candexposed ( CTX); if (issupport) { new thread (new runnable () { @Override public void run () { check (CTX); } }). Start (); } return isSupport; } private void check (FINAL CONTEXT CTX) { requestmanager manager = requestmanager.getinstance (); manager.setipatchinforequest (Requestmanager.getinstance (). Getipatchinforequest ());     &NBsp; manager.reqeust (New requestmanager.onrequestcallbacklistener () { @Override public void onrequest (Final patchinfo info) { if (null != info) { string apkpath = utils.getcacheapkfilepath (ctx, Info.apkfileurl); file file = new file (Apkpath); if (file.exists ())  {   &NBsp; loadpath (Info, ctx, apkpath); } else { downloadmanager.getinstance (). DownloadFile (ctx, info.apkfileurl, new Downloadmanager.onfiledownload () { @ override public void filedownload (String apkfilepath) { loadpath (Info, ctx, apkfilepath); } }); } } } } ); } private void loadpath (PatchInfo info, context ctx, string apkfilepath) { if ( Utils.issignequal (Ctx, apkfilepath) && textutils.equals (info.apkMd5, Utils.getmd5byfile (New file (Apkfilepath))) { patchmain.load (ctx, apkfilepath, null); } }}
A good example of client and server implementations has been provided. The URLs were:
Client Example implementation: Https://github.com/fengcunhan/Hotpatch-Sample
Simple Server implementation: Https://github.com/fengcunhan/Hotpatch-SimpleServer
To run an example step:
1. Modify the infoin the Defaultpatch class. The IP in the Patchapkurl URL is replaced with the IP of your server, and the code is deployed to Tomcat.
2. Import the project in Hotpatch-sample into Android studio.
3. Open the URL http://server IP/pathserver server port, patchsample under build/outputs/apk/app-debug.apk upload to the server, must not change the name.
4. Modify the IP and port of the Hosturl in defaultpatchinforequest for your server-side IP and port.
5. Run Dexposedexamples project, click Check HotPatch, if "the dialog is shown from Patch apk", congratulations, successfully run the demo.
The patch in the demo is simply an example of how a confusing class can write patches and so on, which will be described later in one by one. Please pay attention to this hotpatch series.
The server implementation of the demo is very slag. Please server side big God do not spray, can fork, do not fuck.
Android HotPatch Series-Project Introduction