搭建ndk+cmake環(huán)境
setting中system settings->android sdk->sdk tools,把如下三條勾選上,apply自動下載

環(huán)境搭建
先下載bsdiff和bspatch文件以及bzip2包https://pan.baidu.com/s/1boTzvsj 全部打包在了一起
在src/main目錄下,新建cpp文件夾,將上面壓縮包,解壓后復(fù)制進(jìn)去
在cpp目錄下,新建一個c文件patch.c

包結(jié)構(gòu)
#include <jni.h>
#include "bzip2/bsdiff.c"
#include "bzip2/bspatch.c"
JNIEXPORT jint JNICALL
Java_com_nick_bsdiff_Diffutils_generateDiffApk(JNIEnv *env, jclass type, jstring oldPath_,
jstring newPath_, jstring patchPath_) {
int argc = 4;
char *argv[argc];
argv[0] = (char *) "bspatch";
argv[1] = (char *) (*env)->GetStringUTFChars(env, oldPath_, 0);
argv[2] = (char *) (*env)->GetStringUTFChars(env, newPath_, 0);
argv[3] = (char *) (*env)->GetStringUTFChars(env, patchPath_, 0);
jint result = generateDiffApk(argc, argv);
(*env)->ReleaseStringUTFChars(env, oldPath_, argv[1]);
(*env)->ReleaseStringUTFChars(env, newPath_, argv[2]);
(*env)->ReleaseStringUTFChars(env, patchPath_, argv[3]);
return result;
}
JNIEXPORT jint JNICALL
Java_com_nick_bsdiff_Diffutils_mergeDiffApk(JNIEnv *env, jclass type, jstring oldPath_,
jstring newPath_, jstring patchPath_) {
int argc = 4;
char *argv[argc];
argv[0] = (char *) "bspatch";
argv[1] = (char *) (*env)->GetStringUTFChars(env, oldPath_, 0);
argv[2] = (char *) (*env)->GetStringUTFChars(env, newPath_, 0);
argv[3] = (char *) (*env)->GetStringUTFChars(env, patchPath_, 0);
printf("old apk = %s \n", argv[1]);
printf("patch = %s \n", argv[3]);
printf("new apk = %s \n", argv[2]);
jint result = mergeDiffApk(argc, argv);
(*env)->ReleaseStringUTFChars(env, oldPath_, argv[1]);
(*env)->ReleaseStringUTFChars(env, newPath_, argv[2]);
(*env)->ReleaseStringUTFChars(env, patchPath_, argv[3]);
return result;
}
java文件中,新建DiffUtil.java
package com.nick.bsdiff;
/**
* Created by nick on 2017/2/27.
*/
public class Diffutils {
static {
System.loadLibrary("patch");
}
/**
* @param oldPath 舊的安裝包路徑
* @param newPath 新的安裝包路徑
* @param patchPath 差分包路徑
* @return 生成的結(jié)果
*/
public static native int generateDiffApk(String oldPath, String newPath, String patchPath);
/**
* @param oldPath 舊的安裝包路徑
* @param newPath 新的安裝包路徑
* @param patchPath 差分包路徑
* @return 生成的結(jié)果
*/
public static native int mergeDiffApk(String oldPath, String newPath, String patchPath);
}
注意c文件中的方法名為java文件的包名+方法名
在項(xiàng)目最外層目錄,新建CMakeLists.txt
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
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 it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
patch
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/patch.c )
#最后的斜杠一定要加
include_directories(src/main/cpp/bzip2/)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
patch
# Links the target library to the log library
# included in the NDK.
${log-lib} )
module的gradle中,android{}方法中,添加
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
Project的gradle.properties中添加
android.useDeprecatedNdk=true
手動生成patch的方法:
下載兩個exe文件,bsdiff拆分 bspatch合并
https://pan.baidu.com/s/1pLGzWBx
使用 cmd切換到目錄下 執(zhí)行
bsdiff old.apk new.apk old-to-new.patch
生成增量文件 old為老包 new 為新包 patch為生成的patch 在同一路徑下 若不同路徑 需寫完整路徑
bspatch old.apk new2.apk old-to-new.patch
合并包