热补丁介绍及Andfix的使用

热补丁介绍及Andfix的使用
Andfix热修复框架原理及源码解析-上篇

Andfix热修复框架原理及源码解析-下篇


一、热补丁概念

指能够修复软件漏洞的一些代码,是一种快速、低成本修复产品软件版本缺陷的方式

二、热补丁的作用(结合公司项目)

1.对于移动端来说,如果线上包出现不是很严重的问题,我们一般处理方式都是把新包放在官网,或者后台那边线上代码能否改下。这样导致用户出现问题就需要卸载当前包,去官网重新下载。用户体验很不好。

2.版本迭代时,偶尔会出现,一些新功能,由于以前的代码没有考虑周全,导致如果发布新包,以前的老包(apk包)会不兼容,出问题。

以上热补丁都可以解决。

三、热补丁项目

1.Dexposed   https://github.com/alibaba/dexposed

2. AndFix     https://github.com/alibaba/AndFix

3.ClassLoader 开源实现: Nuwa , HotFix , DroidFix

12分别是阿里巴巴淘宝和支付宝团队开发,3是腾讯QQ空间团队负责开发的,基于它比较出名的有Nuwa , HotFix , DroidFix。有兴趣的可以深入研究下,这个我就不进一步介绍了。

四、热补丁项目优缺点

1.说明之前,简单介绍下ART模式和Dalvik模式,Dalvik就相当于一辆组装的自行车,必须要组装好才能骑,ART好比一辆不需要组装就能骑的自行车,它的效率高很多。在Android4.4之后就开始使用。

2.Dexposed:不支持Art模式(5.0+),写补丁困难。ClassLoader:兼容性稳定,但需要重启。AndFix不重启就能修复,而且写补丁的方法很简单。

五、Andfix的使用

1.如果是使用AndroidStudio,可以直接在build.gradle添加compile'com.alipay.euler:andfix:0.3.1@aar‘。我是使用module的方式添加andfix,这样可以直接查看编辑源码。记得新建jniLibs文件夹,将AndFix里面的libs里的so文件移到jniLibs里。

目录结构:


2.步骤

A.Application中进行初始化

package com.example.lhx.andfixdemo;

import android.app.Application;
import android.os.Environment;
import android.util.Log;

import com.alipay.euler.andfix.patch.PatchManager;

import java.io.File;
import java.io.IOException;

/**
 * sample application
 *
 */
public class MainApplication extends Application {
    private static final String TAG = " andrew";

    private static final String APATCH_PATH = "/out.apatch";

    private static final String DIR = "apatch";//补丁文件夹
    /**
     * patch manager
     */
    private PatchManager mPatchManager;

    @Override
    public void onCreate() {
        super.onCreate();
        // initialize
        mPatchManager = new PatchManager(this);
        mPatchManager.init("1.0");
        Log.d(TAG, "inited.");

        // load patch
        mPatchManager.loadPatch();
        try {
            // .apatch file path
            String patchFileString = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + APATCH_PATH;
            mPatchManager.addPatch(patchFileString);
            Log.d(TAG, "apatch:" + patchFileString + " added.");

            //复制且加载补丁成功后,删除下载的补丁
            File f = new File(this.getFilesDir(), DIR + APATCH_PATH);
            if (f.exists()) {
                boolean result = new File(patchFileString).delete();
                if (!result)
                    Log.e(TAG, patchFileString + " delete fail");
            }
        } catch (IOException e) {
            Log.e(TAG, "", e);
        }
    }
}

B.demo的场景就是一开始点击确定按钮textview显示I am old.然后假设这个方法有问题,我改为I am new.区分新包和旧包。

 

package com.example.lhx.andfixdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


/**
 * main activity
 */
public class MainActivity extends Activity {
    private TextView mShowTextView;
    private Button mSureButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mShowTextView = (TextView) this.findViewById(R.id.show_tv);
        mSureButton = (Button) this.findViewById(R.id.sure_btn);

        //打包1.apk后,修改textviewt内容,打包2.apk
        mSureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowTextView.setText("I am new");
            }
        });
    }
}

热补丁介绍及Andfix的使用_第1张图片

C.混淆

 

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in E:\android_sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}
-optimizationpasses 5                                                           # 指定代码的压缩级别
-dontusemixedcaseclassnames                                                     # 是否使用大小写混合
-dontskipnonpubliclibraryclasses                                                # 是否混淆第三方jar
-dontpreverify                                                                  # 混淆时是否做预校验
-verbose                                                                        # 混淆时是否记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*        # 混淆时所采用的算法

#重要,別忘了這些,不混淆andfix包,不混淆native方法
-dontwarn android.annotation
-dontwarn com.alipay.euler.**
-keep class com.alipay.euler.** {*;}
-keep class * extends java.lang.annotation.Annotation
-keepclasseswithmembernames class * {
    native <methods>;
}<span style="font-size:18px;"></span>

D.使用工具打包补丁文件:apkpatch-1.0.3(我放到后面的链接里可以直接下载)

1)注意要把你的keystone文件,新包和旧包一同放在apkpatch-1.0.3文件里面。这里注意下,apk指的是没有经过第三方加固的。

 

2然后win+R调出cmd命令

3)这里对命令先做一个简单介绍,后面也会特意分析这个打包工具。直接上图。

apkpatch.bat -f new.apk -t old.apk -o output -k lhxtest.jks -p 123456 -a 123456 -e 123456

 热补丁介绍及Andfix的使用_第2张图片

4)命令成功后,会在目录下生成一个输出文件夹,文件夹里面就有扩展名为.patch的文件,把它改为out.patch.(PS:新手不熟悉命令行的话,记得一定要cdapkpatch-1.0.3目录下,你可以直接cd然后加上那个目录的路径就可以了,然后再输入命令)

 热补丁介绍及Andfix的使用_第3张图片

热补丁介绍及Andfix的使用_第4张图片

热补丁介绍及Andfix的使用_第5张图片

 

5)为了检测,首先运行旧包,然后把out.patch文件负责到在手机根目录下,再重新打开app,你会发现点击确定按钮,textview显示为”I am new”就表示方法修复成功了!!想要进一步了解怎么实现的,可以看我写的Andfix热修复框架原理及源码解析-上篇

 热补丁介绍及Andfix的使用_第6张图片热补丁介绍及Andfix的使用_第7张图片


网友总结的可以修复的地方:

热补丁介绍及Andfix的使用_第8张图片

转载请注明转自:http://blog.csdn.net/u011176685/article/details/50984638

相关资料工具及demo下载地址:http://pan.baidu.com/s/1hsdcs7a

你可能感兴趣的:(热补丁介绍及Andfix的使用)