Android App incremental update details and example code

Android App incremental update instance -- smart app updates

introduce

What you see is an open source library for incremental updates to Android applications.

Including client and server code.

Principle

Since Android 4.1, Google has introduced incremental updates to applications.

  Link: http://developer.android.com/about/versions/jelly-bean.html

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 compare the installed APK on the mobile phone with the latest APK on the server and get the differential package. When updating the program, users only need to download the differential package and use the differential package locally with the installed APK to synthesize the new version of apk.

For example, microblog V1 with a size of 12.8mb has been installed in the current mobile phone. Now microblog has released the latest version of V2 with a size of 15.4mb. After we check and compare the scores of the two versions of APK files, we find that the difference is only 3M, so users only need to download a 3M differential package, combine the old version of APK with this differential package to obtain a new version of APK, and remind users to install it, There is no need to download the 15.4m microblog V2 APK in the whole package.

The difference and synthesis of APK files can be realized through the open source binary comparison tool bsdiff (link: http://www.daemonology.net/bsdiff/ )

Because bsdiff depends on bzip2, we also need bzip2 (link: http://www.bzip.org/downloads.html )

Bsdiff, bsdiff C is used to generate subcontracting query, bspatch C is used to compose files.

Next, let's talk about three things separately.

  1. On the server side, generate the differential package of the two versions of microblog;

  2. In the mobile client, use the installed old version APK and this differential package to synthesize a new version of microblog APK;

  3. Verify whether the newly synthesized microblog client file is completed, and the signature is consistent with the installed client. If it is consistent, prompt the user to install it.

Process analysis

1 generate differential subcontracting

This step needs to be implemented on the server side. Generally speaking, whenever a new version of APK 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 difference between all previous versions and the latest version.

For example, your APK has released 3 versions, v1 0、V2. 0、V3. 0. At this time, you need to release v4.0 in the background 0, then, when you upload the latest v4 0 package, the server should immediately generate the following differential packages:

  V1. 0 ――> V4. Differential subcontracting of 0;

  V2. 0 ――> V4. Differential subcontracting of 0;

  V3. 0 ――> V4. Differential subcontracting of 0;

Apkpatchlibraryserver project is a server-side scoring program implemented in Java language.

The following is a brief description of apkpatchlibraryserver:

1.1 Part C

In apkpatchlibraryserver / JNI, except for the following four:

  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 subcontracting.

  com_ cundong_ utils_ PatchUtils. c com_ cundong_ utils_ PatchUtils. h

Used to compose new APK files.

Where, com_ cundong_ utils_ DiffUtils. C modified from bsdiff / bsdiff c,com_ cundong_ utils_ PatchUtils. C modified from bsdiff / bspatch c。

We need to output the C file and build in JNI as a dynamic link library for Java to call (the file name generated in window environment is libapkpatchlibraryserver.dll, libapkpatchlibraryserver.so in UNIX like system and libapkpatchlibraryserver.dylib in OSX).

After build is successful, add the dynamic link library file to the environment variable for Java language call.

1.2 Java part

  com. cundong. Utils package, a java implementation for calling C language; com. cundong. Apkdiff package, which is the demo of APK sub search program; com. cundong. Apkpatch package, which is the demo of APK merge program;

Call com cundong. utils. The gendiff () method in diffutils can obtain differential packets through the incoming old and new APK paths.

Java code

Call com cundong. utils. The patch () method in patchutils can be synthesized into a new APK through the old APK and differential packet.

Java code

  2. Use the old APK and differential package to synthesize the new APK on the client

It needs to be implemented on the mobile client. Apkpatchlibrary project encapsulates this process.

2.1 Part C

All files in the apkpatchlibrary / JNI / bzip2 directory are from the bzip2 project.

  ApkPatchLibrary/jni/com_ cundong_ utils_ PatchUtils. c、ApkPatchLibrary/jni/com_ cundong_ utils_ PatchUtils. C realize the file merging process, in which com_ cundong_ utils_ PatchUtils. C modified from bsdiff / bspatch c。

We need to compile a libapkpatchlibrary with NDK So file. The generated so file is located under LIBS / armeabi /, which can be used by other Android projects So file to synthesize APK.

2.2 Java part

  com. cundong. Utils package, a java implementation for calling C language;

Call, com cundong. utils. The patch () method in patchutils can be synthesized into a new APK through the old APK and differential packet.

Java code

  3. Verify the newly synthesized APK file

After the new package is combined, MD5 or SHA1 verification needs to be performed on the APK package synthesized by the client and the latest version of APK package. If the verification code is inconsistent, it indicates that there is a problem in the synthesis process, and the newly synthesized package will not be installed.

Precautions

The prerequisite for incremental update is that the mobile client allows us to read the source APK after the current application is installed. If the source APK cannot be obtained, incremental update cannot be carried out.

In addition, if your application is not very large, such as only 2 and 3M, it is not necessary to use incremental update. Incremental update is applicable to the case of large APK package, such as game client.

GitHub address

  GitHub: https://github.com/cundong/SmartAppUpdates

Some notes

The source code contains the following files:

  1. Apkpatchlibraryserver: it is implemented in Java language, and the server side generates differential subcontracting projects;

  2. Apkpatchlibrary: APK composition library used by the client;

  3. Apkpatchlibrarydemo: reference the demo of apkpatchlibrary library. Take the upgrade of sina Weibo client as an example, assuming that v4.0 is installed on the mobile phone 5.0, the latest version is v4.0 5.5, the user needs to start from v4 Upgrade from 5.0 to v4.0 5.5。

  4. Testapk: the old version of Weibo client used for testing, and the old and new Sina Weibo differential subcontracting generated using apkpatchlibraryserver.

Through this article, I hope to help friends in need. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>