Incremental updates for Android clients

Source: Internet
Author: User

First we need to understand what is incremental update, incremental update popular point is that the client only need to download the new version and the old version of the differential packet, the client then the differential packet with the old version of the composite to get a new apk, in the installation of this new APK, this new apk in fact the new version, implementation updates, Note the difference between an incremental update and a hot fix.
Principle you can view: For more information, see this article Android incremental update full resolution is not a hot fix incremental

We need to know about Ndk,jni, and how to build it, and here's how we prepare for Windows
1. Download Bsdiff

  :http://www.daemonology.net/bsdiff/bsdiff-4.3.tar.gz  这里需要说明这个包中文件是做什么的:bsdiff.c和bspatch.c都是c文件,bsdiff.c是生成差分包所需要的调用的c文件,bspatch.c是差分包和旧的apk合成所需要的调用的c文件。  我们下载bsdiff这个文件可以制作bsdiff和bspatch这两个工具,bsdiff这个工具是制作差分包的,bspatch这个工具是把差分包和旧的apk合成得到新apk的(这个合成的新的apk其实就是我们服务器的新apk)  在这里我们不用制作有现成的,这是下载windows版本中这两个文件的地址http://download.csdn.net/detail/z191726501/9651809(在网上找到的,博文地址为http://blog.csdn.net/z191726501/article/details/52802104)。

2. Download bzip2 This file

:http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz,这个文件其实就是压缩使用的。

When the preparations are done, we can begin.

First you can follow my steps to start the simple feeling of the effect:
1. Prepare two apk, of course, under one item. A old.apk, one is to modify the code under the old apk to generate a new new.apk;
2. Download the addresses of the two tools mentioned above Bsdiff and Bspatch, and unzip them;
3. Place old.apk and new.apk in the directory of both the Bsdiff and Bspatch tools, open the CMD command line and enter the directory;
The 3.cmd command line generates Delta file Old-to-new.patch for old.apk and new.apk, with the command "Bsdiff old.apk new.apk Old-to-new.patch";
The 4.cmd command line uses the delta files Old-to-new.patch and old.apk to merge into the new new2.apk, with the command "BSPATHC old.apk new2.apk old-to-new.patch". , this time to generate a new APK, run this new apk, is not the same as our new.apk function

Let's talk about how to use it in a project:
first: Generate. So files
1. Configure the NDK environment, online a lot of information
2. In the bzip2 file, extract the. h and. c files, and then select the folder copy to the App/main/jni of our module.
3. Unzip the downloaded Bsdiff and copy the BSPATCH.C to our module APP/MAIN/JNI.
4. In App.gradle, the Defaultconfig directory adds

    ndk {       moduleName = ‘bsdiff‘    }

5. Build a new class Bspatch, note that bsdiff in System.loadlibrary ("Bsdiff") is the same as modulename in the previous step Bsdiff

publicclass BsPatch {    static {        System.loadLibrary("bsdiff");    }    publicstaticintbspatch(String oldApk, String newApk, String patch);}

6. Generate Bspatch header file for this class, online a lot of information, such as in terminal, switch to the project Java directory to execute "JAVAH-JNI package name", such as:

7. Modify # Include in BSPATCH.C

Jniexport jint jnicall java_com_ce_utils_bspatch_bspatch (jnienv *env, Jclass cls, jstring old, jstringNew, jstring patch) {int argc =4;    char * ARGV[ARGC]; argv[0] ="Bspatch"; argv[1] =(  char*) ((*env)->getstringutfchars (env, old, 0)); argv    [2] = (char*) ((*env)->getstringutfchars (env, new, 0)); argv    [3] = (char*) ((*env)->getstringutfchars (env, Patch, 0)); int    ret = patchmethod(argc, argv); (*env)Releasestringutfchars (env, old, argv[1]); (*env)Releasestringutfchars (ENV,New, argv[2]); (*env)Releasestringutfchars (env, Patch, argv[3]);returnRET;}

In 9.build, Rebuild Project, you can see the resulting. So file in the App\build\intermediates\ndk\debug\lib
10. We can now use this. so file, this. so file is bspatch.c. So file, we must be aware of the package name when calling the. so file when using

* * There are two ways to produce. So, the first is the use of MK, the second is the use of the NDK
The MK compilation requires writing android.mk and application.mk and configuring the output path in Gradle, if the output path is not configured in debug, but the NDK only needs to write the NDK configuration in Gradle, output in Debug * *

Step Two: Create a new project using the. so file
1. Note that new projects, of course, can also be used in the projects used above, except to differentiate how to generate. So and use the. so file to add permissions in Androidmainfest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

2. New Apkextract class:

 Public classapkextract { Public StaticStringExtract(Context context)        {context = Context.getapplicationcontext ();        ApplicationInfo applicationinfo = Context.getapplicationinfo (); String Apkpath = Applicationinfo.sourcedir;returnApkpath; } Public Static void Install(context context, String Apkpath) {Intent i =NewIntent (Intent.action_view);        I.setflags (Intent.flag_activity_new_task); I.setdataandtype (Uri.fromfile (NewFile (Apkpath)),"Application/vnd.android.package-archive");        Context.startactivity (i);    Android.os.Process.killProcess (Android.os.Process.myPid ()); }}

3. In mainactivity: Method of calling Dobspatch

privatevoiddoBspatch() {        finalnew"dest.apk");        finalnew"PATCH.patch");        BsPatch.bspatch(ApkExtract.extract(this),                destApk.getAbsolutePath(),                patch.getAbsolutePath());        if (destApk.exists())            ApkExtract.install(this, destApk.getAbsolutePath());    }

4. New Bspatch class

publicclass BsPatch {    static {        System.loadLibrary("bsdiff");    }    publicstaticintbspatch(String oldApk, String newApk, String patch);}

5. Using the tools under Windows Bsdiff and Bspatch (http://download.csdn.net/detail/z191726501/9651809, mentioned above), CMD command line to generate old.apk and new.apk delta file old-to-new.patch, command for "Bsdiff old.apk new.apk old-to-new.patch", get the differential packet, put the differential packet to the phone SD card root directory
6. Run the current apk to get old.apk, modify the code to run get new.apk, remember the difference between old.apk and the new apk, then the phone installed old.apk, the resulting patch differential packet into the phone SD card root directory, old.apk call Dobspatch ( ) method to see the effect

Incremental updates for Android clients

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.