天天看點

阿裡雲Sophix 3.0熱修複技術接入總結

對比各種熱修複技術和開發難度,我最終選擇了Sophix

阿裡雲Sophix 3.0熱修複技術接入總結
阿裡雲Sophix 3.0熱修複技術接入總結

接入之前先了解一下Sophix文檔:

阿裡熱修複:https://www.aliyun.com/product/hotfix

快速接入直通車:https://help.aliyun.com/document_detail/53240.html?spm=a2c4g.11174283.3.1.D6UW0D

穩鍵接入直通車:https://help.aliyun.com/document_detail/61082.html?spm=a2c4g.11186623.6.547.ArjpX7

我在項目裡使用的是  穩鍵接入  下面介紹 穩鍵接入  的流程

  1.在app中gradle檔案中添加(倉庫)

   repositories {

         maven  {

             url "http://maven.aliyun.com/nexus/content/repositories/releases"

               }

            }

    2.然後在app的build.gradle裡面添加依賴:

       compile'com.aliyun.ams:alicloud-android-hotfix:3.1.3'

     注意,若SDK內建過程中出現UTDID沖突,請參考:https://help.aliyun.com/knowledge_detail/59152.html

   (我接入友盟sdk,造成了UTDID沖突,原因是友盟sdk和SopHix有重複的東西)

  3.穩健接入需要加入一個SophixStubApplication 的類

    這裡有一點需要注意SophixStubApplication 類裡有個 括号裡寫原來自己的Application名字

    import android.content.Context;

import android.support.annotation.Keep;

import android.util.Log;

import com.jiaoyu.utils.ILog;

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;

public class SophixStubApplication extends SophixApplication {

    private final String TAG = "SophixStubApplication";

    // 此處SophixEntry括号裡面應填寫原來的的Application,并且保證RealApplicationStub類名不被混淆。

    @Keep

    @SophixEntry(Application.class)

    static class RealApplicationStub {

    }

    @Override

    protected void attachBaseContext(Context base) {

        super.attachBaseContext(base);

//         如果需要使用MultiDex,需要在此處調用。

//         MultiDex.install(this);

        initSophix();

    }

    private void initSophix() {

//你的背景版本,和控制台版本一樣           

        String appVersion = "2.4.4";

        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儲存狀态。

                            //必須使用原生的SharePreference

                            Log.i(TAG, "sophix preload patch success. restart app to make effect.");

                        }

                    }

                }).initialize();

    }

}

4.之後我們在配置下剛才新寫的SophixStubApplication類

<application

        android:name="com.XX.XXXX.SophixStubApplication"

總結一下:

把此SophixStubApplication入口類添加進項目中,所有Sophix相關初始化放在此類中。并且不應包含開發者的任何業務邏輯代碼。 若使用了MultiDex,也應在SophixStubApplication的initSophix之前添加,并且需要記得在原來的Application裡面去除MultiDex,避免重複調用導緻問題。

把RealApplicationStub的SophixEntry注解的内容改為自己原先真正的MyRealApplication類

AndroidManifest裡面的application改為新增的SophixStubApplication入口類

在AndroidManifest.xml中間的application節點下添加如下配置:

<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密鑰" />

将上述value中的值分别改為通過平台HotFix服務申請得到的App Secret和RSA密鑰

5.混淆配置

  1. #hotfix

  2. -keep class com.taobao.sophix.**{*;}

  3. -keep class com.ta.utdid2.device.**{*;}

至此配置完成