The NDK development of the incremental upgrade feature under Android Studio

Source: Internet
Author: User

Recent research on the incremental upgrade features of Android apps, which involved the NDK development content, was documented here. Say a few questions first. First, the NDK development is JNI development Ah, I did not know the trough, I always thought it was two things ah ... Second, about the development environment, the old version of Eclipse to install more tools, what ndk ah, Cygwin ah what, as (the first letter of the Android studio, the following as a replacement) is much simpler, directly installed an NDK can be. It seems that as from the 1.3 version can not be installed Cygwin, and I installed the 2.0 version, but also do not need to install. And the amazing thing is, I didn't turn over the wall when I installed the NDK, because of Google's return. The process of installing the NDK is simple, click the As toolbar button, select the SDK location option in the Open Project Structure window, you can see the NDK installation options, there is no option to download the installation or specify the local installation path, but I did not, Only one is installed. Then start the development step. Take the incremental upgrade feature I developed as an example, by passing an incremental upgrade. The principle of incremental upgrade does not say, Baidu a bit can, directly say how to the incremental upgrade into the app. First, create a new incremental upgrade of the tool class Patchutil, define a local method patch.
Package Com.winter.updatedemo.utils;public class Patchutil {    static{        system.loadlibrary ("patch");    }    public static native int patch (string oldapkpath, String Newapkpath, String patchpath);}

Second, as menu bar, select Build->make Project in turn. When you're done, switch the project bar to Project view, and then turn on app->build->intermediates->classes->debug->com->winter-> Updetedemo->utils, you can see that there is a Patchutil.class file under the folder. Com->winter->updetedemo->utils This path is actually the package name of Patchutil, the actual development of the time please according to their own package name to find the appropriate path. Thirdly, we then use the. class file found above to generate a C + + header file. First open the terminal interface for AS and then enter the following two commands in turn CD app\build\intermediates\classes\debug   Javah-jni Com.winter.updatedemo.utils.PatchUtil, such as: then we look at the Debug folder, we will find a more com_winter_updatedemo_utils_ PatchUtil.h file, this is the header file we need. Contents of the file let's open it up and look at:
/* 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.
After the above steps are completed, we create a new android.mk file under the Jni folder, which is:
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?

The NDK development of the incremental upgrade feature under Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.