Android App Incremental Update and example code _android

Source: Internet
Author: User

Android App Incremental update instance--smart app Updates

Introduced

What you see is an open Source library for incremental updates to the Android app.

Includes the client, the service end two parts of code.

Principle

Since the start of 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 the Google play that introduces a better way of the delivering of app updates to devices. When developers publish a update, Google play now delivers only the bits this have changed to devices, rather than the EN Tire APK. This makes the updates much lighter-weight in most cases, so they are faster to download, save the device ' s battery, and C Onserve bandwidth usage on the users ' mobile data plan. On average, a smart app update are about 1/3 the sizeof a full APK update.

The principle of incremental update is very simple, that is, the mobile phone has been installed APK and server-side of the latest APK binary contrast, and get the difference package, users update the program, only need to download the difference package, and in the local use of differential packets and installed APK, the synthesis of new apk.

For example, the current phone has a micro Bo V1 installed, Size of 12.8MB, now Weibo released the latest version of V2, the size of 15.4MB, we have two versions of the APK file, found that the difference is only 3 m, then users only need to download a 3M difference packet, using the old version of APK and this differential package, the synthesis of a new version of APK, to remind users to install it, do not need The whole package downloads 15.4M Weibo V2 version apk.

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 relies on bzip2, we also need to use BZIP2 (link:http://www.bzip.org/downloads.html)

In Bsdiff, BSDIFF.C is used to generate a packet, BSPATCH.C for a composite file.

Next, we say separately, we need to do 3 things.

1. On the server side, generate these two versions of the micro-BO differential packet;

2. In the mobile phone client, the use of the old version of the APK and this differential package, together to become a new version of micro-Bo apk;

3. Verify the new synthetic microblogging client file is completed, the signature and installed client consistent, such as consistent, prompt user installation.

Process Analysis

1 Generating Difference Packets

This step needs to be implemented on the server side, in general, whenever there is a new version of apk need to prompt the user to upgrade, you need operators in the backend management to upload new apk, upload should be generated by the program before all the old version and the latest version of the difference package.

For example: Your APK has released 3 editions, V1.0, V2.0, V3.0, when you want to post 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 packets:

v1.0--> V4.0 differential packet;

v2.0--> V4.0 differential packet;

v3.0--> V4.0 differential packet;

The Apkpatchlibraryserver project is a server-side look-up program implemented for the Java language.

Here are some simple instructions for apkpatchlibraryserver:

1.1 C Section

Apkpatchlibraryserver/jni, in addition to 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 a differential packet.

COM_CUNDONG_UTILS_PATCHUTILS.C Com_cundong_utils_patchutils.h

Used to synthesize a new apk file.

Among them, 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 in JNI as a dynamic link library, For Java calls (the file name generated under the window environment is the libapkpatchlibraryserver.dll,unix-like system under LIBAPKPATCHLIBRARYSERVER.SO,OSX for Libapkpatchlibraryser Ver.dylib).

After the build succeeds, the dynamic link library file is added to the environment variable for the Java language invocation.

1.2 Java Section

Com.cundong.utils package, for the Java implementation of the call C language, Com.cundong.apkdiff packet, for the APK look-up program demo, Com.cundong.apkpatch package, apk merge program demo;

Call the Gendiff () method in Com.cundong.utils.DiffUtils to get the difference packet by passing in the new and old apk path.

Java code

/** 
 * Class Description:   apk diff Tool class 
 * 
 * @author cundong 
 * @date  2013-9-6 * 
 @version 1.0 
* * The public class Diffutils { 
  
  /** 
   * Local method compares the path to OldPath apk and NewPath apk and generates patch packets, which are stored in Patchpath 
   * 
   * @param oldpath 
   * @param newpath * 
   @param patchpath 
   * @return/public 
  static native int Gendiff (String Oldapkpath, String Newapkpath, String patchpath); 
 

Call the patch () method in Com.cundong.utils.PatchUtils, which can be combined into a new apk through the old apk and the differential packet.

Java code

/** 
 * Class Description:  APK Patch Tool Class 
 * 
 * @author cundong 
 * @date  2013-9-6 
 * @version 1.0 
* * The public class Patchutils { 
 
  /** 
   * Native Method 
   * uses the APK patch package with the path Oldapkpath and the Patchpath, and synthesizes the new apk, and stored in Newapkpath 
   * @param oldapkpath 
   * @param newapkpath 
   * @param patchpath 
   * @return 
  * * public static native int patch (string oldapkpath, String Newapkpath, 
      string patchpath); 
 

2. Use the old version of APK and differential packets, in the client synthesis of new APK

Needs to be implemented on the mobile client, Apkpatchlibrary engineering encapsulates the process.

2.1 C Section

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 implements the process of merging files, where COM_CUNDONG_UTILS_PATCHUTILS.C is modified from BSDIFF/BSPATCH.C.

We need to compile a libapkpatchlibrary.so file with NDK, the resulting so file is located under libs/armeabi/, and other Android projects can use the libapkpatchlibrary.so file to synthesize apk.

2.2 Java Section

Com.cundong.utils package, for the Java implementation of the call C language;

Call, com.cundong.utils.PatchUtils in the patch () method, can be combined into the new apk through the old apk and the differential packet.

Java code

/** 
 * Class Description:  APK Patch Tool Class 
 * 
 * @author cundong 
 * @date  2013-9-6 
 * @version 1.0 
* * The public class Patchutils { 
 
  /** 
   * Native Method 
   * uses the APK patch package with the path Oldapkpath and the Patchpath, and synthesizes the new apk, and stored 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 and into, also need to the client-made APK package and the latest version of the APK package MD5 or SHA1 checksum, if the checksum code is inconsistent, indicating that the synthesis process is problematic, the newly synthesized package will not be installed.

Attention matters

The prerequisite for incremental updates is that the mobile client allows us to read the source apk of the current application installation, and if the source apk is not obtained, the incremental update cannot be made.

In addition, if your application is not very large, such as only 2, 3M, then there is no need to use incremental update, incremental update for the APK package is relatively large, such as game client.

GitHub Address

Github:https://github.com/cundong/smartappupdates

Some notes

The source code contains the following files:

1.apkpatchlibraryserver:java language Implementation, server-side generation of the difference package project;

2.ApkPatchLibrary: APK synthesis library used by clients;

3.ApkPatchLibraryDemo: Quote The demo of Apkpatchlibrary Library, take Sina Weibo client upgrade for example, Assuming the phone is installed on the V4.5.0, the latest version is V4.5.5, users need to upgrade from V4.5.0 to V4.5.5.

4.TESTAPK: Used for testing, the old version of the microblogging client, and the use of apkpatchlibraryserver generated by the new and old Sina Weibo differential packet.

Through this article, I hope to help to have the need of friends, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.