长路漫漫立志行高远,寒夜漆漆心勇敢独行。 --致青春
关于移动热修复,对于现今的开发人员也已经不再陌生了,甚至修复原理也都有所耳闻,总之就是,大家都很牛,但我很菜啊,[捂脸]。
1、原由
- 之前做过几个热修复的案例,但是这次用的时候发现 阿里 整了个 移动研发平台EMAS,于是就跳坑了,但是我没从坑里跳出来,所以这里记录的是 移动热修复的SDK稳健接入 流程及坑。
2、阿里云配置
- 阿里云文档位置:登录 阿里云 > 右上角 文档 > 移动云 > 移动热修复 > 快速入门 > SDK稳健接入
- 阿里云创建热修复项目(看图):
3、项目配置
- 新建Android项目 (不再详细说了)
- 项目(非module) 的 build文件如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
//增加项
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
//增加项
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
- app的build文件如下
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.liuchaoya.alihotfix"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//新增项
implementation 'com.aliyun.ams:alicloud-android-hotfix:3.2.8'
}
-
新建App类,继承自Application
-
新建SophixStubApplication 如下
import android.content.Context;
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;
/**
* @author Created by LiuChaoya on 2019/1/11 16:20.
* email [email protected]
* Sophix入口类,专门用于初始化Sophix,不应包含任何业务逻辑。
* 此类必须继承自SophixApplication,onCreate方法不需要实现。
* 此类不应与项目中的其他类有任何互相调用的逻辑,必须完全做到隔离。
* AndroidManifest中设置application为此类,而SophixEntry中设为原先Application类。
* 注意原先Application里不需要再重复初始化Sophix,并且需要避免混淆原先Application类。
* 如有其它自定义改造,请咨询官方后妥善处理。
*/
public class SophixStubApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
/**
* 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
*/
@Keep
@SophixEntry(App.class)
static class RealApplicationStub {
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此处调用。
// MultiDex.install(this);
initSophix();
}
private void initSophix() {
//要与阿里云中创建的应用版本号一致
String appVersion = "1.0.0";
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
} catch (Exception e) {
e.printStackTrace();
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
.setAppVersion(appVersion)
.setSecretMetaData(null, null, null)
.setEnableDebug(true)
.setEnableFullLog()
.setPatchLoadStatusStub((mode, code, info, 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.");
}
}).initialize();
}
}
- AndroidManifest.xml 文件中引用 SophixStubApplication ,打开下载的aliyun-emas-services.json文件,做如下配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.liuchaoya.alihotfix">
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 外部存储读权限,调试工具加载本地补丁需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:name=".SophixStubApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="com.taobao.android.hotfix.IDSECRET"
android:value="aliyun-emas-services.json文件中的hotfix.idSecret字段对应的值" />
<meta-data
android:name="com.taobao.android.hotfix.APPSECRET"
android:value="aliyun-emas-services.json文件中的hotfix.appSecret字段对应的值" />
<meta-data
android:name="com.taobao.android.hotfix.RSASECRET"
android:value="aliyun-emas-services.json文件中的hotfix.rsaSecret字段对应的值" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
- MainActivity 中调用查询补丁
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.taobao.sophix.SophixManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查询并加载补丁包
SophixManager.getInstance().queryAndLoadNewPatch();
}
}
-
当前版本打个包,我这里命名为 alihotfix-1.apk
-
修改代码
我们在MainActivity 中添加一句:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.taobao.sophix.SophixManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查询并加载补丁包
SophixManager.getInstance().queryAndLoadNewPatch();
//新增加的代码
Toast.makeText(this,"这是热更新过的包",Toast.LENGTH_LONG).show();
}
}
并且把activity_main.xml也改了(显示的文字改了,并且还添加了一张图片)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World!\n\n这是热更新过的包"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.404"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginBottom="76dp"
android:src="@mipmap/isaac_lau"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"/>
</android.support.constraint.ConstraintLayout>
- 再打一个包,这里我命名为 alihotfix-2.apk
4、补丁
- 补丁生成
选择 旧包 和 新包 的路径后就可以 点击 GO! 生成 补丁包了,补丁包为jar格式,名字不要修改。
-
补丁上传
-
补丁发布
作为测试,我这里选择了灰度发布,人数设为5
5、测试
- 将旧包安装到手机上打开,稍作等待
-
查看阿里云热修复后台,可以看到已经有一个通知成功
-
重启app,就可以看到更新过的效果了。
坑
- hotfix.rsaSecret值,copy的时候一定确认从头copy到尾,我就由于每次都双击选中copy,导致解码异常。
- SophixStubApplication文件中
//这里的App名称和RealApplicationStub 最好不一样,我设置了个一样的,总是无法找到真实的Application
@Keep
@SophixEntry(App.class)
static class RealApplicationStub {
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此处调用。
// MultiDex.install(this);
initSophix();
}