Step by step to teach you how to realize Alibaba's Sophix hot fix (I) configure Sophix information

Keywords: Android Gradle Maven SDK

1.0 integration preparation

The gradle remote warehouse depends on. Open the project and find the build.gradle file of the app. Add the following configuration:

Add maven warehouse address:

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

Add gradle coordinate version depending on configuration:

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

1.1 Sophix permission addition

<! -- Network permission -- >
<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" />
<! -- external storage read permission, debugging tools need to load local patches -- >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

1.2 configure Android manifest file

Add the following configuration under the application node in the middle of Android manifest.xml:

<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 secret key" />

Change the value in the above value to the App Secret and RSA key obtained from the HotFix service application of the platform for writing

1.3 confusion configuration

#Baseline package usage, generating mapping.txt
-printmapping mapping.txt
#The generated mapping.txt is moved to the / APP path under the app/build/outputs/mapping/release path
#Use the repaired project to ensure consistent confusion results
#-applymapping mapping.txt
#hotfix
-keep class com.taobao.sophix.**{*;}
-keep class com.ta.utdid2.device.**{*;}
#Prevent inline
-dontoptimize

1.4 SDK access

The call of initialize should be made as early as possible, and SDK initialization must be carried out at the beginning of Application.attachBaseContext() (after super.attachBaseContext, if there is Multidex, it also needs to be after Multidex.install). Other custom classes cannot be used before initialization, otherwise it is likely to cause crash. The operation of querying whether a patch is available on the server can be anywhere else. It is not recommended to initialize in Application.onCreate(), because with ContentProvider, the timing of Sophix initialization will be too late, causing problems.

package com.main;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * Entrance September 15, 2018
 */

public class HbApplication extends Application {

    private String TAG = "TAG";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        initSophix();
    }

    private void initSophix() {
        String appVersion;
        try {
            appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            appVersion = "1.7.9";
        }

        SophixManager.getInstance().setContext(this)
                .setAppVersion(appVersion)
                .setAesKey(null)
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // Patch loading callback notification
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            // Indicates that the patch is loaded successfully
                            Log.e(TAG, "Patch loaded successfully");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // Indicates that the new patch needs to be restarted when it takes effect. The developer can prompt the user or force the restart;
                            // Suggestion: users can monitor background events and then apply suicide to speed up the application of patches
                            // It is recommended to call killprocesssafety. See 1.3.2.3 for details
                            SophixManager.getInstance().killProcessSafely();
                            Log.e(TAG, "Indicates that the new patch needs to be restarted. Developers can prompt users or force restart");
                        } else if (code == PatchStatus.CODE_LOAD_FAIL) {
                            // The internal engine is abnormal. It is recommended to clear the local patch at this time to prevent repeated loading of failed patches
                            SophixManager.getInstance().cleanPatches();
                            Log.e(TAG, " Internal engine exception, It is recommended to clear the local patch at this time, Prevent repeated loading of failed patches");
                        } else {
                            // For other error information, see the description of the PatchStatus class
                            Log.e(TAG, " Other error messages," + code);
                        }
                    }
                }).initialize();
        SophixManager.getInstance().queryAndLoadNewPatch();
    }
}

To do this means that the configuration is complete. Next is to install the patch package and upload the patch package. Are you looking forward to it?

Posted by rohanm02 on Tue, 07 Jan 2020 04:55:49 -0800