title: Android NDK之增量更新

1.增量更新使用到的库bsdiff和bzip2


2.AS创建项目,并导入头文件

 public class Diffutils {

    static {
System.loadLibrary("native-lib");
} /**
* @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);
}

)

# 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.
native-lib # 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/native-lib.c )
#include src/main/cpp/include目录下的所有文件
include_directories(src/main/cpp/include) # 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.
native-lib # Links the target library to the log library
# included in the NDK.
${log-lib} )

3. 差分方法以及合并方法的实现

#include <jni.h>

#include "include/bsdiff.c"
#include "include/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;
}

4.效果展示


5.项目地址

05-08 08:22