1. 程式人生 > >整合騰訊bugly的熱修復功能sdk步驟

整合騰訊bugly的熱修復功能sdk步驟

首先為什麼要整合bugly熱修復。市面上有其他的熱修復框架,為什麼就用bugly?這裡給出2張圖大家就明白了。



引用騰訊bugly官網的一段話:

  • 無需關注Tinker是如何合成補丁的
  • 無需自己搭建補丁管理後臺
  • 無需考慮後臺下發補丁策略的任何事情
  • 無需考慮補丁下載合成的時機,處理後臺下發的策略
  • 我們提供了更加方便整合Tinker的方式
  • 我們提供應用升級一站式解決方案
進入正題:接入流程主要是以下幾個步驟:
  • 打基準包安裝並上報聯網(注:填寫唯一的tinkerId)
  • 對基準包的bug修復(可以是Java程式碼變更,資源的變更)
  • 修改基準包路徑、填寫補丁包tinkerId、mapping檔案路徑、resId檔案路徑
  • 執行tinkerPatchRelease打Release版本補丁包
  • 選擇app/build/outputs/patch目錄下的補丁包並上傳(注:不要選擇tinkerPatch目錄下的補丁包,不然上傳會有問題)
  • 編輯下發補丁規則,點選立即下發
  • 重啟基準包,請求補丁策略(SDK會自動下載補丁併合成)
  • 再次重啟基準包,檢驗補丁應用結果

1:新建基準包工程專案(人為製造有BUG的app版本)

  1. btn.setOnClickListener(new View.OnClickListener() {  
  2.            @Override
  3.            publicvoid
     onClick(View view) {  
  4. /                String str = LoadBugClass.getBugString();  
  5.                String str = BugClass.bug();  
  6.                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();;  
  7.            }  
  8.        });  
  1. publicclass BugClass {  
  2.     publicstatic String bug(){  
  3.         String str = null;  
  4.         int str_length = str.length();  
  5.         return"this is bug class";  
  6.     }  
  7. }  
這個可以看出點選一個按鈕會報空指標異常。

2:接著就是配置相關屬性和新增一個外掛依賴了。

下面也給出我自己配置的過程。

首先在最外層的build.gradle檔案中新增依賴,看下圖:


其次新建sampleapplication和sampleapplicationLike兩個Java

  1. package com.henry.testappbugly;  
  2. import android.annotation.TargetApi;  
  3. import android.app.Application;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.res.AssetManager;  
  7. import android.content.res.Resources;  
  8. import android.os.Build;  
  9. import android.support.multidex.MultiDex;  
  10. import com.tencent.bugly.Bugly;  
  11. import com.tencent.bugly.beta.Beta;  
  12. import com.tencent.tinker.loader.app.DefaultApplicationLike;  
  13. /** 
  14.  * Created by W61 on 2016/11/29. 
  15.  */
  16. publicclass SampleApplicationLike extends DefaultApplicationLike {  
  17.     publicstaticfinal String TAG = "Tinker.SampleApplicationLike";  
  18.     public SampleApplicationLike(Application application, int tinkerFlags,  
  19.                                  boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,  
  20.                                  long applicationStartMillisTime, Intent tinkerResultIntent, Resources[] resources,  
  21.                                  ClassLoader[] classLoader, AssetManager[] assetManager) {  
  22.         super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime,  
  23.                 applicationStartMillisTime, tinkerResultIntent, resources, classLoader,  
  24.                 assetManager);  
  25.     }  
  26.     @Override
  27.     publicvoid onCreate() {  
  28.         super.onCreate();  
  29.         // 這裡實現SDK初始化,appId替換成你的在Bugly平臺申請的appId
  30.         Bugly.init(getApplication(), ""true);  
  31.     }  
  32.     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)  
  33.     @Override
  34.     publicvoid onBaseContextAttached(Context base) {  
  35.         super.onBaseContextAttached(base);  
  36.         // you must install multiDex whatever tinker is installed!
  37.         MultiDex.install(base);  
  38.         // 安裝tinker
  39.         // TinkerManager.installTinker(this); 替換成下面Bugly提供的方法
  40.         Beta.installTinker(this);  
  41.     }  
  42.     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)  
  43.     publicvoid registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {  
  44.         getApplication().registerActivityLifecycleCallbacks(callbacks);  
  45.     }  
  46. }  

  1. package com.henry.testappbugly;  
  2. import com.tencent.tinker.loader.app.TinkerApplication;  
  3. import com.tencent.tinker.loader.shareutil.ShareConstants;  
  4. /** 
  5.  * Created by W61 on 2016/11/29. 
  6.  */
  7. publicclass SampleApplication extends TinkerApplication {  
  8.     public SampleApplication() {  
  9.         super(ShareConstants.TINKER_ENABLE_ALL, "SampleApplicationLike所在的包名路徑",  
  10.                 "com.tencent.tinker.loader.TinkerLoader"false);  
  11.     }  
  12. }  

在在Androidmanifest.xml檔案中配置許可權及application類名
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.henry.testappbugly">  
  4.     <application  
  5.         android:name=".SampleApplication"
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:supportsRtl="true"
  10.         android:theme="@style/AppTheme">  
  11.         <activity android:name=".MainActivity">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.                 <category android:name="android.intent.category.LAUNCHER" />  
  15.             </intent-filter>  
  16.         </activity>  
  17.         <!--API 24以上配置-->  
  18.         <provider  
  19.             android:name="android.support.v4.content.FileProvider"
  20.             android:authorities="com.tencent.bugly.hotfix.fileProvider"
  21.             android:exported="false"
  22.             android:grantUriPermissions="true">  
  23.             <meta-data  
  24.                 android:name="android.support.FILE_PROVIDER_PATHS"
  25.                 android:resource="@xml/provider_paths"/>  
  26.         </provider>  
  27.     &l