Android--------Ali andfix hot fix

Source: Internet
Author: User

Andfix, full name is Android Hot-fix. Ali Open source is a hot patch framework that allows the app to fix bugs on the line without republishing the release.

Devices that support Android 2.3 to 6.0 and support arm and X86 system architectures. Perfect support for Dalvik and art runtime, patch files are files ending with. Apatch.

Principle

The principle of andfix is replacing the method, replacing the method with the bug with the method in the patch file;

The structure information of the method is swapped directly in the native layer, which realizes the perfect method old and new substitution, thus realizes the hot repair function.

adding dependencies and confusion
Compile ' com.alipay.euler:andfix:[email protected] '
class extends java.lang.annotation.Annotation class * {    native <methods>;}

Permissions
<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>  <android:name= "Android.permission.READ_EXTERNAL_STORAGE" />

Application code (added in Andriodmanifest)
 Public classAndfixapplicationextendsApplication { Public StaticPatchmanager Mpatchmanager;  PublicString TAG = "Application"; Private Static FinalString Apatch_path = "/out.apatch";//Patch file name@Override Public voidonCreate () {Super. OnCreate (); //Initializing patch Management classesMpatchmanager =NewPatchmanager ( This); //initializing a patch versionMpatchmanager.init ("1.0");        Mpatchmanager.loadpatch (); Try {            //. apatch file path, it is important to note that each cell phone SD card path is differentString patchfilestring = Environment.getexternalstoragedirectory (). GetAbsolutePath () +Apatch_path; LOG.I (TAG,"OnCreate:" +patchfilestring); //3) Add patchMpatchmanager.addpatch (patchfilestring); LOG.D (TAG,"Apatch:" + patchfilestring + "added."); } Catch(IOException e) {log.e (TAG,"", E); }    }}

Generate bug apk files and build repaired apk files

The first time the APK file code indicates that there is a bug not fixed

  Private void Initview () {        = (TextView) Findviewbyid (R.id.textview);        Findviewbyid (R.ID.BTN). Setonclicklistener (new  View.onclicklistener () {            @Override             publicvoid  OnClick (View v) {                toast.maketext (mainactivity. This, "Bug not Fixed", Toast.length_long). Show ();                Textview.settext ("Bug not Fixed");}}        );    

Generate the signature file Andfix.jks then generate the apk file named old.apk

The second step to build bug fix apk file, my fix is simple, named new.apk

Private void Initview () {        = (TextView) Findviewbyid (R.id.textview);        Findviewbyid (R.ID.BTN). Setonclicklistener (new  View.onclicklistener () {            @Override             publicvoid  OnClick (View v) {                toast.maketext (mainactivity. This, "Bug fixed", Toast.length_long). Show ();                Textview.settext ("bug fixed");}            }        );    

Generate Patch files

Download the build tool: Https://github.com/alibaba/AndFix/tree/master/tools under Apkpatch-1.0.3.zip

Unzip the file, assuming in: F:\blog\android\java\git\three\rexiufu\aliyun\andfix\apkpatch\ directory ( indicating that it is easy for me to explain the patch file generation )

CMD Open command, switch to F:\blog\android\java\git\three\rexiufu\aliyun\andfix\apkpatch\ directory

Then copy the previously generated two apk files old.apk and new.apk to the directory, and copy the resulting signature file Andfix.jks to the directory.

Enter a command to generate a patch file, enter a command, and see below for success

Command: apkpatch.bat-f new.apk-t old.apk-o D:\Android-k andfix.jks-p 123456-a andfix-e 123456

Grammatical explanations

-F <new.apk>: New version
-T <old.apk>: older versions
-o <output>: Output directory
-K <keystore>: KeyStore used for packaging
-P <password>: keystore password
-a <alias>: KeyStore user Alias
-e <alias password>: KeyStore user alias password

Two files generated under the D:\Android directory

The second is the patch file, and we're going to use it to implement patch repair.

Copy the second file to the F:\blog\android\java\git\three\rexiufu\aliyun\andfix\apkpatch\ directory for easy submission to the phone's internal storage.

Rename to Out.apatch and the name of the Apatch_path in the preceding application class corresponds to

Private Static Final // Patch file name

Patch files fix old (bug) apk

Before submitting a patch file, install the bug apk on your phone and run it to see my:

Here we use the ADB command to upload patches to the local memory directory of the phone, and then fix the error,

For more information about ADB commands, see the blog:

After uploading successfully, we end bug application, reboot, Fix patch,:

Used in projects

General formal projects do not go like this requires the user to click on a button to load the repair package (Out.apatch), but when the user starts to enter our app, the first interface should go to the server access (generally an app will have an interface to get the server to the client's global configuration).

Assuming that the server returned with the updated package, should be 弹框 to download the apatch file , this time the user can not do anything, it is best to add a progress bar, when the download is complete, then repair, and finally jump to the main interface.

It is best to use HTTPS to ensure the security of data, when we access the server if there is a repair package, if there is, the server should return the repair file apatch and the MD5 value of the file,

After the client download is finished, the file is MD5 and then compared to the MD5 value returned by the server to see if it is consistent, because the file may be tampered with, security issues, the MD5 encrypted value and the background staff consulted, consistent.

Limitations
    • YunOS not supported
    • Unable to add new class and new field
    • Need to use the pre-hardening apk to make patches, but the patch file is easy to decompile, that is, the modified class source easily leaked.
    • Some phones do not support, love error

Therefore, it is not recommended.

Conclusion:

My demo Test release version (need to configure signature) I tested successfully, can download reference

Github:https://github.com/dickyqie/android-andfix

Andfix official website source code and case: Https://github.com/alibaba/AndFix

Android--------Ali andfix hot fix

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.