增量更新

搭建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
合并包

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 增量更新在Android开发中是一种很常见的技术。 增量更新的原理 增量更新的原理非常简单,就是将本地apk与服务...
    re冷星阅读 1,594评论 3 3
  • 版权声明:本文为LooperJing原创文章,转载请注明出处! 前言 如果要对一个 APP 进行更新,你会怎么做呢...
    LooperJing阅读 3,534评论 0 28
  • 最近就是在练习ndk开发,刚好遇到android增量更新的话题,主要是工具的运用,略带使用第三方so库的流程~~~...
    红黑军团号阅读 573评论 1 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • 先定一个小目标,再逐步制定目标链,然后逐步实现大的目标,目标链,就是在实现一个又一个小目标的时候,积累下来的发展经...
    龙腾虎跃江一龙阅读 173评论 0 0