????????最近公司項目漸趨成熟,已經(jīng)不需要經(jīng)常更新版本,并且更新版本對客戶的影響特別大,但是日常維護難免需要更新代碼,因此熱修復(fù)的技術(shù),就比較迫切了。
? ? ? ? 經(jīng)過一段時間的對比,我們最終決定使用阿里的Sophix方案,因為它實在是太強大,而且接入及其簡單。
? ? ? ? 阿里網(wǎng)址如下:阿里云登錄 - 歡迎登錄阿里云,安全穩(wěn)定的云計算服務(wù)平臺
? ? ? ? ?應(yīng)用申請這些因為過于簡單,我這里就不說了,直接上代碼
1、引入官方庫,在app的build.gradle下,直接插入
compile 'com.aliyun.ams:alicloud-android-hotfix:3.3.5'
2、心間自定義Application文件名為SophixStubApplication,繼承自SophixApplication,代碼如下:
public class SophixStubApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
// 此處SophixEntry應(yīng)指定真正的Application,并且保證RealApplicationStub類名不被混淆。
@Keep
@SophixEntry(自定義的Application.class)
static class RealApplicationStub {}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此處調(diào)用。
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)
.setUsingEnhance() // 適配加固模式,如果app使用了加固則需要加上此方法
.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保存狀態(tài)。
Log.i(TAG, "sophix preload patch success. restart app to make effect.");
}
}
}).initialize();
instance.queryAndLoadNewPatch();
}
}
簡單說一下這里,基本是固定格式,最重要的代碼是
instance.queryAndLoadNewPatch();
顧名思義,查詢并加載新的補丁。這個一定要調(diào)用的,不然無法加載補丁,記得把SophixStubApplication注冊到AndroidManifest.xml中,舉個例子:
<application
android:name="xxxxxx.SophixStubApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:maxAspectRatio="2.4"
android:resizeableActivity="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<meta-data
android:name="com.taobao.android.hotfix.IDSECRET"
android:value="" />
<meta-data
android:name="com.taobao.android.hotfix.APPSECRET"
android:value="" />
<meta-data
android:name="com.taobao.android.hotfix.RSASECRET"
android:value="" />
</application>
這樣一套下來,阿里云熱修復(fù)就已經(jīng)成功集成到app中。
接再來再說說一些注意事項
1、不能熱更新的場景
????????1)、新增四大組件
? ? ? ? 2)、修改了SophixStubApplication的內(nèi)容
? ? ? ? 3)、修改了AndroidManifest.xml的內(nèi)容
? ? ? ? 4)、其他配置文件
? ? ? ? 除此之外,其他的一切皆可更新,包括.so文件文章來源:http://www.zghlxwxcb.cn/news/detail-718543.html
2、關(guān)于加固的app,如果采用了加固,去打補丁的時候一定要拿未加固前的apk去操作,否則無法加固文章來源地址http://www.zghlxwxcb.cn/news/detail-718543.html
到了這里,關(guān)于一文接入Android阿里Sophix熱更新的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!