現在增量更新的項目有基于Bsdiff、Courgette、HDiff ,相關的信息可以問問度娘去。我還是說說我實際在用的吧。
2013年的時候,APP應用市場慢慢浮現了省流量更新,那時公司趕著幾個項目,周更的頻度,更新環境經常是在測試車輛上,那個時候移動流量還是很蹩腳,我就下定決心嘗試實現這個功能,從前臺到后臺,從無到有。增量更新、Bsdiff 這些詞開始進入我的開發視野。那是第一次接觸NDK,在eclipse下進行操作,我的神船(已賣出)是Ubuntu, NDK環境的安裝都很順利,避開了Windows下不少坑,也是由于Ubuntu這個原因,得以存活。得到的so庫,一直用到現在,換了公司,換了平臺。
這幾天有空,重新在Mac AS上通過cmake復現一次編譯過程。
增量更新原理
其實增量升級的原理很簡單,即首先將應用的舊版本Apk與新版本Apk在服務器做差分,得到更新的部分的補丁,下載需要更新的補丁,跟本地的文件合并,安裝即可。例如舊版本的APK有5M,新版的有8M,更新的部分則只能比3M大 (得到的差分包大小并不是簡單的相減,因為Bsdiff是在二進制的基礎上差分整個APK文件),使用增量升級的好處顯而易見,像我周更的APP(15MB),通常增加功能是1MB-5MB左右,修復Bug的話,可以很小,30KB- 200K,可以暫時忽略心里那根刺,隨時隨地更新,不擔心流量。
整個更新過程的細節留在第二部分再細細說來。
bsdiff 源碼下載
首先,重新找一下bsdiff的源碼,從這里可以得知bsdiff的信息。
- 版本V4.3
- 依賴Bzip2
- 內存饑餓型 bsdiff: max(17*old,9*old+new)+O(1) bspatch:old+new+O(1)
- 運行時間差異:bsdiff 耗時長,bspatch 2秒
- 最大文件大小:2^61-1 = 2Ei-1 bytes
了解了這些,對以后出現的問題也心里有數了,bsdiff不適合在移動設備運行,也不適合在服務器端同步運行。接著就是去下一個bzip2的源碼了。
bzip2 源碼下載
Bzip2 現在版本 1.0.6, 發布于2010-09-20.其他信息也不需要了。
代碼修改
根目錄存放 Bsdiff 源碼,再建一個bzip2 文件夾放置Bzip2源碼
里面有很多多余文件,分批清理一下,剩下來的就干凈多了。
這個時候還是存在問題,bsdiff.c 、bspatch.c 是入口文件,肯定存在main函數,改一下,我就加上文件名作為前綴,編譯會發現 bzip2recover.c 也有一個main函數,這個main函數直接注釋就行了。
AS 項目結構
說了那么多,我還沒說一下我的項目結構。
測試APP很簡單,提供兩個按鈕測試功能,信息從Logcat中獲取。
開發編譯
添加CMakeLists.txt文件。我也是剛接觸Cmake,查了下資料,也細細看了CMakeLists.txt文件里的默認注釋,這是第一手資料哇。aux_source_directory和list方法 也是后來才知道,不用抄路徑了,很歡喜....
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
# 將./src/main/cpp目錄(不包含子目錄)下所有源文件保存在變量 SRC 中
aux_source_directory(./src/main/cpp SRC)
# 將./src/main/cpp/bzip2目錄(不包含子目錄)下所有源文件保存在變量 SRC_OTHER 中
aux_source_directory(./src/main/cpp/bzip2 SRC_OTHER)
# 將 SRC_OTHER 添加到 SRC 中
list(APPEND SRC ${SRC_OTHER})
add_library( # Sets the name of the library.
bsdiff
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${SRC}
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
bsdiff
# Links the target library to the log library
# included in the NDK.
${log-lib} )
同步一下,AS為你創建環境,接下來,就需要C層向Java層提供接口調用,這就進入JNI的范疇了。
//Java層接口文件
public class BsdiffUtils {
static {
System.loadLibrary("bsdiff");
}
//生成差分包
public static native int diff(String oldpath, String newpath, String patch);
//舊apk和差分包合并
/**
* native方法
* 使用路徑為oldApkPath的apk與路徑為patchPath的補丁包,合成新的apk,并存儲于newApkPath
*
* @param oldpath
* @param newpath
* @param patch
* @return
*/
public static native int patch(String oldpath, String newpath, String patch);
}
在Android Studio 2.2 之后就方便多了,直接在方法名那,按 Alt+Enter 提示一下,就可以補全C層入口了。
這時候文件可能會創建在jni目錄下,名字和你的庫名字相同,改改就好了,我就拷回cpp目錄下,順帶改了一下名字(重復了)。
可以看到,大部分的代碼都有了,添加代碼去完成我們需要的事情。
創建一個頭文件,聲明主要的方法。
//native-lib.h
#include <jni.h>
#include <malloc.h>
int bsdiff_main(int argc,char *argv[]);
int bspatch_main(int argc,char * argv[]);
JNIEXPORT jint JNICALL
Java_com_kw_lib_bsdiff_BsdiffUtils_diff(JNIEnv *env, jclass type, jstring oldpath_,
jstring newpath_, jstring patch_) ;
JNIEXPORT jint JNICALL
Java_com_kw_lib_bsdiff_BsdiffUtils_patch(JNIEnv *env, jclass type, jstring oldpath_,
jstring newpath_, jstring patch_);
再修改native-lib.c文件,調用 Bsdiff 的方法。
#include "native-lib.h"
JNIEXPORT jint JNICALL
Java_com_kw_lib_bsdiff_BsdiffUtils_diff(JNIEnv *env, jclass type, jstring oldpath_,
jstring newpath_, jstring patch_) {
char *argv[4];
argv[0] = "bsdiff";
argv[1] = (*env)->GetStringUTFChars(env, oldpath_, 0);
argv[2] = (*env)->GetStringUTFChars(env, newpath_, 0);
argv[3] = (*env)->GetStringUTFChars(env, patch_, 0);
bsdiff_main(4, (char *)argv);
(*env)->ReleaseStringUTFChars(env, oldpath_, argv[1]);
(*env)->ReleaseStringUTFChars(env, newpath_, argv[2]);
(*env)->ReleaseStringUTFChars(env, patch_, argv[3]);
free(argv);
return 0;
}
JNIEXPORT jint JNICALL
Java_com_kw_lib_bsdiff_BsdiffUtils_patch(JNIEnv *env, jclass type, jstring oldpath_,
jstring newpath_, jstring patch_) {
char *argv[4];
argv[0] = "bspatch";
argv[1] = (*env)->GetStringUTFChars(env, oldpath_, 0);
argv[2] = (*env)->GetStringUTFChars(env, newpath_, 0);
argv[3] = (*env)->GetStringUTFChars(env, patch_, 0);
bspatch_main(4, (char *)argv);
(*env)->ReleaseStringUTFChars(env, oldpath_, argv[1]);
(*env)->ReleaseStringUTFChars(env, newpath_, argv[2]);
(*env)->ReleaseStringUTFChars(env, patch_, argv[3]);
free(argv);
return 0;
}
是不是很簡單呢?這個時候Bsdiff模塊完成了,寫寫測試代碼了
MainActivity的布局就不說了,簡單的兩個按鈕,指定點擊事件到click方法。必須用線程去處理,這里就簡單處理了,后面還有更完善的方式,搞一個UpdateManager。
public class MainActivity extends AppCompatActivity {
//舊版本
String old = getsdpath() + "com.ajb.anjubao.intelligent_1.3.2_38.apk";
//新版本
String newp = getsdpath() + "com.ajb.anjubao.intelligent_1.3.3_39.apk";
//差分包
String patch = getsdpath() + "com.ajb.anjubao.intelligent_38_39.patch";
//舊版apk和差分包合并生成的新版apk
String tmp = getsdpath() + "new.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
switch (view.getId()) {
case R.id.bt_diff:
Log.d("Thread", "bt_diff Thread ID is " + Thread.currentThread().getId());
new Thread(new Runnable() {
@Override
public void run() {
long s = System.currentTimeMillis();
Log.d("Thread", "Thread ID is " + Thread.currentThread().getId());
BsdiffUtils.diff(old, newp, patch);
long s1 = System.currentTimeMillis();
Log.d("bsdiff", "生成差分包成功,用時:" + (s1 - s) + "ms");
}
}).start();
break;
case R.id.bt_patch:
Log.d("Thread", "bt_patch Thread ID is " + Thread.currentThread().getId());
new Thread(new Runnable() {
@Override
public void run() {
long s = System.currentTimeMillis();
Log.d("Thread", "Thread ID is " + Thread.currentThread().getId());
long s2 = System.currentTimeMillis();
BsdiffUtils.patch(old, tmp, patch);
long s3 = System.currentTimeMillis();
Log.d("bsdiff", "差分包合并成功,用時:" + (s3 - s2) + "ms");
}
}).start();
break;
}
}
private String getsdpath() {
return Environment.getExternalStorageDirectory().getPath() + File.separator;
}
}
如何測試成功與否
在終端安裝 Bsdiff 命令工具。Mac下,我裝了Homebrew,直接brew install bsdiff 就有了。
在終端拆分兩個文件得到一個patch文件,通過sha1sum命令計算SHA1校驗碼,將這兩個文件放到手機中,也進行拆分,部分手機會由于內存問題奔潰,忽略之,畢竟這一步不在手機上運行,拷出patch文件,通過SHA1校驗碼判斷是否一致。
合并之后就簡單了,人工判斷apk文件是否可以安裝就行了,代碼上斷不能如此簡單,這個以后再說。
第一部分到此就結束了,雖然講解原理的地方不多,見諒。