My simple book:Jane’s book

preface

Hot repair as the current hot technology, in the industry is more famous ali AndFix, Dexposed, Tencent QQ space super patch technology and wechat Tinker. However, I haven’t done much research on thermal repair, because recently I learned that Ali released a new generation of thermal repair technology — Sophix. So I decided to do a wave…

I. Open mobile heat repair service

  • 1. Register an Aliyun account and choose mobile hot repair service

To enable this function, click “Open now” :

  • 2. Open the administrative console and click on the upper right corner to create the APP


When you return to the Hotfix list, both Android and iOS apps will appear.

Ps: It is important to note that one is the mobile service APP list and the other is the Hotfix list. The two lists need to be distinguished.

  • 3. RSA key (later needs to be configured in the AndroidManifest file)

Basic information can be viewed by clicking on the management at the back of Hoxfix android app:


2. Access the hot repair service

1. Add dependency packages

In your project app.gradle:

  • Add maven repository address:
repositories {
   maven {
       url "http://maven.aliyun.com/nexus/content/repositories/releases"}}Copy the code
  • Add gradle coordinate version dependency:

dependencies {
    compile 'com. Aliyun. Ams: alicloud - android - hotfix: 3.1.0'
}
Copy the code

Ps: If the project also relies on other Ari SDK, it may also rely on utDID SDK. To avoid conflicts, it can be as follows:

dependencies {
      compile ('com. Aliyun. Ams: alicloud - android - hotfix: 3.1.0') {
           exclude(module: 'alicloud-android-utdid')}}Copy the code
2. Related configuration in AndroidManifest
  • 1. Configure related permissions:
<! -- 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, debug tool load local patch required -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Copy the code
  • 2. Add metadata:
<application 
    .
    >
    <meta-data
      android:name="com.taobao.android.hotfix.IDSECRET"
      android:value="APP ID of your APP" />
    <meta-data
      android:name="com.taobao.android.hotfix.APPSECRET"
      android:value="APP Secret for your APP" />
    <meta-data
      android:name="com.taobao.android.hotfix.RSASECRET"
      android:value="RSA key of your APP" />
</application>
Copy the code
Initialize Sophix

Initialize the call should be as early as possible, must be in the Application. The attachBaseContext () or Application. The onCreate () started the SDK initialization, or is likely to lead to collapse. The query to see if the server has a patch available can be done anywhere else.

Here I inherit Application and initialize it in the onCreate() method:

public class SophixApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initSophix();
    }

    private void initSophix() {
        String appVersion;

        try {
            appVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            appVersion = "1.0.0";
            e.printStackTrace();
        }

        // Initialize is best placed before attachBaseContext
        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 load callback notification
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            // The patch is loaded successfully
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // Restart is required for the new patch to take effect. Developers can prompt users or force a reboot;
                            Suggestion: The user can listen for the background event and call killProcessSafely to commit suicide to speed up the application of the patch. See 1.3.2.3
                        } else {
                            // For other error messages, see PatchStatus class description
                        }
                    }
                }).initialize();
        // queryAndLoadNewPatch cannot be placed in attachBaseContext, otherwise it has no network permissions. You are advised to place it at any time later, such as onCreateSophixManager.getInstance().queryAndLoadNewPatch(); }}Copy the code

3. Generate a patch and upload it to the console

1. Perform the first run and package to generate APK

For a simple test, take a TextView packaged with the name first.apk


public class MainActivity extends AppCompatActivity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText("This is the first run,"); }}Copy the code

2. Run the second time and package to generate APK

At this point we change the text Settings of our TextView and package it with the name second.apk

3. Generate patches
  • A. To generate patches, you need to download SophixPatchTool, ali’s patching tool, and then choose the generated APK package twice.

Ps: Old package: < Required > Select the baseline package path (problematic APK). New package: < Required > Select a new package path (fixed the problem APK). Log: The log output window is displayed. Advanced: Expand advanced options. Settings: Configure other information. Go! : The patch generation starts.

  • B. Whether cold startup is required to open advanced Settings, this item is the default, no need to set:

Ps: Strong cooling startup: If this parameter is selected, the patch package will be generated in a format that requires cold startup. If you do not select this option by default, the tool automatically selects instant hot replacement or cold start repair based on code changes. Do not Compare resources: Do not compare resource changes during patch installation. Do not compare the SO library: Do not compare the changes of the SO library during patch installation.

  • C. Open the Settings option to set the signature information:

  • D. Click Go to generate patch:



  • E. Upload the patch

Upload the generated patch file sophix-patch.jar to the console:


4. Debug and release patches

After uploading the patch, check the patch details and you will find that the patch status is waiting for release. Ali recommends using hotfixDebug for local test before release. Of course, we can directly release the patch in full.

1. Local testing

Download The HotFixDebug tool. After installing the hotFixDebug tool, fill in the package name, click Connect, and scan the QR code in the patch details to download the patch. Or put the patch package into the mobile phone SDcard and fill in the absolute path is also ok.

Mode:1 indicates the scanning Mode. Code:12 indicates that the application has a patch. Therefore, the new patch will not be loaded immediately and needs to be loaded after the next restart. (because I’ve already downloaded it once)

At this point, open the first. Apk that we have installed and find that there is no change, kill the process, re-enter and find that it is already what we want.

2. Release patches in full

Select full release and the fix will be available to all users who have first.apk installed. Click to confirm release.

After publishing, we can restart the APP and see that the content is what we want. (This process may be slightly delayed)

For more details on the release process, click On Resources

Perfect for thermal repair!

conclusion

Benefits of the Sophix:

  • 1. The patch can take effect immediately, and there is no need to restart the APP (in my test, the APP needs to be restarted because it already exists locally);
  • 2. The PATCH package also adopts differential technology, and the generated PATCH volume is small. Ali uses its own SophixPatchTool to generate patches, which is easy to use.
  • 3, no intrusion to the application, almost no performance loss;
  • 4. Fool access.

Disadvantages:

Start charging, but there is a free threshold, you can check out the price

Click download Demo

The resources

Patch debugging patch installation tool Download Debugging tool APK download Android hot repair technology selection