Android 热修复 使用Demo

2017-02-27  本文已影响225人  iehshx

Fix 框架 使用步骤和Demo 演示。
之前我们分享了fix 的导入,现在我们一起来写一个使用的demo。
关于如何导入请参考之前的文章 :

http://www.jianshu.com/p/28b1eb8c2525

新建Android Studio 项目 FixDemo,导入之前的配置。
由于我是第一次使用所以之前没有发布过版本,所以在配置的时候需要注意 oldHotFixDir 不需要传入值。

hotFix {
includePackage = [];
excludeClass = [];
oldHotFixDir = ""; }

新建MyApp记成Application,重写attachBaseContext对fix进行初始化。

public class MyApp extends Application {

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    HotFix.init(this);
    if (new File(Environment.getExternalStorageDirectory().getAbsolutePath().concat("/patch.jar")).exists())
    HotFix.loadPatch(this, Environment.getExternalStorageDirectory().getAbsolutePath().concat("/patch.jar"));
} }

新建MainActivity,加载布局activity_main.xml。布局也没什么只是一个TextView。*

<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iehshx.fix.demo.MainActivity">
     <TextView
        android:id="@+id/fixText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
布局展示

在MainActivity对TextView设置一段文本。

public class MainActivity extends AppCompatActivity {

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

  TextView textView = (TextView) findViewById(R.id.fixText);
    textView.setText("出现bug 需要修复bug");
} }

然后在AndroidManifest.xml中修改application为我们自己的APP,并且申请sd卡的权限

 <application
        android:allowBackup="true"
        android:name=".MyApp"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

然后运行项目


这里写图片描述

ok已经成功,没有出现问题。
现在用户反馈有bug 需要修复,所以我们需要打patch包。
在打patch包之前我们需要将在线版本的混淆文件和映射文件保存到C:hotfix目录下。
文件在我们项目的app\build\outputs\hotfix 直接将目录拷贝到C盘即可。

然后我们修改MainActivity中的代码

public class MainActivity extends AppCompatActivity {

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

  TextView textView = (TextView) findViewById(R.id.fixText);
    textView.setText("bug 已经修复好 可以使用了");
} }

我们运行在另外一个设备里


这里写图片描述

这时会在项目的app\build\outputs\hotfix\Debug\patch下生成patch.jar包 这就是我们的补丁包。
然后将我们的补丁包 传到手机sd卡里。


这里写图片描述

adb push patch.jar sdcard/

然后重启应用就可以了


这里写图片描述

有的同学重启后发现并没有修复bug,那是因为我们没有给应用赋予权限


这里写图片描述
在APP里面设置storage权限,再重启App就可以了。

还有的同学在运行的时候发现提示


这里写图片描述

这是因为你开启了混淆代码,需要在混淆的配置中加入:

-keep class com.iehshx.fixlibrary.** { ; }
-dontwarn com.iehshx.hot.Hack
-dontwarn com.iehshx.fixlibrary.
*

以上就是使用demo 代码稍后我会传到github上。

https://github.com/iehshx/FixDemo.git

上一篇下一篇

猜你喜欢

热点阅读