Package Com.winter.updatedemo.utils;public class Patchutil { static{ system.loadlibrary ("patch"); } public static native int patch (string oldapkpath, String Newapkpath, String patchpath);}
/* do don't EDIT this file-it was machine generated */#include <jni.h>/* Header for Clas S Com_winter_updatedemo_utils_patchutil */#ifndef _included_com_winter_updatedemo_utils_patchutil#define _Included _com_winter_updatedemo_utils_patchutil#ifdef __cplusplusextern "C" {#endif/* * Class:com_winter_updatedemo_utils_ Patchutil * Method:patch * Signature: (ljava/lang/string; ljava/lang/string; ljava/lang/string;) I */jniexport jint jnicall java_com_winter_updatedemo_utils_patchutil_patch (JNIENV *, Jobject, Jstring, jstring, jstring); #ifdef __cplusplus} #endif #endif
It declares a function jniexport jint jnicall java_com_winter_updatedemo_utils_patchutil_patch (jnienv *, Jobject, jstring, jstring, jstring); This function is the C implementation of the local method inside the Patchutil class. Of course, the header file is just a function declaration, and then we're going to implement this function. Create a JNI folder and copy the generated header files. Right-click in the App\src\main folder, select New->folder->jni folder, and then create a new folder under the main folder JNI, first copy our header file. Next we need to use the Bsdiff tool, Bsdiff is an open-source binary differential tool to generate the differential packets required for an incremental upgrade, and the corresponding Bspatch is used to synthesize the new APK package via the differential packet. For the client, we only need to complete the function of Bspatch, Bsdiff function is generally implemented by the server. In addition, Bsdiff relies on bzip2, so we download the two tools separately. BSDIFF:BSDIFFBZIP2:BZIP2BZIP2 decompression After a lot of files, but we do not use much, so in the Jni folder under a new folder, the file is copied into: And then the bspatch.c file into the Jni folder. Then make the following modifications: 1. Find line 31st # include <bzlib.h>, change to # include "Bzip2/bzlib.h" 2. Add a row below line 37, add a new line to the header file we generated earlier #include "com_ Winter_updatedemo_utils_patchutil.h "3. Change the name of the main () function to Applypatch () or another name 4. Add the following function at the end of the bspatch.c file:
Jniexport jint jnicall java_com_winter_updatedemo_utils_patchutil_patch (jnienv *env,jobject obj, jstring old, jstring New, Jstring patch) {char * ch[4];ch[0] = "Bspatch"; ch[1] = (char*) ((*env)->getstringutfchars (env, old, 0)); ch[2] = (c har*) ((*env)->getstringutfchars (env, new, 0)); ch[3] = (char*) ((*env)->getstringutfchars (env, patch, 0)); int ret = Applypatch (4, ch);(*env)->releasestringutfchars (env, old, ch[1]);(*env)->releasestringutfchars (env, new, ch[ 2]);(*env)->releasestringutfchars (env, Patch, ch[3]); return ret;}
The name and parameters of the function are consistent with com_winter_updatedemo_utils_patchutil.h, which is actually the C implementation of the patch () method in Patchutil.
Local_path: = $ (call My-dir) include $ (clear_vars) local_module : = patchlocal_src_files : = Bspatch.clocal_ Ldlibs : =-lz-lloginclude $ (build_shared_library)
The work under the Ok,jni folder is done. Finally, modify the Build.gradle file under the App folder to add the following in the Defaultconfig code block:
ndk{ modulename "patch" ldlibs "z", "Log" }
One thing to note here is that ModuleName is the name of the final compiled build library, and of course, the final. So file will be prefixed with "Lib", such as the above code, and the full name of the. So file generated after compilation will be libpatch.so. In addition, looking back at the static code block in the Patchutil class, called the system method of loading the library file, where the name of the library file is consistent with our modulename, do not add any prefix. Seven, all the work is over, the patch method can be called, in the activity of a thread, call Patchutil.patch (), you can do the APK package synthesis. Eight, in addition to the Bsdiff can download Bsdiff source code, you can also download the Windows environment of the. exe executable file, download it, we can manually generate the differential packet locally, and do not need to rely on the server. Nine, the demo will be served later. Ten, doubt. I heard that the source code of Android also contains Bsdiff source code, then Android has a native incremental upgrade interface it?