Android开源:bsdiff - 增量更新

rkni4633 8年前
   <pre>  <code class="language-java">title: Android NDK之增量更新</code></pre>    <h3>1.增量更新使用到的库bsdiff和bzip2</h3>    <p>bsdiff库是一个开源的二进制差分工具,通过对比Apk的二进制,从而进行差分包的生成。</p>    <p>bsdiff库可以参考官网: bsdiff</p>    <p>下载地址:已经将用到的bsdiff和bzip上传到 百度云 。</p>    <h3>2.AS创建项目,并导入头文件</h3>    <p>本项目使用Android Studio,通过cmake进行编译。</p>    <p>新建一个工具类,进行差分包的生成及合并</p>    <p>Diffutils</p>    <pre>  <code class="language-java">public class Diffutils {        static {          System.loadLibrary("native-lib");      }          /**       * @param oldPath 旧的安装包路径       * @param newPath 新的安装包路径       * @param patchPath 差分包路径       * @return 生成的结果       */      public static native int generateDiffApk(String oldPath, String newPath, String patchPath);        /**       * @param oldPath 旧的安装包路径       * @param newPath 新的安装包路径       * @param patchPath 差分包路径       * @return 生成的结果       */      public static native int mergeDiffApk(String oldPath, String newPath, String patchPath);  }</code></pre>    <p>导入bsdiff和bzip2的头文件以及.c文件</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a50de4519371313f26936c75edefa51f.png"></p>    <p>将生成的.cpp文件改成.c文件。为什么要修改成.c文件?主要是因为c和c++对void * malloc这个函数编译不同,c中不用将结果强制转换成类型* ,而c++则不同,它必须要将结果强制转换成类型*。我们使用到的bsdiff库和bzip2库好多地方都使用了malloc函数,如果使用c++编译会报大量的错误,因此我们采用c编译。</p>    <p>修改了cpp文件我们不要忘记在CMakeList中进行更新以及导入新的文件</p>    <p>CMakeList.txt</p>    <pre>  <code class="language-java"># Sets the minimum version of CMake required to build the native  # library. You should either keep the default value or only pass a  # value of 3.4.0 or lower.    cmake_minimum_required(VERSION 3.4.1)    # Creates and names a library, sets it as either STATIC  # or SHARED, and provides the relative paths to its source code.  # You can define multiple libraries, and CMake builds it for you.  # Gradle automatically packages shared libraries with your APK.    add_library( # Sets the name of the library.               native-lib                 # Sets the library as a shared library.               SHARED                 # Provides a relative path to your source file(s).               # Associated headers in the same location as their source               # file are automatically included.               src/main/cpp/native-lib.c )  #include src/main/cpp/include目录下的所有文件  include_directories(src/main/cpp/include)    # Searches for a specified prebuilt library and stores the path as a  # variable. Because system libraries are included in the search path by  # default, you only need to specify the name of the public NDK library  # you want to add. CMake verifies that the library exists before  # completing its build.    find_library( # Sets the name of the path variable.                log-lib                  # Specifies the name of the NDK library that                # you want CMake to locate.                log )    # Specifies libraries CMake should link to your target library. You  # can link multiple libraries, such as libraries you define in the  # build script, prebuilt third-party libraries, or system libraries.    target_link_libraries( # Specifies the target library.                         native-lib                           # Links the target library to the log library                         # included in the NDK.                         ${log-lib} )</code></pre>    <h3>3. 差分方法以及合并方法的实现</h3>    <p>差分的方法在bsdiff.c的main函数已经实现,但是我们为了区分差分方法和合并方法我将main函数修改成generateDiffApk。同理合并方法在bspatch中已经实现,我将其main函数修改成mergeDiffApk</p>    <p>native-lib.c</p>    <pre>  <code class="language-java">#include <jni.h>    #include "include/bsdiff.c"  #include "include/bspatch.c"    JNIEXPORT jint JNICALL  Java_com_nick_bsdiff_Diffutils_generateDiffApk(JNIEnv *env, jclass type, jstring oldPath_,                                                 jstring newPath_, jstring patchPath_) {      int argc = 4;      char *argv[argc];      argv[0] = (char *) "bspatch";      argv[1] = (char *) (*env)->GetStringUTFChars(env, oldPath_, 0);      argv[2] = (char *) (*env)->GetStringUTFChars(env, newPath_, 0);      argv[3] = (char *) (*env)->GetStringUTFChars(env, patchPath_, 0);        jint result = generateDiffApk(argc, argv);        (*env)->ReleaseStringUTFChars(env, oldPath_, argv[1]);      (*env)->ReleaseStringUTFChars(env, newPath_, argv[2]);      (*env)->ReleaseStringUTFChars(env, patchPath_, argv[3]);      return result;  }    JNIEXPORT jint JNICALL  Java_com_nick_bsdiff_Diffutils_mergeDiffApk(JNIEnv *env, jclass type, jstring oldPath_,                                              jstring newPath_, jstring patchPath_) {        int argc = 4;      char *argv[argc];      argv[0] = (char *) "bspatch";      argv[1] = (char *) (*env)->GetStringUTFChars(env, oldPath_, 0);      argv[2] = (char *) (*env)->GetStringUTFChars(env, newPath_, 0);      argv[3] = (char *) (*env)->GetStringUTFChars(env, patchPath_, 0);        printf("old apk = %s \n", argv[1]);      printf("patch = %s \n", argv[3]);      printf("new apk = %s \n", argv[2]);        jint result = mergeDiffApk(argc, argv);        (*env)->ReleaseStringUTFChars(env, oldPath_, argv[1]);      (*env)->ReleaseStringUTFChars(env, newPath_, argv[2]);      (*env)->ReleaseStringUTFChars(env, patchPath_, argv[3]);      return result;  }</code></pre>    <h3>4.效果展示</h3>    <p>旧版本:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/50d757819c2262540c5ec15958466ef3.jpg"></p>    <p>生成的差分包(app3.patch):</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/0136d8d2cf7a49be8ca098f0e753ba37.jpg"></p>    <p>合并后的新的安装包(app1): <img src="https://simg.open-open.com/show/0136d8d2cf7a49be8ca098f0e753ba37.jpg"></p>    <p style="text-align:center">安装后:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d0a851369414a90ccde672b5dc388ac9.jpg"></p>    <h3> </h3>    <p>来自:http://www.cnblogs.com/mc-ksuu/p/6481730.html</p>    <p> </p>