Android App incremental update instance--Smart App Updates
introduce
What you see is an open source library for incremental updates of Android applications.
Including two parts of code: client and server.
principle
Since Android 4.1, Google has introduced incremental updates to the app.
Link: /about/versions/
Smart app updates is a new feature of Google Play that introduces a better way of delivering app updates to devices. When developers publish an update, Google Play now delivers only the bits that have changed to devices, rather than the entire APK. This makes the updates much lighter-weight in most cases, so they are faster to download, save the device's battery, and conserve bandwidth usage on users' mobile data plan. On average, a smart app update is about 1/3 the sizeof a full APK update.
The principle of incremental update is very simple. It is to binary comparison of installed apks on the mobile phone with the latest server-side apks and get differential packages. When users update the program, they only need to download the differential packages and use the differential packages and installed apks locally to synthesize the new version of the apk.
For example, Weibo V1 is currently installed on the mobile phone, with a size of 12.8MB. Now Weibo has released the latest version of V2, with a size of 15.4MB. After we checked and compared the two versions of the apk files, we found that the difference was only 3M. Then the user only needs to download a 3M differential package, use the old version of the apk and this differential package, and synthesize it to get a new version of the apk, reminding the user to install it. There is no need to download the 15.4M Weibo V2 version of the apk in the entire package.
The difference and synthesis of apk files can be implemented through the open source binary comparison tool bsdiff (Link: /bsdiff/)
Because bsdiff depends on bzip2, we also need to use bzip2 (Link:/)
bsdiff is used to generate subpackages for synthesis of files.
Next, let’s talk separately, three things need to be done.
1. On the server side, generate differential packages for these two versions of Weibo;
2. On the mobile client, use the installed old version of the apk and this differential package to combine it into a new version of the Weibo apk;
3. Verify whether the newly synthesized Weibo client file is completed. The signature is consistent with the installed client. If the same is true, the user will be prompted to install it.
Process Analysis
1 Generate differential packages
This step needs to be implemented on the server side. Generally speaking, whenever there is a new version of the apk that needs to prompt the user to upgrade, the operator needs to upload the new apk on the background management side. When uploading, the program should generate the differential packages between all the previous versions and the latest version.
For example: Your apk has released 3 versions, V1.0, V2.0, and V3.0. At this time, you have to release V4.0 in the background. Then, when you upload the latest V4.0 package on the server, the server side should immediately generate the following differential packages:
V1.0 ——> V4.0 differential package;
V2.0 ——> V4.0 differential package;
V3.0 ——> V4.0 differential package;
The ApkPatchLibraryServer project is a server-side scoring program implemented in the Java language.
Here are some simple instructions for ApkPatchLibraryServer:
Part 1.1 C
In ApkPatchLibraryServer/jni, except for the following 4:
com_cundong_utils_DiffUtils.c com_cundong_utils_DiffUtils.h com_cundong_utils_PatchUtils.c com_cundong_utils_PatchUtils.h
All from bzip.
com_cundong_utils_DiffUtils.c com_cundong_utils_DiffUtils.h
Used to generate differential packages.
com_cundong_utils_PatchUtils.c com_cundong_utils_PatchUtils.h
Used to synthesize new apk files.
Among them, com_cundong_utils_DiffUtils.c is modified from bsdiff/, and com_cundong_utils_PatchUtils.c is modified from bsdiff/.
When we need to build the C file in jni as a dynamic link library for Java call (the file name generated in the Window environment is, under Unix-like system, and under OSX).
After the Build is successful, add the dynamic link library file to environment variables for Java language call.
1.2 Java Part
Package, is a Java implementation that calls C language; Package, is a demo of the apk scoring program; Package, is a demo of the apk merge program;
Calling the genDiff() method can obtain the differential package through the new and old apk paths passed in.
Java code
/** * Class Description: apk diff tool class * * @author Cundong * @date 2013-9-6 * @version 1.0 */ public class DiffUtils { /** * Local method Compare the differences between the apk with the path oldPath and the apk with the newPath, and generate a patch package, which is stored in patchPath * * @param oldPath * @param newPath * @param patchPath * @return */ public static native int genDiff(String oldApkPath, String newApkPath, String patchPath); }
The patch() method in the call can be combined into a new apk through the old apk and the differential package.
Java code
/** * Class Description: APK Patch Tool Class * * @author Cundong * @date 2013-9-6 * @version 1.0 */ public class PatchUtils { /** * native method * Use the apk with the path oldApkPath and the patchPath patchPath to synthesize the new apk and store it in newApkPath * @param oldApkPath * @param newApkPath * @param patchPath * @return */ public static native int patch(String oldApkPath, String newApkPath, String patchPath); }
2. Use old version of apk and differential packages to synthesize new apk on the client side
It needs to be implemented on the mobile client, and the ApkPatchLibrary project encapsulates this process.
Part 2.1 C
All files in the ApkPatchLibrary/jni/bzip2 directory are from the bzip2 project.
ApkPatchLibrary/jni/com_cundong_utils_PatchUtils.c and ApkPatchLibrary/jni/com_cundong_utils_PatchUtils.c implement the file merge process, where com_cundong_utils_PatchUtils.c is modified from bsdiff/.
We need to compile a file with NDK, and the generated so file is located under libs/armeabi/. Other Android projects can use this file to synthesize apk.
2.2 Java Part
Package, for calling Java implementations of C language;
Calling the patch() method can be combined into a new apk through the old apk and the differential package.
Java code
/** * Class Description: APK Patch Tool Class * * @author Cundong * @date 2013-9-6 * @version 1.0 */ public class PatchUtils { /** * native method * Use the apk with the path oldApkPath and the patchPath patchPath to synthesize the new apk and store it in newApkPath * @param oldApkPath * @param newApkPath * @param patchPath * @return */ public static native int patch(String oldApkPath, String newApkPath, String patchPath); }
3. Verify the newly synthesized apk file
After the new package is completed, the apk package synthesized by the client is also required to perform MD5 or SHA1 verification between the latest version of the apk package. If the verification code is inconsistent, it means that there is a problem with the synthesis process and the newly synthesized package will not be installed.
Things to note
The prerequisite for incremental update is that on the mobile client, we can read the source apk after the current application is installed. If the source apk cannot be obtained, incremental updates cannot be performed.
In addition, if your application is not large, such as only 2 or 3M, then there is no need to use incremental updates. Incremental updates are suitable for situations where the apk package is relatively large, such as game clients.
GitHub address
GitHub:/cundong/SmartAppUpdates
Some instructions
The source code contains the following files:
: implemented in Java language, differential package project generated by server-side;
: The apk synthesis library used by the client;
: Refer to the demo of ApkPatchLibrary Library. Taking the upgrade of Sina Weibo client as an example, assuming that the phone is V4.5.0 installed, and the latest version is V4.5.5, users need to upgrade from V4.5.0 to V4.5.5.
: Used for testing, old version of Weibo client, and new and old Sina Weibo differential packages generated using ApkPatchLibraryServer.
Through this article, I hope it can help friends in need. Thank you for your support to this site!