Basic operation steps of alisophix hot fix framework

  • The first step is to go to Alibaba cloud to apply for an account registration. The next step is the management background instructions. The links are as follows:
    https://help.aliyun.com/document_detail/51434.html?spm=5176.doc53287.6.552.vZxNDm#Step5 Release patches
  • List contents

    Create an app project project on android studio, and obtain AppId, AppSecret and RSA key from the management background.
    The above three parameters need to be configured under the application node:

        <meta-data
            android:name="com.taobao.android.hotfix.IDSECRET"
            android:value="AppId" />
        <meta-data
            android:name="com.taobao.android.hotfix.APPSECRET"
            android:value="AppSecret" />
        <meta-data
            android:name="com.taobao.android.hotfix.RSASECRET"
            android:value="RSA secret key" />
  • List contents

    . add the corresponding library under the corresponding build.gradle of the project

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

Add under build.gradle corresponding to the module

 compile 'com.aliyun.ams:alicloud-android-hotfix:3.1.7'
  • Apply for corresponding permissions
     <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" />

As for permissions, mobile phones above 6.0 need dynamic access. If you don't have the relevant permission, you can't check the new patch package by launching the app. When I use Huawei's mobile phone and install the app, the log will always output "there is not update". The reason is that I didn't have the permission to read the memory card and didn't apply successfully.

  • Create MyApplication
package xxq.com.andfixdemo;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.alipay.euler.andfix.patch.PatchManager;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/12/14 0014.
 */

public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    /**
     * apatch file
     */
    private static final String APATCH_PATH= "/Out.apatch";

    private PatchManager mPatchManager;

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }
    private void init() {
        SophixManager.getInstance().setContext(this)
                .setAppVersion(getAppVersionName(this))
                .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.i(TAG, "onLoad: success");
                        } 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 listen for background events and then commit suicide
                            Log.i(TAG, "onLoad: Restart is required to take effect");
                            restartApp();
                        } 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();
                        } else {
                            // For other error information, see the description of the PatchStatus class
                            Log.i(TAG, "onLoad: Other error messages"+code);
                        }
                    }
                }).initialize();
//        SophixManager.getInstance().queryAndLoadNewPatch();
    }
    /**
     * Returns the current program version name
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            //versioncode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }
    public void restartApp(){
        RestartAPPTool.restartAPP(this,2000);
    }
}

Add in activity oncreate()

SophixManager.getInstance().queryAndLoadNewPatch();

The overall code is as follows:

package xxq.com.andfixdemo;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.taobao.sophix.SophixManager;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SophixManager.getInstance().queryAndLoadNewPatch();
        tv = (TextView) findViewById(R.id.textview);
        tv.setOnClickListener(this);


    }
    public void onStart(View V){
        tv.setText("I'm primitive app");
//        tv.setText("I'm a hot fix app");
    }
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();
    }
}

Please refer to the official website for the final use of SopHix
The above contents refer to: http://www.jianshu.com/p/42c436f1b207
Thank you, Yang hai

Tags: Android Java Gradle Maven

Posted on Fri, 15 May 2020 08:47:24 -0700 by paudelvikash