An overview,

With the rapid development and innovation of technology, thermal repair has mushroomed, such as Meituan’s Robust, Ele. me’s Amigo, wechat’s Tinker, and Ali’s Andfix and Sophix. Standing on the shoulders of giants can broaden our horizons. Now follow me to integrate Ali’s Sophix! Dribble, dribble, let’s go…

Ii. Comparison of thermal repair schemes

It can be seen that alibaba’s hotfix product not only supports a comprehensive range of replacement types, but also has access to Grey Easy. Are you excited? Tinker, which has previously integrated wechat, is indeed a bit more complicated than Sophix!

Three, preparation

Create an Ali account, select Mobile Hotfix, create a product, create an application, so that a set of skills can be MSCP, and finally it looks like this:

Remind me that integration of this product requires real name authentication, so I use alipay login!!

Iv. Integration steps

Now it’s time to magnify the carry mode…

Add maven repository to project gradle:

buildscript { repositories { jcenter() maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" } Google dependencies ()} {the classpath 'com. Android. View the build: gradle: 3.0.1' / / NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" } google() } }Copy the code

2. Add ali remote dependency to App Gradle

Implementation 'com. Aliyun. Ams: alicloud - android - hotfix: 3.1.8'Copy the code

3. Manifest configuration information:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/> <! - hot fix meta - data - > < meta - data android: name = "com. Taobao. Android. The hotfix. IDSECRET" android: value = "IDSECRET" / > < meta - data android:name="com.taobao.android.hotfix.APPSECRET" android:value="APPSECRET"/> <meta-data android:name="com.taobao.android.hotfix.RSASECRET" android:value="RSASECRET"/> <! -- Hotfix meta-data-->Copy the code

Create a new class to inherit SophixStubApplication and specify SophixStubApplication in the manifest:

/** * Created by lin on 2017/12/19. */ public class SophixStubApplication extends SophixApplication { private final String TAG = "SophixStubApplication"; @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(); } @Override public void onCreate() { super.onCreate(); // queryAndLoadNewPatch cannot be placed in attachBaseContext, otherwise it has no network permissions. For example, the /** patch in onCreate will not be actively pushed down to the client after being released in the background. Client by calling a queryAndLoadNewPatch method query background patch is available. * / SophixManager getInstance () queryAndLoadNewPatch (); } private void initSophix() {String appVersion = "1.0"; try { appVersion = this.getPackageManager() .getPackageInfo(this.getPackageName(), 0) .versionName; } catch (Exception e) { } final SophixManager instance = SophixManager.getInstance(); Log.e("lin", "appVersion= " + appVersion); 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.e(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.e(TAG, "sophix preload patch success. restart app to make effect."); }else{ Log.e("lin", "code= " + code); Log.e("lin", "mode= " + mode); Log.e("lin", "info= " + info); Log.e("lin", "handlePatchVersion= " + handlePatchVersion); } } }).initialize(); } // SophixEntry should specify the real Application and ensure that the RealApplicationStub class name is not confused. @Keep @SophixEntry(MyRealApplication.class) static class RealApplicationStub { } }Copy the code

Create a MyRealApplication class that inherits Application:

/** * Created by lin on 2017/12/19. */ public class MyRealApplication extends Application { @Override public void onCreate() { super.onCreate(); Log.e("lin", "MyRealApplication start" ); }}Copy the code

Take a look at the PatchStatus class:

public class PatchStatus { public static final int CODE_LOAD_SUCCESS = 1; public static final int CODE_ERR_INBLACKLIST = 4; public static final int CODE_REQ_NOUPDATE = 6; public static final int CODE_REQ_NOTNEWEST = 7; public static final int CODE_DOWNLOAD_SUCCESS = 9; Public static final int CODE_DOWNLOAD_BROKEN = 10; public static final int CODE_UNZIP_FAIL = 11; public static final int CODE_LOAD_RELAUNCH = 12; Public static final int CODE_REQ_APPIDERR = 15; public static final int CODE_REQ_SIGNERR = 16; public static final int CODE_REQ_UNAVAIABLE = 17; public static final int CODE_REQ_SYSTEMERR = 22; public static final int CODE_REQ_CLEARPATCH = 18; Public static final int CODE_PATCH_INVAILD = 20; . }Copy the code

4, Add confusion:

Use # # sophix baseline package, generate mapping. TXT - printmapping mapping. TXT # generate mapping. TXT in the app/buidl/outputs/mapping/release directory, #hotfix -keep class com.taobao. Sophix.**{*; } -keep class com.ta.utdid2.device.**{*; } -keepclassmembers class com.lin.opensourcelist.SophixStubApplication{ public <init>(); } # if you don't use android. Support. The annotation. Keep need to add trip # - Keep class. Com. My PKG. $RealApplicationStub SophixStubApplicationCopy the code

Ok, the basic functionality has been initialized by following these steps, now let’s verify that it works…

Five, validation,

Add the version number (as needed in Gradle) :

2. Generate patch package:

Download the packaging tool

Patch generation requires SophixPatchTool. If you have not downloaded the package, please download the Android package.

Mac version packaging tools address: ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatch…

Windows packaging tools address: ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatch…

Linux version packaging tools address: ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatch…

A debugging tool address: ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/hotfix_debu…



Select the faulty installation package from the fixed installation package without setting anything else, and then click GO! The difference JAR package is generated:

3. Release patch packs:

Click upload patch:

Click here for details:

You can now verify that the patch package has been successfully sent using the test tool. First connect your application:

Then scan the QR code:

If the preceding information is displayed, the difference package is loaded successfully, and the message is displayed indicating that the application needs to be restarted for the difference package to take effect.

The interface after the restart:

Here, full release and grayscale release can be used. I directly select full release and check whether it is successful in the simulator:

The status has become published. Look at the printed log, you can see that the difference package has been downloaded, reboot will take effect:

This is the log after the restart, you can see that there is no update, indicating that the latest patch package:

Go to the console and see if any data is pushed:



Sure enough, there was push data (a simulator test)…

Six, summarized

Finally, the number of hot repair product push is also limited, the specific number of points to view here, recommend ali hot repair books, learn the technical principle behind it, Didi, the train has arrived, please get off one after another……