首先,登录https://homenew.console.aliyun.com/ ,选择移动测试后,进行项目创建
创建后点击进入,创建子模块,我创建了一个叫HotFixDemo的,创建中会提供连接下载Json配置文件
将json文件复制到project的根目录下
接下来配置gradle远程仓库依赖, 打开项目找到app的build.gradle文件,添加如下配置:
添加maven仓库地址:
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
}
添加gradle坐标版本依赖:
compile 'com.aliyun.ams:alicloud-android-hotfix:3.2.3'
在AndroidManifest中添加相关权限
接下来,在AndroidManifest.xml
中间的application
节点下添加如下配置:
App ID为 Json文件中的App Key
App Secret 为Json 文件的App Secret
RSA密钥对应json文件的 rsaSecret (特别长一串)
接下来,设置一个Application ,继承系统的Application ,在attachBaseContext中进行初始化操作(否则会无法打包补丁)。示例如下
initHotFix执行初始化操作,同时,如果收到更新推送,会执行 New PatchLoadStatusListener 的onLoad回调;
public class AppApplication extends Application {
public interface MsgDisplayListener {
void handle(String msg);
}
public static MsgDisplayListener msgDisplayListener = null;
public static StringBuilder cacheMsg = new StringBuilder();
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
initHotfix();
}
private void initHotfix() {
String appVersion;
try {
appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
} catch (Exception e) {
appVersion = "1.0.0";
}
SophixManager.getInstance().setContext(this)
.setAppVersion(appVersion)
.setAesKey(null)
//.setAesKey("0123456789123456")
.setEnableDebug(true)
.setPatchLoadStatusStub(new PatchLoadStatusListener() {
@Override
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
String msg = new StringBuilder("").append("Mode:").append(mode)
.append(" Code:").append(code)
.append(" Info:").append(info)
.append(" HandlePatchVersion:").append(handlePatchVersion).toString();
if (msgDisplayListener != null) {
Toast.makeText(getApplicationContext(),"code",Toast.LENGTH_SHORT).show();
msgDisplayListener.handle(msg);
} else {
cacheMsg.append("\n").append(msg);
}
}
}).initialize();
}
}
我们再在AndroidManifest中注册这个APPLICATION,并在Application节点添加如下设置
在MainActivity中,我们可以通过调用方法进行手动查询更新
public void click(View view) {
// mStatusTv.setText("修改");
// 主动更新查询
SophixManager.getInstance().queryAndLoadNewPatch();
Toast.makeText(this, "版本1.0", Toast.LENGTH_SHORT).show();
}
调用前需要先进行设置动态权限请求(SDK>=23)
private void requestExternalStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE_PERMISSION:
if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
updateConsole("local external storage patch is invalid as not read external storage permission");
}
break;
default:
}
}
这样, 就已经完成了热修复的基本操作。接下来只需要在创建的模块中设置补丁和版本
补丁设置具体参考官方资料:
补丁 :https://help.aliyun.com/knowledge_detail/70239.html?spm=a2c4g.11186623.6.585.4efa2a55KRtkOz
参考资料:官网文档 https://help.aliyun.com/document_detail/69874.html?spm=a2c4g.11186623.6.554.67f5737dhfr3dk