長路漫漫立志行高遠,寒夜漆漆心勇敢獨行。 --致青春
關於移動熱修復,對於現今的開發人員也已經不再陌生了,甚至修復原理也都有所耳聞,總之就是,大家都很牛,但我很菜啊,[捂臉]。
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();
}