搭建ndk+cmake环境
setting中system settings->android sdk->sdk tools,把如下三条勾选上,apply自动下载
环境搭建
先下载bsdiff和bspatch文件以及bzip2包https://pan.baidu.com/s/1boTzvsj 全部打包在了一起
在src/main目录下,新建cpp文件夹,将上面压缩包,解压后复制进去
在cpp目录下,新建一个c文件patch.c
包结构
#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 生成的结果
*/
public static native int generateDiffApk(String oldPath, String newPath, String patchPath);
/**
* @param oldPath 旧的安装包路径
* @param newPath 新的安装包路径
* @param patchPath 差分包路径
* @return 生成的结果
*/
public static native int mergeDiffApk(String oldPath, String newPath, String patchPath);
}
注意c文件中的方法名为java文件的包名+方法名
在项目最外层目录,新建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切换到目录下 执行
bsdiff old.apk new.apk old-to-new.patch
生成增量文件 old为老包 new 为新包 patch为生成的patch 在同一路径下 若不同路径 需写完整路径
bspatch old.apk new2.apk old-to-new.patch
合并包