安卓增量更新
环境:
10多年的阿克苏网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整阿克苏建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“阿克苏网站设计”,“阿克苏网站推广”以来,每个客户项目都认真落实执行。
centos android9 idea
步骤:
1.下载bsdiff
2.centos 安装
gcc
make
bzip2
bzip2-devel
命令
yum -y install gcc autoconf automake libtool make
yum -y install bzip2
yum -y install bzip2-devel
3.修改解压后bsdiff中Makefile文件 .ifndef WITHOUT_MAN 和 endif缩进
4.在bsdiff中执行make命令生成bsdiff和bspatch 文件
5.生成合并差异包
生成 bsdiff oldapk_path.apk newapk_path.apk patchName.patch
合并 bspatch oldapk_path.apk newName_path.apk patch_path.patch
以上为服务端
Android端
1.ndk新建个PatchUtil.java用与调用native方法
public class PatchUtils {
private static PatchUtils instance;
static {
System.loadLibrary("update");
}
public static PatchUtils getInstance(){
if(instance==null){
instance = new PatchUtils();
}
return instance;
}
/**
* native方法 使用路径为oldApkPath的Apk与路径为patchPath的补丁包合成一个新的apk存储与newApkPath
* 返回0成功
* @param oldApkPath 示例:/usr/old/old.apk
* @param newApkPath 示例:/usr/new/new.apk
* @param patchPath 示例:/usr/patch/patch_1.patch
* @return
*/
public static native int bspatch(String oldApkPath, String newApkPath, String patchPath);
}
配置java.h 和buildOS
3.使用javah命令生成头文件,会在jni文件夹下生成com_xxx_xxx_xxx.PatchUtils.h,
4.将bsdiff文件夹下载bspatch.c导入jni文件夹
5.下载bzip2导入jni文件夹,删除除了.c和.h的其他文件
修改bspatch.c
/*-
* Copyright 2003-2005 Colin Percival
* All rights reserved
*
* redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if 0
__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $");
#endif
#include
#include
#include
#include
#include
#include
#include "bzip2/bzlib.c"
#include "bzip2/crctable.c"
function(){ //点差 http://www.fx61.com/faq/muniu/460.html
#include "bzip2/compress.c"
#include "bzip2/decompress.c"
#include "bzip2/randtable.c"
#include "bzip2/blocksort.c"
#include "bzip2/huffman.c"
#include
int genpatch(int argc,char * argv[]);
JNIEXPORT jint JNICALL Java_xxx.xxxx.xxx_PatchUtils_bspatch(JNIEnv *env,
jclass cls, jstring oldApkPath, jstring newApkPath, jstring patchPath) {
int argc = 4;
char * argv[argc];
argv[0] = "bsdiff";
argv[1] = (char*) ((*env)->GetStringUTFChars(env, oldApkPath, 0));
argv[2] = (char*) ((*env)->GetStringUTFChars(env, newApkPath, 0));
argv[3] = (char*) ((*env)->GetStringUTFChars(env, patchPath, 0));
printf("old apk = %s \n", argv[1]);
printf("new apk = %s \n", argv[2]);
printf("patch = %s \n", argv[3]);
int ret = genpatch(argc, argv);
printf("genDiff result = %d ", ret);
(*env)->ReleaseStringUTFChars(env, oldApkPath, argv[1]);
(*env)->ReleaseStringUTFChars(env, newApkPath, argv[2]);
(*env)->ReleaseStringUTFChars(env, patchPath, argv[3]);
return ret;
}
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 genpatch(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(newpos /* Read control data */ for(i=0;i<=2;i++) { lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8); if ((lenread < 8) || ((cbz2err != BZ_OK) && (cbz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); ctrl[i]=offtin(buf); }; /* Sanity-check */ if(newpos+ctrl[0]>newsize) 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 if((oldpos+i>=0) && (oldpos+i new[newpos+i]+=old[oldpos+i]; /* Adjust pointers */ newpos+=ctrl[0]; oldpos+=ctrl[0]; /* Sanity-check */ if(newpos+ctrl[1]>newsize) 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; } 添加Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE = update #此处为PatchUtils.java中静态代码块中的名字,可按需要修改 LOCAL_C_INCLUDES:=$(LOCAL_PATH)/bzip2 LOCAL_CFLAGS = $(L_CFLAGS) LOCAL_SRC_FILES = bspatch.c LOCAL_C_INCLUDES = $(INCLUDES) include $(BUILD_SHARED_LIBRARY) 使用BuilsOS生成so文件 文件生成在libs文件夹下,可按自己需求删除不需要的ABI,或提前配置Application.mk jni目录 安装包合成 private boolean bdpatch(String name) { final File destApk = new File(Environment.getExternalStorageDirectory(), "xxx.apk"); final File patch = new File(Environment.getExternalStorageDirectory(), name); //一定要检查文件都存在 final String _thisapk = ApkExist.extract(this); System.out.println(patch.getAbsolutePath()); if(patch.exists()){ showToast("正在合成包请等待!"); int result = PatchUtils.bspatch(_thisapk,destApk.getAbsolutePath(),patch.getAbsolutePath()); System.out.println(result); if(result==0){ if (destApk.exists()) { showToast("合成成功!"); return true; }else { showToast("更新包合成失败,请卸载后重新安装!"); return false; } }else{ showToast("更新包合成失败,请卸载后重新安装!"); return false; } }else{ showToast("更新包合成失败,请卸载后重新安装!"); return false; } } public class ApkExist{ public static String extract(Context context) { context = context.getApplicationContext(); ApplicationInfo applicationInfo = context.getApplicationInfo(); return applicationInfo.sourceDir; } } 10.找到合成后的安装包拉起安装
网页名称:安卓增量更新
URL链接:http://myzitong.com/article/igjdhs.html