Android开发 使用差分算法实现增量更新
作者:互联网
还是老样子先上效果图,由于上传图片限制压缩有点严重凑合看吧:
首先使用差分算法bsdiff计算出差分包,感兴趣的可以自己点击进去下载源码,然后就是使用bzip2压缩工具打包生成补丁差分包文件和合并补丁包文件;由于这里提供的都是C语言程序所以我们需要借助NDK/JNI实现增量更新了。
我们先去bsdiff地址下载bsdiff.c和bspatch.c这两个文件,然后去bzip2下载源码包解压复制粘贴出我们需要的文件:
bzip2/blocksort.c\
bzip2/bzip2.c\
bzip2/bzip2recover.c\
bzip2/bzlib.c\
bzip2/bzlib.h\
bzip2/bzlib_private.h\
bzip2/compress.c\
bzip2/crctable.c\
bzip2/decompress.c\
bzip2/huffman.c\
bzip2/randtable.c
好了准备工作我们都做好了就开始创建JNI文件吧:
1.创建一个java文件例如:DiffPatchUtil,然后使用命令进入该文件目录下用命令编译“javac DiffPatchUtil.java”生成DiffPatchUtil.class文件,再执行“javah com.xhiston.diffpatch
.DiffPatchUtil”生成com_xhiston_diffpatch_DiffPatchUtil.h文件这一步大家需要注意一下命令目录回退一下到DiffPatchUtil的最外层包名下面不然命令提示找不包名下文件。DiffPatchUtil.java创建的时候可以简单的只放JNI回调的相关方法,生成com_xhiston_diffpatch_DiffPatchUtil.h文件后再修改添加其他方法。
package com.xhiston.diffpatch;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;
/**
* Created by xie on 2020/10/20.
*/
public class DiffPatchUtil {
static {
System.loadLibrary("diffpatch");
}
/**
* 采用差分算法将当前包与新包打patch补丁包,生成xxx.patch文件
**/
public native int diff(String oldApk, String newApk, String patch);
/**
* 采用差分算法将patch补丁包与当前包合并生成新包,生成apk文件
**/
public native int patch(String oldApk, String newApk, String patch);
}
com_xhiston_diffpatch_DiffPatchUtil.h文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xhiston_diffpatch_DiffPatchUtil */
#ifndef _Included_com_xhiston_diffpatch_DiffPatchUtil
#define _Included_com_xhiston_diffpatch_DiffPatchUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xhiston_diffpatch_DiffPatchUtil
* Method: diff
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_xhiston_diffpatch_DiffPatchUtil_diff
(JNIEnv *, jobject, jstring, jstring, jstring);
/*
* Class: com_xhiston_diffpatch_DiffPatchUtil
* Method: patch
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_xhiston_diffpatch_DiffPatchUtil_patch
(JNIEnv *, jobject, jstring, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
2.创建com_xhiston_diffpatch_DiffPatchUtil.c以及JNI配置文件Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
APP_ABI := All
APP_PLATFORM := android-16
LOCAL_C_INCLUDES :=bzip2
LOCAL_MODULE := diffpatch
LOCAL_SRC_FILES := com_xhiston_diffpatch_DiffPatchUtil.h\
com_xhiston_diffpatch_DiffPatchUtil.c\
bspatch.c\
bsdiff.c\
myerr.h\
myerr.c\
bzip2/blocksort.c\
bzip2/bzip2.c\
bzip2/bzip2recover.c\
bzip2/bzlib.c\
bzip2/bzlib.h\
bzip2/bzlib_private.h\
bzip2/compress.c\
bzip2/crctable.c\
bzip2/decompress.c\
bzip2/huffman.c\
bzip2/randtable.c\
include $(BUILD_SHARED_LIBRARY)
修改一下bspatch.c、bsdiff.c里的main方法名然后com_xhiston_diffpatch_DiffPatchUtil中就可以重新调用了,可以参考我的源码进行修改,确保代码无误后便可以ndk-buildd编译生成so库了,当然编译的时候也会检查代码报错的需要自行修改,不过这个就要求大家有一定的C语言基础了,没基础的话可以现学一下不是多难。
3.如果大家不想修改,我这里已经为大家封装成library库了,大家直接下载导入diffpatch库引用一下就可以直接使用了非常简单;
标签:bzip2,String,DiffPatchUtil,差分,diffpatch,xhiston,算法,Android,com 来源: https://blog.csdn.net/xhf_123/article/details/111151048