所有的步驟都是通過阿裡雲的接口文檔來寫的,其實很簡單。按照步驟一步一步進行就可以了。
1.我的項目的gradle寫法:
// 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.2.1'
// 添加emas-services插件
classpath 'com.aliyun.ams:emas-services:1.0.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
}
2.app的gradle寫法:
// 在 apply plugin: 'com.android.application' 下添加
apply plugin: 'com.aliyun.ams.emas-services'
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
}
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.7'
}
3.初始化:
package com.ysl.myhotfix;
import android.app.Application;
public class MyApplication extends Application {
//做自己的程式要做的工作
}
package com.ysl.myhotfix;
import android.app.Application;
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;
import com.taobao.sophix.listener.PatchLoadStatusListener;
/**
* 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(MyApplication.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(null, null, null)
.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.");
}
}
}).initialize();
}
@Override
public void onCreate() {
super.onCreate();
// queryAndLoadNewPatch不可放在attachBaseContext 中,否則無網絡權限,建議放在後面任意時刻,如onCreate中
SophixManager.getInstance().queryAndLoadNewPatch();
}
}
4.AndroidManifest.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ysl.myhotfix">
<!-- 網絡權限 -->
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.taobao.android.hotfix.IDSECRET"
android:value="App ID" />
<meta-data
android:name="com.taobao.android.hotfix.APPSECRET"
android:value="App Secret" />
<meta-data
android:name="com.taobao.android.hotfix.RSASECRET"
android:value="RSA密鑰" />
<activity android:name=".SecondActivity"/>
</application>
</manifest>
5.配置好檔案後,就可以修改bug,并生成更新檔檔案,上傳到阿裡雲,做熱修複了。
6.經過驗證:Sophix并不支援修改AndroidManifest.xml檔案,不支援添加四大元件,也不能改版本号。是以Sophix更适合于修複緻命bug。如果是增加或修改功能等操作,還是釋出新版本比較好。
源碼位址:https://github.com/yueshaolong/MyHotFix.git