Incremental upgrade (Save traffic update) for Android client implementation

Source: Internet
Author: User

Reproduced with

Zhouhuiah's Column http://blog.csdn.net/zhouhuiah/article/details/16939937

This article adds exception handling based on the above two blogs and packages the resulting so library with the native code.

1. Preparation Tools

(1) Bspatch Source (click to download), two different versions of an application. or directly download the tools and materials provided by the first blogger mentioned above. Click to open the link here The bread includes the Bsdiff source code and APK that we need to use.

(2) In addition, you need to download bzip2. Click to open link

2. Compiling environment

Linux, Windows can, but must have ndk,windows under the NDK version needs to be above R7, otherwise need to install Cygwin. Under Windows to build the NDK environment, please refer to: NDK configuration

3. Code implementation

(1) Create Native method class

Look at the code first:

[Java]View Plaincopy
  1. Public class patchclient{
  2. //define native method
  3. static private native int applypatchtooldapk (string oldapk_filepath, String Newapk_savepath, String  Patchpath);
  4. public static void Loadlib () {
  5. System.loadlibrary ("patchdroid");
  6. }
  7. /** 
  8. *
  9. * @param oldapkpath The path to the old apk file
  10. * @param newapkpath The path of the new APK file
  11. * @param the path of the Patchpath incremental package
  12. * @throws IOException
  13. */
  14. public static void Applypatch (String oldapkpath, String Newapkpath, String Patchpath) throws IOException {  
  15. applypatchtooldapk (Oldapkpath, Newapkpath, Patchpath);
  16. }
  17. /** 
  18. * Get the old apk file of this app according to the context, merge with the incremental package to generate the new version apk
  19. * @param context
  20. * @param newapkpath new apk file path
  21. * @param patchpath Incremental Package path
  22. * @throws IOException
  23. */
  24. public static void Applypatchtoown (context context, string Newapkpath, String Patchpath) throws ioexception{
  25. String old = Context.getapplicationinfo (). SourceDir;
  26. APPLYPATCHTOOLDAPK (old, Newapkpath, Patchpath);
  27. }
  28. }

The focus is on the third line of the code, and several other methods are described later. After compiling, the. class file is generated under the project's bin/classes, open cmd, cut to the directory, enter

[Plain]View Plaincopy
    1. Javah cn.sgwhp.PatchDroid (package name. Class name)

The header file Cn_sgwhp_patchdroid_patchclient.h is generated, the JNI folder is created at the root of the project, and the. h file that you just generated is cut past.

(2) Implement native method

Unzip the downloaded BZIP2 package and Bspatch source to the JNI directory, bspatch.c named cn_sgwhp_patchdroid_patchclient.c, Implementing JAVA_CN_SGWHP_PATCHDROID_ Patchclient_applypatchtooldapk method. Here we simply invoke the main method of the source code to implement:

[CPP]View Plaincopy
  1. #include "Cn_sgwhp_patchdroid_patchclient.h"
  2. #include "Bzlib_private.h"
  3. Jniexport jint jnicall java_cn_sgwhp_patchdroid_patchclient_applypatchtooldapk (jnienv *env,
  4. Jobject obj, jstring old, jstring new, jstring patch) {
  5. int argc=4;
  6. char * ARGV[ARGC];
  7. argv[0]="Bspatch";
  8. argv[1]= (char*) ((*env)->getstringutfchars (env,old, 0));
  9. argv[2]= (char*) ((*env)->getstringutfchars (env,new, 0));
  10. argv[3]= (char*) ((*env)->getstringutfchars (env,patch, 0));
  11. int Ret=applypatch (argc, argv, env); //The main method in the source code can be changed to Applypatch
  12. (*env)->releasestringutfchars (env,old,argv[1]);
  13. (*env)->releasestringutfchars (env,new,argv[2]);
  14. (*env)->releasestringutfchars (env,patch,argv[3]);
  15. return ret;
  16. }

(3) Exception handling

Only C in the source code err exception, Java is no way to output these exception information in the console, we will transform it, using jnienv Thrownew method to throw IOException. Add the Throwioexception method to the CN_SGWHP_PATCHDROID_PATCHCLIENT.C and remove the env parameter from the Java_cn_sgwhp_patchdroid_patchclient_ applypatchtooldapk Incoming Applypatch:

[CPP]View Plaincopy
  1. void Throwioexception (jnienv* env,const char* msg)
  2. {
  3. Find class type for the specified name
  4. Jclass cls= (*env)->findclass (env,"java/io/ioexception");
  5. (*env)->thrownew (env,cls,msg);
  6. (*env)->deletelocalref (ENV,CLS);
  7. }
  8. int Applypatch (int argc,char * argv[],jnienv* env)
  9. {  ...}

Comment out all code in the Applypatch method that calls the Err method, and change it to:

[CPP]View Plaincopy
    1. Throwioexception (env, "Can not open the patch file");
    2. return 0

At this point, Applypatch and Applypatchtoown in the native method class declare the IOException to be thrown, preventing the program from exiting because of an exception.

(4) Compiling

Create the Android.mk file under the JNI directory and copy the following code:

[CPP]View Plaincopy
  1. Local_path:= $ (call My-dir)
  2. Include $ (clear_vars)
  3. # This is the target being built.
  4. local_module:= patchdroid
  5. # All of the source files that we'll compile.
  6. # exactly what C code is needed, not carefully studied
  7. local_src_files:= cn_sgwhp_patchdroid_patchclient.c \
  8. BZLIB.C \
  9. BLOCKSORT.C \
  10. COMPRESS.C \
  11. CRCTABLE.C \
  12. DECOMPRESS.C \
  13. HUFFMAN.C \
  14. RANDTABLE.C \
  15. BZIP2.C \
  16. Ifeq ($ (host_os), Windows)
  17. #NDK环境下
  18. Local_ldlibs: =-llog
  19. Else
  20. #完整源码环境下
  21. Local_shared_libraries: = Libutils
  22. endif
  23. Local_shared_libraries: = \
  24. Libandroid_runtime
  25. # No static libraries.
  26. Local_static_libraries: = \
  27. Libbz
  28. # Also need the JNI headers.
  29. Local_c_includes + = \
  30. $ (jni_h_include) external/bzip2
  31. # No Special Compiler flags.
  32. Local_cflags + =
  33. Include $ (build_shared_library)

Refresh the project the NDK will automatically compile for us.

4. Packing

Right-click the project, select Properties, select Android, tick the is Library, save the Clean project, and a jar package will be generated in the bin directory. When other projects need to be used, the jar package is imported and the Armeabi directory under the Libs directory is copied to the Libs directory of the new project.

5, something

For Applypatchtoown (context context, string Newapkpath, String Patchpath) method: After the program is installed, the APK file will exist in the/data/app directory, the name is "package name-number." APK ", where" numbers "are generally 1 or 2, it is unclear what the rules are. But it doesn't matter, we can get the current version of the APK absolute path through ApplicationInfo SourceDir, so as long as the incremental package download to the SD card can be synthesized new version of the APK. Remember to add read and write permissions to the external storage device.

Incremental upgrade (Save traffic update) for Android client implementation

Related Article

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.