下面给大家介绍下:增量更新
先回答一下上面的两个问题
1、不需要流量,是因为他们在有wifi的情况下已经将增量更新包偷偷的下载好了
2、不需要重新下载新的安装包,是因为他们下载的是增量更新包
为啥要用增量更新呢???
原因很简单,举个例子来说 微信大小35M,要是每次更新都需要重新下载的话,那么流量很是问题(在国内的移动网络下流量还是很贵的)。
那有啥好的解决办法没有?
我们来看下apk安装的手机的本质,apk的本质还是一个文件(大家可以改一下后缀,改成“.zip”然后解压看下),那么apk安装在手机上实际上就是一些文件放在了指定的目录下,每次有新的功能上线,是不是就是添加一些新的新的文件在指定的目录下。有没有感觉到和我们开发中用到的什么相似? SVN,就是这个工具,我们每次提交新文件到SVN上的时候,实际上提交的就是几个新文件 ,并没有把所有的文件都提交上去。
SVN是怎么实现的呢?
bsdiff和bspatch,(官网 http://www.daemonology.net/bsdiff/ )下载最新的版本。bsdiff和bspatch是建立和应用补丁的二进制文件的工具,通过使用后缀排序(特别是Larsson和Sadakane的qsufsort)和利用有可执行文件的变化,bsdiff经常产生二进制补丁50-80%小于那些产生的Xdelta,和15%个较小的比产生的。这是官方描述。大家想多了解的可以百度下。
下面说说增量更新的实现(这里用到了NDK开发和反射):
这里面有三个点:
第一个 如何生成差分包,
第二个 合并差分包。
第三个 签名验证
步骤:
1、首先在咱们的应用中新建一个Patchutils工具类用来调用C代码
public class Patchutils {
public native static int patch(String oldVersionPath,String newVersionPath,String patch);
static{
System.loadLibrary("bspatch");
}
}
2、生成.h文件,在命令行找到src路径,使用命令 javah 包名+类名(没有.java后缀)生成.h文件。
3、写C文件,生成动态库来合并成新的安装包。
#include
#include "bzip2/bzlib.h"
#include "bzip2/bzlib.c"
#include "bzip2/compress.c"
#include "bzip2/decompress.c"
#include "bzip2/randtable.c"
#include "bzip2/blocksort.c"
#include "bzip2/huffman.c"
#include "bzip2/crctable.c"
#include
#include
#include
#include
#include
#include
#include
#include
#include "com_example_utils_Patchutils.h"
static off_t offtin(u_char *buf)
{
off_t y;
y=buf[7]&0x7F;
y=y*256;y+=buf[6];
y=y*256;y+=buf[5];
y=y*256;y+=buf[4];
y=y*256;y+=buf[3];
y=y*256;y+=buf[2];
y=y*256;y+=buf[1];
y=y*256;y+=buf[0];
if(buf[7]&0x80) y=-y;
return y;
}
int applypatch(int argc,char * argv[])
{
FILE * f, * cpf, * dpf, * epf;
BZFILE * cpfbz2, * dpfbz2, * epfbz2;
int cbz2err, dbz2err, ebz2err;
int fd;
ssize_t oldsize,newsize;
ssize_t bzctrllen,bzdatalen;
u_char header[32],buf[8];
u_char *old, *new;
off_t oldpos,newpos;
off_t ctrl[3];
off_t lenread;
off_t i;
if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
/* Open patch file */
if ((f = fopen(argv[3], "r")) == NULL)
err(1, "fopen(%s)", argv[3]);
/*
File format:
0 8 "BSDIFF40"
8 8 X
16 8 Y
24 8 sizeof(newfile)
32 X bzip2(control block)
32+X Y bzip2(diff block)
32+X+Y ??? bzip2(extra block)
with control block a set of triples (x,y,z) meaning "add x bytes
from oldfile to x bytes from the diff block; copy y bytes from the
extra block; seek forwards in oldfile by z bytes".
*/
/* Read header */
if (fread(header, 1, 32, f) < 32) {
if (feof(f))
errx(1, "Corrupt patch\n");
err(1, "fread(%s)", argv[3]);
}
/* Check for appropriate magic */
if (memcmp(header, "BSDIFF40", 8) != 0)
errx(1, "Corrupt patch\n");
/* Read lengths from header */
bzctrllen=offtin(header+8);
bzdatalen=offtin(header+16);
newsize=offtin(header+24);
if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
errx(1,"Corrupt patch\n");
/* Close patch file and re-open it via libbzip2 at the right places */
if (fclose(f))
err(1, "fclose(%s)", argv[3]);
if ((cpf = fopen(argv[3], "r")) == NULL)
err(1, "fopen(%s)", argv[3]);
if (fseeko(cpf, 32, SEEK_SET))
err(1, "fseeko(%s, %lld)", argv[3],
(long long)32);
if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
if ((dpf = fopen(argv[3], "r")) == NULL)
err(1, "fopen(%s)", argv[3]);
if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
err(1, "fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen));
if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
if ((epf = fopen(argv[3], "r")) == NULL)
err(1, "fopen(%s)", argv[3]);
if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
err(1, "fseeko(%s, %lld)", argv[3],
(long long)(32 + bzctrllen + bzdatalen));
if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
if(((fd=open(argv[1],O_RDONLY,0))<0) ||
((oldsize=lseek(fd,0,SEEK_END))==-1) ||
((old=malloc(oldsize+1))==NULL) ||
(lseek(fd,0,SEEK_SET)!=0) ||
(read(fd,old,oldsize)!=oldsize) ||
(close(fd)==-1)) err(1,"%s",argv[1]);
if((new=malloc(newsize+1))==NULL) err(1,NULL);
oldpos=0;newpos=0;
while(newposnewsize)
errx(1,"Corrupt patch\n");
/* Read diff string */
lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
if ((lenread < ctrl[0]) ||
((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
errx(1, "Corrupt patch\n");
/* Add old data to diff string */
for(i=0;i=0) && (oldpos+inewsize)
errx(1,"Corrupt patch\n");
/* Read extra string */
lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
if ((lenread < ctrl[1]) ||
((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
errx(1, "Corrupt patch\n");
/* Adjust pointers */
newpos+=ctrl[1];
oldpos+=ctrl[2];
};
/* Clean up the bzip2 reads */
BZ2_bzReadClose(&cbz2err, cpfbz2);
BZ2_bzReadClose(&dbz2err, dpfbz2);
BZ2_bzReadClose(&ebz2err, epfbz2);
if (fclose(cpf) || fclose(dpf) || fclose(epf))
err(1, "fclose(%s)", argv[3]);
/* Write the new file */
if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
err(1,"%s",argv[2]);
free(new);
free(old);
return 0;
}
JNIEXPORT jint JNICALL Java_com_example_utils_Patchutils_patch(JNIEnv *env,
jobject obj, jstring old, jstring new , jstring patch){
int argc=4;
char * argv[argc];
argv[0]="bspatch";
argv[1]=(*env)->GetStringUTFChars(env,old, 0);
argv[2]=(*env)->GetStringUTFChars(env,new, 0);
argv[3]=(*env)->GetStringUTFChars(env,patch, 0);
int ret=applypatch(argc, argv);
(*env)->ReleaseStringUTFChars(env,old,argv[1]);
(*env)->ReleaseStringUTFChars(env,new,argv[2]);
(*env)->ReleaseStringUTFChars(env,patch,argv[3]);
return ret;
}
注意修改Android.mk文件。LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bspatch
LOCAL_SRC_FILES := com_example_utils_Patchutils.c
LOCAL_LDLIBS := -lz -llog
include $(BUILD_SHARED_LIBRARY)
4、签名校验(为了安全)
获取已安装应用的签名
/**
* 获取已安装的应用签名
*
* @param context
* @param packageName
* @return
*/
public static String getAppSign(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
List packageInfos = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
for (PackageInfo pkg : packageInfos) {
if (packageName.equals(pkg.packageName)) {
return pkg.signatures[0].toCharsString();
}
}
return null;
}
/**
* 获取APK签名
* @param path
* @return
*/
public static String getAPKSign(String path) {
String PATH_PackageParser = "android.content.pm.PackageParser";
try {
// apk包的文件路径
// 这是一个Package 解释器, 是隐藏的
// 构造函数的参数只有一个, apk文件的路径
// PackageParser packageParser = new PackageParser(path);
//实例化PackageParser对象
Class> pkgParserCls = Class.forName(PATH_PackageParser);
Class[] typeArgs = new Class[1];
typeArgs[0] = String.class;
Constructor> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
Object[] valueArgs = new Object[1];
valueArgs[0] = path;
Object pkgParser = pkgParserCt.newInstance(valueArgs);
// 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
// PackageParser.Package mPkgInfo = packageParser.parsePackage(new
// File(path), path,
// metrics, 0);
typeArgs = new Class[4];
typeArgs[0] = File.class;
typeArgs[1] = String.class;
typeArgs[2] = DisplayMetrics.class;
typeArgs[3] = Integer.TYPE;
Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
valueArgs = new Object[4];
valueArgs[0] = new File(path);
valueArgs[1] = path;
valueArgs[2] = metrics;
valueArgs[3] = PackageManager.GET_SIGNATURES;
Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
typeArgs = new Class[2];
typeArgs[0] = pkgParserPkg.getClass();
typeArgs[1] = Integer.TYPE;
Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod("collectCertificates", typeArgs);
valueArgs = new Object[2];
valueArgs[0] = pkgParserPkg;
valueArgs[1] = PackageManager.GET_SIGNATURES;
pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
// 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures");
Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg);
return info[0].toCharsString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
5、编译两个不同的版本,生成差分包
在bsdiff.exe文件夹下,将两个版本的apk同时放在该目录下。使用命令 bsdiff wenying1.apk wenying2.apk apk.patch 说一下这个个命令的三个参数,wenying1.apk----->旧版本apk名,wenying2.apk -----> 新版本apk名,apk.patch ----->生成的差分包名
6、把差分包放在服务器上。
大功告成。