Android 增量更新

title: Android NDK之增量更新

1.增量更新使用到的库bsdiff和bzip2

bsdiff库是一个开源的二进制差分工具,通过对比Apk的二进制,从而进行差分包的生成。
bsdiff库可以参考官网:bsdiff
下载地址:已经将用到的bsdiff和bzip上传到百度云


2.AS创建项目,并导入头文件

本项目使用Android Studio,通过cmake进行编译。
新建一个工具类,进行差分包的生成及合并
Diffutils

 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);
}

导入bsdiff和bzip2的头文件以及.c文件include

)

将生成的.cpp文件改成.c文件。为什么要修改成.c文件?主要是因为c和c++对void * malloc这个函数编译不同,c中不用将结果强制转换成类型* ,而c++则不同,它必须要将结果强制转换成类型*。我们使用到的bsdiff库和bzip2库好多地方都使用了malloc函数,如果使用c++编译会报大量的错误,因此我们采用c编译。
修改了cpp文件我们不要忘记在CMakeList中进行更新以及导入新的文件
CMakeList.txt

# 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} )


3. 差分方法以及合并方法的实现

差分的方法在bsdiff.c的main函数已经实现,但是我们为了区分差分方法和合并方法我将main函数修改成generateDiffApk。同理合并方法在bspatch中已经实现,我将其main函数修改成mergeDiffApk
native-lib.c

#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;
}

4.效果展示

旧版本:旧版本
生成的差分包(app3.patch):  生成的差分包
合并后的新的安装包(app1):合并后的新的安装包
安装后:安装后


5.项目地址

我将完整项目上传到了GitHub上,有兴趣的可以点击这里进入

posted @ 2017-02-28 23:58  风起云涌,勿忘本心  阅读(2664)  评论(0编辑  收藏  举报