android增量更新
android增量更新
步骤
环境: windows
github 项目地址: https://github.com/yuankangli/diffupdate
里面的4个 todo 需要额外注意
下载文件需要APP开放存储权限, 这部分需要自己开发实现
轻松几步实现增量更新
- 复制 diffupdate 模块到项目并引入
- 需要更新的地方调用
UpdateUtil.updateApp(mContext, true);
具体图文
1. 复制github上 diffupdate 文件夹, 并将项目添加到as项目中
修改 根目录下 settings.gradle
文件, 将 include ':diffupdate'
加入到项目当中
app/build.gradle
新增引入当前module, implementation project(path: ':diffupdate')
如需修改包名, 请注意
diffupdate/src/main/cpp/update-lib.cpp
路径第 10 行也需要一起修改
2. Link c++
在diffupdate
模块上src/cpp
右击选择Link C++ Project with Gradle
等待重新编译完毕即可
3. 在需要更新的地方调用
/**
*
* @param autoInstall 是否直接安装, 如果为false, 则弹出dialog, 提示用户更新信息并在确定后进行安装
*/
UpdateUtil.updateApp(mContext, true);
4. 测试更新功能
- 打包当前apk
- 修改内容, 打包新的apk, 注意打包新的apk时, 需要增加
versionName
的版本号 - 使用
tools/windows/获取查分文件.bat
, 获取查分文件patch.patch 和 md5 值 - 修改服务器代码, 新增两个方法
getAppUpdateInfo
和downloadApp
注意: 服务器端url需要修改文件
com.liyk.app.diffupdate.network.GetUpdateInfo.GET_UPDATE_INFO_URL
java服务器参考代码
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/diffUpdate")
public class DiffUpdate {
@GetMapping("getAppUpdateInfo")
public Map<String, Object> getAppUpdateInfo(String versionCode){
Map<String, Object> result = new HashMap<>();
if ("1.0".equals(versionCode)) {
result.put("needUpdate", true);
result.put("patchFileUrl", "patch.patch");
result.put("updatedMd5", "xxx");
result.put("modifyContent", "更新了版本号而已");
result.put("updatedVersionCode", "1.0.1");
} else {
result.put("needUpdate", false);
}
return result;
}
@RequestMapping(value = "downloadApp")
public ResponseEntity<byte[]> downloadApp(HttpServletRequest request, HttpServletResponse response, String patchFileUrl) throws Exception {
if (StringUtils.isEmpty(patchFileUrl)) {
return null;
}
String rootPath = "e:/demo/" + patchFileUrl;
File file = new File(rootPath);
InputStream is = null;
if (file.exists()) {
try {
byte[] body = null;
is = new FileInputStream(file);
body = new byte[is.available()];
int read = is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=patch.patch");
HttpStatus statusCode = HttpStatus.OK;
return new ResponseEntity<>(body, headers, statusCode);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
}
return null;
}
}