[关闭]
@linux1s1s 2017-01-22T16:54:57.000000Z 字数 5401 阅读 2837

Android 在线热修复框架 AndFix 初步一

AndroidExtend 2016-03


AndFix工作流程

关于AndFix的基本使用和使用流程在AndFix官方Github上已经说的比较详细,这里提取一下基本的修复流程,帮助初次使用者快速的使用AndFix,下面是简要流程图。

此处输入图片的描述

上面的流程不细说,接下来看看github上Download下来的代码导入Eclipse,对于Android Studio导入不再给出。

AndFix Demo

此处输入图片的描述

按照上面的流程图,这个apk就是线上的APK文件,将这个apk重命名为old.apk以备用

线上的MainActivity类如下

  1. public class MainActivity extends Activity {
  2. private static final String TAG = "euler";
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. Log.e(TAG, A.a("good"));
  7. Log.e(TAG, "" + new A().b("s1", "s2"));
  8. Log.e(TAG, "" + new A().getI());
  9. }
  10. @Override
  11. protected void onDestroy() {
  12. super.onDestroy();
  13. android.os.Process.killProcess(android.os.Process.myPid());
  14. }
  15. }

修复以后的

  1. public class MainActivity extends Activity {
  2. private static final String TAG = "euler";
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. Log.e(TAG, Fix.a("good"));
  7. Log.e(TAG, "" + new Fix().b("s1", "s2"));
  8. Log.e(TAG, "" + new Fix().getI());
  9. }
  10. @Override
  11. protected void onDestroy() {
  12. super.onDestroy();
  13. android.os.Process.killProcess(android.os.Process.myPid());
  14. }
  15. }

这里的命令稍微说明一下:(参数必选,如果没有输入会一直提示你正取的输入姿势,稍后我们来解答为什么需要这么严格的限制)

  1. 命令 : apkpatch.bat -f new.apk -t old.apk -o output1 -k debug.keystore -p android -a androiddebugkey -e android
  2. -f <new.apk> :新版本
  3. -t <old.apk> : 旧版本
  4. -o <output> 输出目录
  5. -k <keystore>: 打包所用的keystore
  6. -p <password>: keystore的密码
  7. -a <alias>: keystore 用户别名
  8. -e <alias password>: keystore 用户别名密码
  1. public class MainApplication extends Application {
  2. private static final String TAG = "euler";
  3. private static final String APATCH_PATH = "/Download/out.apatch";
  4. /**
  5. * patch manager
  6. */
  7. private PatchManager mPatchManager;
  8. @Override
  9. public void onCreate() {
  10. super.onCreate();
  11. // initialize
  12. mPatchManager = new PatchManager(this);
  13. mPatchManager.init("1.0");
  14. Log.d(TAG, "inited.");
  15. // load patch
  16. mPatchManager.loadPatch();
  17. Log.d(TAG, "apatch loaded.");
  18. // add patch at runtime
  19. try {
  20. // .apatch file path
  21. String patchFileString = Environment.getExternalStorageDirectory()
  22. .getAbsolutePath() + APATCH_PATH;
  23. mPatchManager.addPatch(patchFileString);
  24. Log.d(TAG, "apatch:" + patchFileString + " added.");
  25. } catch (IOException e) {
  26. Log.e(TAG, "", e);
  27. }
  28. }
  29. }

L25 就是指定加载的patch文件目录,所以我们将上面生成的patch文件放到内部存储的.../Download/out.apatch目录即可

为什么说是bug修复了呢,看看以下源码就清楚了
Class A

  1. public class A {
  2. String s = "s";
  3. private static O o = new O("a");
  4. static int i = 10;
  5. public static String a(String str) {
  6. Log.i("euler", "fix error");
  7. return "a";
  8. }
  9. public int b(String s1, String s2) {
  10. Log.i("euler", "fix error");
  11. Log.i("euler", o.s);
  12. return 0;
  13. }
  14. public int getI() {
  15. return i;
  16. }
  17. }

Class Fix

  1. public class Fix {
  2. String s = "s";
  3. private static O o = new O("fix");
  4. static int i = 11;
  5. public static String a(String str) {
  6. Log.d("qq", str);
  7. Log.i("euler", "fix succes");
  8. return "b";
  9. }
  10. public int b(String s1, String s2) {
  11. Log.e("euler", s1);
  12. Log.e("euler", i + "==" + c());
  13. Log.i("euler", "fix succes" + s);
  14. Log.i("euler", o.s);
  15. return 0;
  16. }
  17. private String c() {
  18. return "a";
  19. }
  20. public int getI() {
  21. return i;
  22. }
  23. }

AndFix 测试

测试机器:Genymotion
Android版本:2.3 - 5.1
测试结果:4.0 - 5.1 均通过,2.3Crash,附上Log如下(真机未测试)

  1. 03-03 01:44:29.000: E/AndroidRuntime(502): FATAL EXCEPTION: main
  2. 03-03 01:44:29.000: E/AndroidRuntime(502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.euler.andfix/com.euler.andfix.MainActivity}: java.lang.NullPointerException: null result when primitive expected
  3. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  4. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  5. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  6. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  7. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.os.Handler.dispatchMessage(Handler.java:99)
  8. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.os.Looper.loop(Looper.java:130)
  9. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread.main(ActivityThread.java:3683)
  10. 03-03 01:44:29.000: E/AndroidRuntime(502): at java.lang.reflect.Method.invokeNative(Native Method)
  11. 03-03 01:44:29.000: E/AndroidRuntime(502): at java.lang.reflect.Method.invoke(Method.java:507)
  12. 03-03 01:44:29.000: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  13. 03-03 01:44:29.000: E/AndroidRuntime(502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  14. 03-03 01:44:29.000: E/AndroidRuntime(502): at dalvik.system.NativeStart.main(Native Method)
  15. 03-03 01:44:29.000: E/AndroidRuntime(502): Caused by: java.lang.NullPointerException: null result when primitive expected
  16. 03-03 01:44:29.000: E/AndroidRuntime(502): at com.euler.andfix.MainActivity.onCreate(Native Method)
  17. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  18. 03-03 01:44:29.000: E/AndroidRuntime(502): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  19. 03-03 01:44:29.000: E/AndroidRuntime(502): ... 11 more

AndFix 局限性

上面埋下了一个坑,就是apkpatch工具输入的参数为什么是必须的,这个坑准备在下一篇博文中填,另外AndFix源码解析部分也会有博文更新,未完待续...

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注