1. 程式人生 > >Android 熱修復框架 AndFix (三)

Android 熱修復框架 AndFix (三)

這篇文章將演示AndFix框架細節問題。在看這篇文章之前請先看Android 熱修復框架 AndFix (二)。這篇文章是居於上一篇:Android 熱修復框架 AndFix (二) AndroidFix專案的。

1.修改佈局檔案

1.先簽名打包apk

打包簽名生成app-release.apk, 然後改名成app-release1.0.apk,備用

2.修改activity_main.xml

新增一個註冊的 button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
.... <Button android:id="@+id/btn_register" android:layout_width
="match_parent" android:layout_height="60dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:text="註冊" />
</LinearLayout>

2.打包簽名,生成補丁

1.打包簽名生成app-release.apk, 並改名成app-release1.0.fix.apk

2.使用apkpatch-1.0.3

工具生成補丁out.apatch

apkpatch -f app-release1.0.fix.apk -t app-release1.0.apk -o apk -k androidfix.jks -p androidfix -a androidfix -e androidfix

3.在生成補丁的時候報錯:

C:\Users\Administrator\Desktop\apkpatch-1.0.3>apkpatch -f app-release1.0.fix.apk
 -t app-release1.0.apk -o apk -k androidfix.jks -p androidfix -a androidfix -e a
ndroidfix

java.lang.RuntimeException: can,t modified Field:action0(I), in class :Landroid/
support/v7/appcompat/R$id;
        at com.euler.patch.diff.DiffInfo.addModifiedFields(DiffInfo.java:88)
        at com.euler.patch.diff.DexDiffer.compareField(DexDiffer.java:125)
        at com.euler.patch.diff.DexDiffer.compareField(DexDiffer.java:101)
        at com.euler.patch.diff.DexDiffer.compareField(DexDiffer.java:95)
        at com.euler.patch.diff.DexDiffer.diff(DexDiffer.java:32)
        at com.euler.patch.ApkPatch.doPatch(ApkPatch.java:68)
        at com.euler.patch.Main.main(Main.java:97)

4.總結:

AndFix 不支援修改佈局檔案

2.讀取資原始檔(Assets)

1.先簽名打包apk

打包簽名生成app-release.apk, 然後改名成app-release1.0.apk,備用

2.在AndroidFix專案中增加Assets目錄

在Assets目錄中新增config.txt檔案, 在檔案中輸入:AndFix text from assets ! 字串

3.修改MainActivity

package xmg.com.androidfix;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static android.R.attr.password;


public class MainActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        try {
            //讀取assets中的資源
            InputStream in = getResources().getAssets().open("config.txt");
            BufferedReader reader=new BufferedReader(new InputStreamReader(in));
            String s = reader.readLine();
            btn_login.setText(s);
            Log.d("TAG","assets="+s);
        } catch (IOException e) {
            Log.d("TAG","assets="+e);
            e.printStackTrace();
        }

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              ...
              ...
            }
        });

    }


}

3.簽名打包,生成補丁

1.打包簽名生成app-release.apk, 並改名成app-release1.0.fix.apk

2.使用apkpatch-1.0.3工具生成補丁out.apatch

apkpatch -f app-release1.0.fix.apk -t app-release1.0.apk -o apk -k androidfix.jks -p androidfix -a androidfix -e androidfix

3.把out.apatch補丁push到手機中:

5.重新啟動APP軟體時:

1.控制檯輸出:

assets=AndFix text  from assets !

2.並且介面的登入button字型也改成了:

AndFix text from assets !

4.總結

AndFix 支援讀取資原始檔(Assets)

* 3.新增成員變數*

1.先簽名打包apk

打包簽名生成app-release.apk, 然後改名成app-release1.0.apk,備用

2.修改MainActivity

package xmg.com.androidfix;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static android.R.attr.password;


public class MainActivity extends AppCompatActivity {
    //3.新增成員變數
    private String mButtonName="註冊";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        //3.新增成員變數
        btn_login.setText(mButtonName);
        Log.d("TAG","mButtonName="+mButtonName);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              ...
              ...
            }
        });

    }


}

2.簽名打包,生成補丁

1.打包簽名生成app-release.apk, 並改名成app-release1.0.fix.apk

2.使用apkpatch-1.0.3工具生成補丁out.apatch

apkpatch -f app-release1.0.fix.apk -t app-release1.0.apk -o apk -k androidfix.jks -p androidfix -a androidfix -e androidfix

3.把out.apatch補丁push到手機中:

4.重新啟動App軟體時:

1).控制檯列印:

mButtonName=註冊

2).並且介面的登入button字型也改成了:註冊

3.總結

AndFix 支援在Activity中新增成員變數

* 4.新增匿名內部類*

1.先簽名打包apk

打包簽名生成app-release.apk, 然後改名成app-release1.0.apk,備用

2.修改MainActivity

package xmg.com.androidfix;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static android.R.attr.password;


public class MainActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        //4.新增匿名內部類,點選事件 View.OnClickListener()是一個匿名內部類
        img_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"header",Toast.LENGTH_SHORT).show();
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              ...
              ...
            }
        });

    }


}

2.簽名打包,生成補丁

1.打包簽名生成app-release.apk, 並改名成app-release1.0.fix.apk

2.使用apkpatch-1.0.3工具生成補丁out.apatch

apkpatch -f app-release1.0.fix.apk -t app-release1.0.apk -o apk -k androidfix.jks -p androidfix -a androidfix -e androidfix

3.把out.apatch補丁push到手機中:

4.重新啟動APP軟體時:

1).控制檯列印:

Could not find class 'android.graphics.drawable.RippleDrawable', 
referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

3.總結

AndFix 不支援新增匿名內部類(就是點選事件)

5.新增新的類

1.新增一個新的類,並且使用該新的類會包如下的錯誤:

Could not find class 'android.graphics.drawable.RippleDrawable', 
referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

總結

AndFix 不支援新增新的類

6.在Activity可以新增新的方法

總結

AndFix 支援在Activity新增新的方法

7.連續修復bugs

1.打了一次補丁的apk是否還可以打補丁? 可以的

例如:

第一次打補丁:

app-release1.0.apk 與 app-release1.0.fix1.apk 生出補丁out.apatch

第二次打補丁:

app-release1.0.fix1.apk 與 app-release1.0.fix2.apk 生出補丁outs.apatch

總結

AndFix 支援連續打補丁(注意補丁的名字要求不一樣)