Since Android 4.1, Google has introduced incremental updates to applications.
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, that is, compare the local apk with the latest version on the server side and get the difference package. You only need to download the difference package when updating the App. For example, the latest versions of Sina Weibo V3.5, 12.8 MB, and Sina Weibo V4.0 and 15.4 MB are installed. After comparison, the differences between the two versions are only 7 and 8 MB, at this time, the user only needs to download a 7-8 m differential package, and does not need to download the full package to download the new version of Weibo client 15.4M. After downloading the differential package, use the old apk + differential package on the mobile phone end to synthesize the latest Weibo version V4.0 and remind the user to install the package.
- Take the upgrade of Sina Weibo client as an example. Assume that V3.5 is installed on the mobile phone and the latest version is V4.0. You need to upgrade from V3.5 to V4.0.
After figuring out the principle, we need to solve two problems:
1. How to compare two versions of apk to generate different packages;
2. How to use the old apk + differential package to generate a new apk;
(1) generate a differential package
This step needs to be implemented on the server side. Generally, after an apk has a new version, you need to upload a new apk to the background management terminal. During the upload, you should generate a different package for each old version and the latest version.
Assume that your apk has released three versions: 1.0, 2.0, and 3.0. At this time, you need to release 4.0 in the background. When you upload the apk, you should generate
1.0 --> 4.0 difference packages;
2.0 --> 4.0 difference packages;
3.0 --> 4.0 difference packages;
Choose to use this open-source binary comparison tool for implementation:
Http://www.daemonology.net/bsdiff/
Download the file to bsdiff-4.3.tar.gz.
Here, bsdiff. c is the code for binary file comparison; bspatch. c is the code for binary file synthesis;
We will use this bsdiff to generate two apk patch packages, and use bspatch. c to synthesize the old apk and patch packages;
Use bsdiff, bspatch, also need to bzip2: http://www.bzip.org/downloads.html
Download To get: bzip2-1.0.6.tar.gz.
You need to use the following 13 files in bzip2-1.0.6.tar.gz (some of them may be unnecessary, and I copied them all ):
Copy the 13 files to the jni directory. Next, we will call bsdiff to generate a differential package and call bspatch to synthesize a new package.
I did it on Mac and used java for development. I used jni to call C Programs (bsdiff and bzip2 ).
You can call the genDiff () method in com. cundong. utils. DiffUtils. java to obtain the patchPath of the new (newApkPath) Old (oldApkPath) apk ).
1 |
public static native int genDiff(String oldApkPath, String newApkPath,String patchPath); |
Call the patch () method in com. cundong. utils. PatchUtils. java to obtain the new apk (newApkPath) through the old apk (oldApkPath) and the difference package (patchPath ).
1 |
public static native int patch(String oldApkPath, String newApkPath,String patchPath); |
(2) Use the old apk + differential package to synthesize the new apk on the client
The difference package has been generated on the server. We only need to notify the user of an update on the client, and then ask the user to download the difference package. After the download is successful,
Use the local apk and the difference package to generate the new apk.
This step needs to be developed in the Android Application.
1. first, NDK compiles *. so, the APKPatch project is responsible for generating libapkpatch so, and the generated so file is located in APKPatch/libs/libapkpatch. so, Other Android projects can use this so file to synthesize the apk.
2. Call the so file.
Any Android project that uses this so file and copies it to libs \ armeabi can call the patch () method to convert it to the old apk + differential package.
The test project in the attachment is an example of calling the so file.
- Note 1. after the new package is merged, you also need to perform MD5 or SHA1 verification on the merged apk package and the latest apk package. If the verification code is inconsistent, it indicates that there is a problem in the synthesis process, new merged packages cannot be installed.
2. the premise for successful incremental upgrade is that the user's mobile phone must have an apk that allows you to copy and is consistent with the version used by your server for the differential version. This will exist, for example, the built-in apk of the system cannot be obtained and incremental upgrades cannot be performed. For some of the apk versions that are consistent with your differential version but have modified the content (such as the cracked apk version), incremental upgrades cannot be performed, to prevent merging patch errors, it is best to verify the old apk version before merging the patch to ensure the consistency of the basic package.