调用.so
这段代码要写在你要更新的class里
static { //调用.so文件,引入打包库 System.loadLibrary("Patcher"); }
对比版本
我们要向后台发送我们这里的版本号与最新版本进行对比,然后后台生成增量升级包
这里对比的是androidmanifest的code
这里注意是versionCode,千万不要改name,否则后台是对比不出来的,并且code 只能是int ,也就是整数
如果有差别,这时候后台应该返回patchname,也就是增量包的名字,以供下载
下载patch
增量包下载不多说
合成新APK
下载好增量包之后 异步生成APK(旧APK+patch增量包= 新APK)
private void exAsyncTask() { AsyncTask task = new AsyncTask() { String patchPath; File changFile; String rootPath; String changeName; private ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = ProgressDialog.show(InspectionActivity.this, "正在生成APK...", "请稍等...", true, false); progressDialog.show(); } @Override protected Object doInBackground(Object... arg0) { //判断旧版本apk是否存在 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/otm/Inspection.apk"; File file = new File(path); if (file.exists()) { //修改旧版本apk名称 rootPath = file.getParent(); int nameLength = patchName.length(); //修改后的旧版本apk名称 changeName = rootPath + "/" + patchName.substring(0, nameLength - 6) + ".apk"; changFile = new File(changeName); file.renameTo(changFile); if (changFile.exists()) { patchPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/otm/" + patchName; } } patcher(changeName, rootPath + File.separator + "Inspection.apk", patchPath); return null; } @Override protected void onPostExecute(Object result) { progressDialog.dismiss(); Toast.makeText(InspectionActivity.this, "打包完成,安装。。。。", Toast.LENGTH_SHORT).show(); changFile.delete(); File f = new File(rootPath + File.separator + "Inspection.apk"); installApk(f); } }; task.execute(); }
public native void patcher(String old, String newapk, String patch);//跟onCreat平级
private void installApk(File filePath) { Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + filePath.toString()), "application/vnd.android.package-archive"); startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid());// 如果不加上这句的话在apk安装完成之后点击开会崩溃 }
这时候系统就会用新生成的APK来提示你升级了
作者很菜 不喜勿喷