Hot update has become one of the essential functions of our major apps. When we found a small display problem as soon as we launched and had to change it; When we need to modify a UI and the rest of the business is not moving; When we need to add a method, change a logic, but the change is not big, often we do not want to package the release. Because constantly downloading updates and constantly having users install apps, especially ToC apps, can increase user disgust. This is when hot updates become necessary. Here we are going to introduce the Sophix solution. The Sophix solution is Ali’s online solution, which we can use as long as we have access to the Internet. Of course, if the number of users in this scheme is large, relevant fees need to be paid. You can check the specific fees on the official website. We only provide relevant access methods here.

1. First we need to join Ali’s Maven warehouse

In your project build.gradle

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

2. Then in build. Gradle of app

android { ...... DefaultConfig {applicationId "com.xxx.xxx" // Package name...... NDK {// Select the. So library to add for the CPU type. / / hot repair support five abiFilters' arm64 - v8a ', 'armeabi', 'armeabi - v7a', 'x86, x86_64'}... }... } dependencies { ...... The compile 'com. Aliyun. Ams: alicloud - android - hotfix: 3.2.17'... }Copy the code

3. Add related 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"/>
Copy the code

4. Under the Application node in androidmanifest.xml, add the following content

<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 The android: name = "com. Taobao. Android. The hotfix. RSASECRET" android: value = "RSA key" / >Copy the code

The three parameters must be obtained from aliyun official website after configuration.

5. Configuration Application

public class SophixStubApplication extends SophixApplication { private final String TAG = "SophixStubApplication"; // SophixEntry should specify the real Application and ensure that the RealApplicationStub class name is not confused. @Keep @SophixEntry(MyRealApplication.class) static class RealApplicationStub {} @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); // If you want to use MultiDex, you need to call it here. // MultiDex.install(this); initSophix(); } private void initSophix() {String appVersion = "1.0.0"; 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) {// If you need to restart in the background, it is recommended to use SharePreference to save the status. Log.i(TAG, "sophix preload patch success. restart app to make effect."); } } }).initialize(); }}Copy the code

MyRealApplication is our original Application

6. Add the obfuscation file as follows

# using baseline package, to generate the mapping. TXT - printmapping mapping. TXT # generate mapping. TXT in the app/build/outputs/mapping/release directory, #hotfix -keep class com.taobao. Sophix.**{*; } -keep class com.ta.utdid2.device.**{*; } - dontwarn com. Alibaba. SDK. Android. Utils. * * # to prevent the inline - dontoptimize - keepclassmembers class com.hao.hotfix.MyRealApplication { public <init>(); } -keep class com.hao.hotfix.SophixStubApplication$RealApplicationStubCopy the code

7. Configure Application in the manifest file

android:name=".SophixStubApplication"
Copy the code

8. Query and load patches

SophixManager.getInstance().queryAndLoadNewPatch();
Copy the code

So that’s what’s in our APP. Let’s explain the generation of the patch pack.

9. Generate patches

Help.aliyun.com/document_de…

This address is the download address of the patch package generation tool. You can download the patch package generation tool of the corresponding system on your PC.

Note that the package here needs to be officially signed. The content to set the signature in the patch builder.

10. Add an application.

  • The first step is to log in to the official website of Ali Cloud
  • Log in with the account provided by the company. It is taobao account can.
  • Click on the console in the upper right corner
  • Search for mobile hotfixes from the search box and click mobile Hotfixes from the console entry in the drop-down box
  • If there is no workspace, create one. Or choose an existing workspace as required by the company.
  • Enter the workspace and add the application. The package name of the application is required.
  • Add the application and add the appropriate code and files as required. Notice that we don’t want to repeat what we did before.
  • Once the app is added, click the mobile Hotfix TAB on the left
  • Select the application we just added from the drop-down box above.
  • Click The Application information to obtain the app key and other data of our application, and fill it into our code. Refer to Step 4.
  • Then click Patch Management on the left, click Add Application, and enter the VersionName of the current version we want to hotfix
  • Then select the corresponding version to upload the patch
  • Then we open the app and a moment later, we kill the app and re-enter, and the app has been updated.
  • Here I would like to remind you that the version of the patch we add on Ali Cloud must be the same as the version we want to hot update, otherwise it cannot be updated.

If you have any questions, please leave me a message. I will be waiting for you to exchange your learning experience here.

Finally, I will provide my Gradle version configuration

The classpath "com. Android. Tools. Build: gradle: 4.0.0" distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zipCopy the code