Android 版本更新之增量更新 包含java、.net服务端

/** * @param sourceFile 旧版本文件(.apk) * @param targetFile 新版本文件(.apk) * @param output输出文件(.patch) * */com.nothome.delta.Delta.compute(File sourceFile, File targetFile, DiffWriter output) throws IOException

/** * 生成差分包:old_new.patch = diff(old.apk, old.apk) * * */private void createPatch() {try {String sd = Environment.getExternalStorageDirectory().getPath();String oldFile = sd + "/aDiff/old.apk";String newFile = sd + "/aDiff/new.apk";String patchFile = sd + "/aDiff/old_new.patch";DiffWriter output = null;File sourceFile = null;File targetFile = null;sourceFile = new File(oldFile);targetFile = new File(newFile);output = new GDiffWriter(new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(patchFile)))));if (sourceFile.length() > Integer.MAX_VALUE|| targetFile.length() > Integer.MAX_VALUE) {System.err.println("source or target is too large, max length is "+ Integer.MAX_VALUE);System.err.println("aborting..");}Delta d = new Delta();d.compute(sourceFile, targetFile, output);Toast.makeText(getApplicationContext(), "生成完成!", Toast.LENGTH_LONG).show();} catch (Exception e) {e.printStackTrace();}}2、合成差分包

思路:将patch和旧版本文件合成,并比对MD5,,若生成文件的MD5与服务器下发的MD5匹配,则提示合成成功,否则删除该文件。

核心代码:

/** * 合成 * @param sourceFile 旧版本文件 * @param patchFile 更新包 * @param outputFile 新版本文件(生成) * * */com.nothome.delta.GDiffPatcher.patch(File sourceFile, File patchFile, File outputFile) throws IOException

/** * 合成差分包:new.apk = old.apk + old_new.patch * */private void mixPatch() {try {String sd = Environment.getExternalStorageDirectory().getAbsolutePath();String serviceFile = sd + "/aDiff/new.apk";String source = sd + "/aDiff/old.apk";String patch = sd + "/aDiff/old_new.patch";String target = sd + "/aDiff/mix.apk";String newMD5 = DiffTool.getMD5(new File(serviceFile));DiffTool.mergeApk(source, patch, target, newMD5);Toast.makeText(getApplicationContext(), "合成完成!", Toast.LENGTH_LONG).show();} catch (Exception e) {e.printStackTrace();}}private static File mergeFile(final String source, final String patch,String target) throws Exception {GDiffPatcher patcher = new GDiffPatcher();File deffFile = new File(patch);File updatedFile = new File(target);patcher.patch(new File(source), deffFile, updatedFile);return updatedFile;}public static File mergeApk(final String source, final String patch,final String target, String newApkMd5) throws Exception {File updateFile = mergeFile(source, patch, target);String ufpMd5 = getMD5(updateFile);System.out.println("服务端下发的md5:" + newApkMd5 + ",新合并后的apk MD5:" + ufpMd5);if (ufpMd5 == null || !newApkMd5.equalsIgnoreCase(ufpMd5)) {if (updateFile.exists()) {updateFile.delete();}throw new Exception("MD5错误,不能成功合并!");}return updateFile;}接下来就是最后一步,安装APK。

3、安装apk

/** * 安装apk。 这边路径已经写死,实际应用中,apk路径需要当参数传入 * */private void installAPK() {File apkfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aDiff/mix.apk");if (!apkfile.exists()) {return;}Intent i = new Intent(Intent.ACTION_VIEW);i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive");MainActivity.this.startActivity(i);}

注释:

4、.NET服务端的.patch文件生成

同样是调用javaxdelta,trove,当然并不是直接调用jar包,首先要将这两个包编译成dll文件,供.NET调用。在这里感谢博客园的一位朋友,详细的讲解了IKVM的使用方法:@xiaotie的博文将java库转换为.net库在这就不详细介绍了。在转换过程中,若遇到问题,可以给我留言,嘿嘿~~

通过IKVM这个工具,将上述两个jar包转成对应的dll文件:javaxdelta.dll、trove.dll,只有这两个包是不够的,还需要将IKVM.OpenJDK.Core.dll、IKVM.Runtime.dll、IKVM.Runtime.JNI.dll三个类库同时引入,才可以正常编译。

自己打败自己是最可悲的失败,

Android 版本更新之增量更新 包含java、.net服务端

相关文章:

你感兴趣的文章:

标签云: