Sophix平台:https://emas.console.aliyun.com/
移动热修复(Mobile Hotfix)是面向移动互联网的APP热修复解决方案。产品基于阿里巴巴首创Hotpatch技术,提供细粒度热修复能力,无需等待,实时修复应用线上问题。
说明:Sophix平台创建应用,创建完根据提示下载aliyun-emas-services.json文件,需要用到三个参数:App ID、App Secret、RSA密钥。
接入SDK
编辑项目级build.gradle
buildscript {
repositories {
google()
jcenter()
//阿里
maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
//阿里
// 添加emas-services插件
classpath 'com.aliyun.ams:emas-services:1.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
//阿里
maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
编辑app级build.gradle
在 apply plugin: 'com.android.application'下面加入
apply plugin: 'com.android.application'
在dependencies里加入
//阿里SopHix热修复
implementation 'com.aliyun.ams:alicloud-android-hotfix:3.2.6'
编辑 AndroidManifest.xml
在application
在application里加入
把下载的aliyun-emas-services.json文件放到项目app目录下
如果运行报错
java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs
解决方法
打开AndroidManifest.xml
点击下面的Merged Manifest
这时你会看到具体报错信息,修改掉即可
MainActivity
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_EXTERNAL_STORAGE_PERMISSION = 0;
private TextView mStatusTv;
private String mStatusStr = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatusTv = findViewById(R.id.text_tv);
updateConsole(MyApplication.cacheMsg.toString());
changedImageViewSrc();
if (Build.VERSION.SDK_INT >= 23) {
requestExternalStoragePermission();
}
MyApplication.msgDisplayListener = new MyApplication.MsgDisplayListener() {
@Override
public void handle(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateConsole(msg);
}
});
}
};
}
/**
* 如果本地补丁放在了外部存储卡中, 6.0以上需要申请读外部存储卡权限才能够使用. 应用内部存储则不受影响
*/
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("本地外部存储补丁无效,因为没有读取外部存储权限");
}
break;
default:
}
}
/**
* 更新监控台的输出信息
*
* @param content 更新内容
*/
private void updateConsole(String content) {
mStatusStr += content + "\n";
if (mStatusTv != null) {
mStatusTv.setText(mStatusStr);
}
}
//要修复的内容
private void changedImageViewSrc() {
final ImageView imageView = findViewById(R.id.iv_girl);
imageView.setImageResource(R.mipmap.add);
}
}
MyApp
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
}
}
MyApplication
public class MyApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
private final String hotfixId = "你的App ID";
private final String hotfixappKey = "你的App Secret";
private final String hotfixRSASECRET = "你的RSA密钥";
@Override
public void onCreate() {
super.onCreate();
//联网下载新的插件
SophixManager.getInstance().queryAndLoadNewPatch();
}
public interface MsgDisplayListener {
void handle(String msg);
}
public static MsgDisplayListener msgDisplayListener = null;
public static StringBuilder cacheMsg = new StringBuilder();
// 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
@Keep
@SophixEntry(MyApp.class)
static class RealApplicationStub {
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此处调用。
// MultiDex.install(this);
initSophix();
}
private void initSophix() {
String appVersion = "0.0.0";
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
} catch (Exception e) {
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
.setAppVersion(appVersion)
.setSecretMetaData(hotfixId, hotfixappKey, hotfixRSASECRET)
.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.");
}
String msg = new StringBuilder("").append("Mode:").append(mode)
.append(" Code:").append(code)
.append(" Info:").append(info)
.append(" HandlePatchVersion:").append(handlePatchVersion).toString();
if (msgDisplayListener != null) {
msgDisplayListener.handle(msg);
} else {
cacheMsg.append("\n").append(msg);
}
}
}).initialize();
}
}
布局 activity_main
项目代码大功告成啦
最后下载补丁和调试工具
patch补丁包生成需要使用到打补丁工具SophixPatchTool, 如还未下载打包工具,请前往下载Android打包工具。
Mac版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_macos.zip
Windows版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_windows.zip
Linux版本打包工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_linux.zip
调试工具地址:http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/hotfix_debug_tool-release.apk