一、介绍
增量更新顾名思义就是在旧版本apk基础上进行更新,那么我们需要如何操作呢,这里先介绍下bsdiff.exe工具,使其对新旧版本安装包做拆分,生成patch文件。更新时只需下载这个文件即可,前提条件“旧版本apk必须存在”,如果获取不到源apk,那么就无法进行增量更新了。
bsdiff官网:http://www.daemonology.net/bsdiff/
BSDiff基本操作
1、对old文件中所有的子字符串形成一个字典
2、对比old文件和new文件,产生diffstring和extrastring
3、将diffstring和extrastring以及相应 的控制字用zip压缩成一个patch包
关于bsdiff算法:https://blog.csdn.net/add_ada/article/details/51232889
二、准备工作
1.bsdiff、bspath下载
https://github.com/cnSchwarzer/bsdiff-win/releases
2.下载后准备两个安装包:
3.打开目录所在路径输入命令:
格式:bsdiff oldfile newfile patchfile
随后便会生成debug.patch的文件
4.生成后是否可以合并成与new.apk相同大小的安装包呢
这时我们需要用到另外一个工具bspatch.exe
格式:bsdiff oldfile newfile patchfile
回车后,我们看到一个新的new2.apk被我们生成
这说明我们合并成功了!
注释:如果我们的安装包比较大,生成的patch文件会和预想的不一样,合并后与我们将要更新的apk大小一致,就说明没问题。
三、代码实现
1.在project的build.gradle添加如下代码
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
2. 在Module的build.gradle添加依赖
compile 'com.github.jiyouliang2:SmartUpdateDemo:1.0.1'
3.添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4.使用PatchUtil.patch方法合并
PatchUtil.patch(旧版本, 新版本, 差分包);
完整代码如下:
public class MainActivity extends AppCompatActivity {
private Context mContext;
private ProgressDialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mDialog = new ProgressDialog(mContext);
}
private void requestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
updatePatch();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == 1) {
updatePatch();
}
}
public void update(View v) {
requestPermission();
}
private void updatePatch() {
try {
final String oldPath = Environment.getExternalStorageDirectory() + File.separator + "Android"+ File.separator+"old.apk";//旧版本路径
final File newApkFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android", "new2.apk");//新版本保存路径
final File patchFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android", "debug.patch");//更新包路径
if (!patchFile.exists()) {
showToast("请将差分包toutiao.patch保存到sdcard");
return;
}
mDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int result = PatchUtil.patch(oldPath, newApkFile.getAbsolutePath(), patchFile.getAbsolutePath());
if (result == 0) {
//合并成功,安装apk
runOnUiThread(new Runnable() {
@Override
public void run() {
mDialog.dismiss();
install(newApkFile.getAbsolutePath());
}
});
} else {
showToast("合并失败");
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void install(String apkPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + apkPath),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void showToast(String msg) {
Toast.makeText(mContext, "" + msg, Toast.LENGTH_SHORT).show();
}
}
四、总结
应用到项目中时要做md5效验,需要先读取本地安装旧版本APK的MD5值或SHA1,判断当前安装的文件是否为合法版本,同样,patch得到新包之后,也需要对它进行MD5或SHA1校验,校验失败,说明合成过程有问题。
参考:
https://github.com/jiyouliang2/SmartUpdateDemo
https://my.oschina.net/liucundong/blog/160436
https://blog.csdn.net/pf_1308108803/article/details/78031331
https://p.codekk.com/detail/Android/huclengyue/SmartUpdate
https://www.it610.com/article/1280728706175746048.htm