Online bugs are a constant headache for programmers. Sometimes it takes just a few lines of code to cause your users serious damage. Google has added Insttan Run to Android Studio. The Apk dynamic loading technique enables the application of non-installation code layer changes. After the qzone team patch began a wave of hot updates. Users do not need to manually update the version of the App, which greatly ensures the user’s adhesion to the product. It is certainly best for developers to use this technology. The hot update and hot repair on the market have different names and the same functions, but the range of support is different. Currently available open platforms include Tencent’s Tinker and HotFix. But Tencent needs to charge, here is how to integrate Ali’s HotFix. And some tread pit treatment.

First of all, you need a Taobao account to log into Alibaichuan and register the corresponding account according to the role. Then apply for an App to get its three parameters: App ID, App Secret,RSA. This is my personal test account, parameters for you to see. As follows:

Download RSA to your computer.

Access application:

Use Android Studio. Add the following to your Gradle file:

allprojects {​        maven {            url "http://repo.baichuan-android.taobao.com/content/groups/BaichuanRepositories"        }    }}
Copy the code

Add the following to moudle gradle:

The compile 'com. Taobao. Android: alisdk - hotfix: 2.0.9'Copy the code

Add permissions to the manifest file:

<! - INTERNET access - > < USES - permission android: name = "android. Permission. INTERNET" / > < USES - the 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

Add parameters of Baichuan SDK to Application:

<meta-dataandroid:name="com.taobao.android.hotfix.IDSECRET"android:value="App ID" /><meta-dataandroid:name="com.taobao.android.hotfix.APPSECRET"android:value="App Secret" / > < meta - dataandroid: name = "com. Taobao. Android. The hotfix. RSASECRET" android: value = "RSA key" / >Copy the code

Confused:

# using baseline package, to generate the mapping. TXT - printmapping mapping. Txt# generated mapping. TXT in the app/buidl/outputs/mapping/release directory path, move to the/app directory path # project use after repair, #hotfix-keep class com.taobao. Sophix.**{*; }-keep class com.ta.utdid2.device.**{*; }Copy the code

Then we initialize the code in the Application respectively, and request the baichuan patch download in onCreate() first:

public void onCreate() { super.onCreate(); initHotFix(); } private void initHotFix() { SophixManager.getInstance().queryAndLoadNewPatch(); }Copy the code

In attatchBaseContent, the patch version should be the same as that of the patch pack you generated in the background:

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 loading callback notification // if (code == Patchstatus.code_load_success) {// // Patch loading success // Toast.maketext (myApplication. this, "patch loaded successfully ", toast.length_short).show(); //} 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: // 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 failure patch repeated loading / / / / SophixManager getInstance (). CleanPatches (); //} else {// // For other error messages, see PatchStatus class description //}}}).initialize();Copy the code

For old and new patches, you need to download the patch generation tool from the official website and then upload the patch package. There are also patch testing tools. The patch uploaded after the release of hungry can be hot update. To test this, I have hidden three banners in the new version of my interface. And patch it. See how it looks:

You can see that the patch has been successfully applied.