在上一篇文章中提到如何生成更新包,不明白的在进入http://www.cnblogs.com/lping/p/5833090.html查看
下载需要使用的bzip
http://www.bzip.org/downloads.html
拷贝源码到android studio工程的jni目录里,如果不明白jni的童鞋自行查阅Google
生成java Patch工具类
package com.github.liuping123.bspatch.util;
/**
* 类说明: APK Patch工具类
*/
public class PatchUtils {
static {
System.loadLibrary("bspatch");
}
/**
* native方法 使用路径为oldApkPath的apk与路径为patchPath的补丁包,合成新的apk,并存储于newApkPath
* <p>
* 返回:0,说明操作成功
*
* @param oldApkPath 示例:/sdcard/old.apk
* @param newApkPath 示例:/sdcard/new.apk
* @param patchPath 示例:/sdcard/patch.apk
* @return
*/
public static native int patch(String oldApkPath, String newApkPath,
String patchPath);
}
通过javah生成.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_github_liuping123_bspatch_util_PatchUtils */
#ifndef _Included_com_github_liuping123_bspatch_util_PatchUtils
#define _Included_com_github_liuping123_bspatch_util_PatchUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_github_liuping123_bspatch_util_PatchUtils
* Method: patch
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_github_liuping123_bspatch_util_PatchUtils_patch
(JNIEnv *, jclass, jstring, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
编译生成so文件,这个根据需要生成不同平台的so
使用
PatchUtils.patch(老的apk,新apk目录,patch文件)
生成新apk后调用安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + apkPath),
"application/vnd.android.package-archive");
context.startActivity(intent);
一切OK了
我生成了jcenter和maven库,so支持所有平台,可以下载源码自己编译
android studio使用maven
repositories {
maven {
url 'https://dl.bintray.com/ping/maven/'
}
}
dependencies {
...
compile 'com.github.liuping123.bspatch:bspatchlibrary:0.0.4@aar'
}
android studio使用jcenter
compile 'com.github.liuping123.bspatch:bspatchlibrary:0.0.4'
Github地址:
https://github.com/liuping123/BsPatchLib/