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 ""

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:

android:value="App ID" />
android:value="App Secret" />
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
-keep class**{*;}
-keep class com.ta.utdid2.device.**{*;}
#Prevent inline

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.content.Context;
import android.util.Log;


 * Entrance September 15, 2018

public class HbApplication extends Application {

    private String TAG = "TAG";

    protected void attachBaseContext(Context base) {

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

                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    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 for details
                            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
                            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);

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