Android增量更新(簡單易懂)
技術標籤:android
一、介紹
增量更新顧名思義就是在舊版本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