接上文,上文AndroidStudio 实现hotfix热修复一 的地址为:
https://mp.csdn.net/postedit/88535620
到上文,就已配置好 manifests文件与gradle中的相关内容。
接下来,就是具体的功能代码的例子。
首先第一步,创建一个application类,如下所示:
该类必须继承
SophixApplication类,
并且这个类要声明到配置文件中:
下面来看看 这个类中的关键代码:
首先,需要重写一个方法:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
initSophix(); //此处为热更新部分的封装方法
}
重写oncreat方法 如下所示:
@Override
public void onCreate() {
super.onCreate();
//热修复
SophixManager.getInstance().queryAndLoadNewPatch();
}
下面是热修复封装方法中的具体内容:
private void initSophix() {
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
String appVersion = "1.0";
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
Log.i(TAG, "此时的版本号"+appVersion);
} catch (Exception e) {
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
.setAppVersion(appVersion)
.setSecretMetaData(getResources().getString(R.string.app_key), getResources().getString(R.string.app_secret), getResources().getString(R.string.app_rsa))
.setEnableDebug(true)
.setEnableFullLog()
.setPatchLoadStatusStub(new PatchLoadStatusListener() {
@Override
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Log.i(TAG, "sophix load patch success!");
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
// 如果需要在后台重启,建议此处用SharePreference保存状态。
//Log.i(TAG, "sophix preload patch success. restart app to make effect.");
SharedPreferences.Editor editor = sp.edit();
editor.putString("hotFix","b1");
editor.apply();
editor.commit();
Log.i(TAG, "sophix need reload!");
}else if (code==PatchStatus.CODE_UNZIP_FAIL){
Log.i(TAG, "sophix load patch failed!");
}else {
Log.i(TAG, "sophix load other false!");
}
}
}).initialize();
}
.setSecretMetaData(getResources().getString(R.string.app_key), getResources().getString(R.string.app_secret), getResources().getString(R.string.app_rsa))
这个方法中的三个参数,就是我们配置文件中的三个参数,我这边是把这些参数写到string.xml 文件中,直接进行调用,大家可以参考,也可以直接写对应的值。
完整的application 可以参考我的application类的写法:
package com.example.administrator.myhotfixdemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Keep;
import android.util.Log;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;
/**
* 代码入口类,配置文件中的application类
*/
public class SophixStudApplication extends SophixApplication {
private SharedPreferences sp;
private String TAG = "SophixStudApplication";
/*@Keep
@SophixEntry(MyApplication.class)
static class RealApplication {
}*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
initSophix();
}
@Override
public void onCreate() {
super.onCreate();
//热修复
SophixManager.getInstance().queryAndLoadNewPatch();
}
private void initSophix() {
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
String appVersion = "1.0";
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
Log.i(TAG, "此时的版本号"+appVersion);
} catch (Exception e) {
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
.setAppVersion(appVersion)
.setSecretMetaData(getResources().getString(R.string.app_key), getResources().getString(R.string.app_secret), getResources().getString(R.string.app_rsa))
.setEnableDebug(true)
.setEnableFullLog()
.setPatchLoadStatusStub(new PatchLoadStatusListener() {
@Override
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Log.i(TAG, "sophix load patch success!");
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
// 如果需要在后台重启,建议此处用SharePreference保存状态。
//Log.i(TAG, "sophix preload patch success. restart app to make effect.");
SharedPreferences.Editor editor = sp.edit();
editor.putString("hotFix","b1");
editor.apply();
editor.commit();
Log.i(TAG, "sophix need reload!");
}else if (code==PatchStatus.CODE_UNZIP_FAIL){
Log.i(TAG, "sophix load patch failed!");
}else {
Log.i(TAG, "sophix load other false!");
}
}
}).initialize();
}
}