1. 程式人生 > >騰訊Tinker熱更新的詳細整合與使用

騰訊Tinker熱更新的詳細整合與使用

因為專案需要集成了騰訊的Tinker熱更新,因騰訊文件有些晦澀所以我做了點整理記錄在此,若有錯誤可在官方文件查閱

關於目前常用的熱更新第三方如下:


第一步:新增外掛依賴

工程根目錄下“build.gradle”檔案中新增:

buildscript {
repositories{
    jcenter()
}
dependencies{
  // tinkersupport外掛, 其中lastest.release指拉取最新版本,也可以指定明確版本號,例如1.0.4
classpath"com.tencent.bugly:tinker-support:1.0.8"
  }
}

在主app module的“build.gradle”檔案中新增:

// 依賴外掛指令碼
apply from: 'tinker-support.gradle'
tinker-support.gradle內容如下所示(示例配置):

注:您需要在同級目錄下建立tinker-support.gradle這個檔案。

apply plugin: 'com.tencent.bugly.tinker-support'

def bakPath = file("${buildDir}/bakApk/")

/**
* 此處填寫每次構建生成的基準包目錄
*/def baseApkDir = "app-0208-15-10-00"

/**
* 對於外掛各引數的詳細解析請參考
*/
tinkerSupport {

// 開啟tinker-support外掛,預設值true
enable = true

// 指定歸檔目錄,預設值當前module的子目錄tinker
autoBackupApkDir = "${bakPath}"

// 是否啟用覆蓋tinkerPatch配置功能,預設值false
// 開啟後tinkerPatch配置不生效,即無需新增tinkerPatch
overrideTinkerPatchConfiguration = true

// 編譯補丁包時,必需指定基線版本的apk,預設值為空
// 如果為空,則表示不是進行補丁包的編譯
// @{link tinkerPatch.oldApk }
baseApk = "${bakPath}/${baseApkDir}/app-release.apk"

// 對應tinker外掛applyMapping
baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"

// 對應tinker外掛applyResourceMapping
baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"

// 構建基準包和補丁包都要指定不同的tinkerId,並且必須保證唯一性
tinkerId = "base-1.0.1"

// 構建多渠道補丁時使用
// buildAllFlavorsDir = "${bakPath}/${baseApkDir}"

// 是否啟用加固模式,預設為false.(tinker-spport 1.0.7起支援)
// isProtectedApp = true

// 是否開啟反射Application模式
enableProxyApplication = false

}

/**
* 一般來說,我們無需對下面的引數做任何的修改
* 對於各引數的詳細介紹請參考:
* https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
*/
tinkerPatch {
//oldApk ="${bakPath}/${appName}/app-release.apk"
ignoreWarning = false
useSign = true
dex {
dexMode = "jar"
pattern = ["classes*.dex"]
loader = []
}
lib {
pattern = ["lib/*/*.so"]
}

res {
pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
ignoreChange = []
largeModSize = 100
}

packageConfig {
}
sevenZip {
zipArtifact = "com.tencent.mm:SevenZip:1.1.10"// path = "/usr/local/bin/7za"
}
buildConfig {
keepDexApply = false
//tinkerId = "1.0.1-base"
//applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" // 可選,設定mapping檔案,建議保持舊apk的proguard混淆方式
//applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可選,設定R.txt檔案,通過舊apk檔案保持ResId的分配
}
}

注意這個檔案中配置了enableProxyApplication = false
意思就是是否開啟反射Application模式,預設為false
這是Tinker推薦的接入方式,一定程度上會增加接入成本,但具有更好的相容性。

配置示例(路徑app/build.gradle):
android {
        defaultConfig {
          ndk {
            //設定支援的SO庫架構
            abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
          }
        }
      }
      dependencies {
          //註釋掉原有bugly的倉庫
          //compile 'com.tencent.bugly:crashreport:latest.release'//其中latest.release指代最新版本號,也可以指定明確的版本號,例如2.3.2
          compile 'com.tencent.bugly:crashreport_upgrade:latest.release'//其中latest.release指代最新版本號,也可以指定明確的版本號,例如1.2.0
          compile 'com.tencent.bugly:nativecrashreport:latest.release' //其中latest.release指代最新版本號,也可以指定明確的版本號,例如2.2.0
      }
第二步:初始化SDK

①:enableProxyApplication = false 的情況:

自定義Application:
public class SampleApplication extends TinkerApplication {
    public SampleApplication() {
        super(ShareConstants.TINKER_ENABLE_ALL, "xxx.xxx.SampleApplicationLike",
                "com.tencent.tinker.loader.TinkerLoader", false);
    }
}

注意:這個類整合TinkerApplication類,這裡面不做任何操作,所有Application的程式碼都會放到ApplicationLike繼承類當中

四個引數解析
引數1:tinkerFlags 表示Tinker支援的型別 dex only、library only or all suuport,default: TINKER_ENABLE_ALL
引數2:delegateClassName Application代理類 這裡填寫你自定義的ApplicationLike
引數3:loaderClassName Tinker的載入器,使用預設即可
引數4:tinkerLoadVerifyFlag 載入dex或者lib是否驗證md5,預設為false

自定義ApplicationLike(這裡需要重新寫一個ApplicationLike來繼承DefaultApplicationLike):
public class SampleApplicationLike extends DefaultApplicationLike {

public static final String TAG = "Tinker.SampleApplicationLike";

public SampleApplicationLike(Application application, int tinkerFlags,
boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
long applicationStartMillisTime, Intent tinkerResultIntent) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
}


@Override
public void onCreate() {
super.onCreate();
// 這裡實現SDK初始化,appId替換成你的在Bugly平臺申請的appId
// 除錯時,將第三個引數改為true
Bugly.init(getApplication(), "900029763", false);
}


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
// you must install multiDex whatever tinker is installed!
MultiDex.install(base);

// 安裝tinker
// TinkerManager.installTinker(this); 替換成下面Bugly提供的方法
Beta.installTinker(this);
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
getApplication().registerActivityLifecycleCallbacks(callbacks);
}

}
注意:tinker需要你開啟MultiDex,你需要在dependencies中進行配置compile "com.android.support:multidex:1.0.1"才可以使用MultiDex.install方法; SampleApplicationLike這個類是Application的代理類,以前所有在Application的實現必須要全部拷貝到這裡,在onCreate方法呼叫SDK的初始化方法,在onBaseContextAttached中呼叫Beta.installTinker(this);

enableProxyApplication = true 的情況:

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
// 這裡實現SDK初始化,appId替換成你的在Bugly平臺申請的appId
// 除錯時,將第三個引數改為true
Bugly.init(this, "900029763", false);
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// you must install multiDex whatever tinker is installed!
MultiDex.install(base);


// 安裝tinker
Beta.installTinker();
}

}
注:無須你改造Application,主要是為了降低接入成本,我們外掛會動態替換AndroidMinifest檔案中的Application為我們定義好用於反射真實Application的類(需要您接入SDK 1.2.2版本 和 外掛版本 1.0.3以上)。第三步:AndroidManifest.xml配置
在AndroidMainfest.xml中進行以下配置:

1. 許可權配置

 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <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_LOGS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. Activity配置

<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity"
android:configChanges="keyboardHidden|orientation|screenSize|locale"
android:theme="@android:style/Theme.Translucent" />
3. 配置FileProvider

注意:如果您想相容Android N或者以上的裝置,必須要在AndroidManifest.xml檔案中配置FileProvider來訪問共享路徑的檔案。

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/></provider>
如果你使用的第三方庫也配置了同樣的FileProvider, 可以通過繼承FileProvider類來解決合併衝突的問題,示例如下:
<provider
    android:name=".utils.BuglyFileProvider"
    android:authorities="${applicationId}.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities,exported,grantUriPermissions">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"
        tools:replace="android:resource"/>
</provider>
${applicationId}請替換為您的包名,例如com.bugly.upgrade.demo。這裡要注意一下,FileProvider類是在support-v4包中的,檢查你的工程是否引入該類庫。在res目錄新建xml資料夾,建立provider_paths.xml檔案如下:
<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
    <external-path name="beta_external_path" path="Download/"/>
    <!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
    <external-path name="beta_external_files_path" path="Android/data/"/></paths>
第四步:混淆配置為了避免混淆SDK,在Proguard混淆檔案中增加以下配置:
-dontwarn com.tencent.bugly.*
-keep public class com.tencent.bugly.**{*;}
如果你使用了support-v4包,你還需要配置以下混淆規則:
-keep class android.support.**{*;}

好了程式碼部分就差不多結束了,接下來就是使用方法了:






Tinker的整合與使用方法是這樣啦,當然把更新上傳上去後不要著急,會有幾分鐘的響應時間,幾分鐘後關閉手機上的APP並重新開啟,熱更新的內容就弄好啦~

相關推薦

Tinker更新詳細整合使用

因為專案需要集成了騰訊的Tinker熱更新,因騰訊文件有些晦澀所以我做了點整理記錄在此,若有錯誤可在官方文件查閱關於目前常用的熱更新第三方如下:第一步:新增外掛依賴工程根目錄下“build.gradle”檔案中新增:buildscript { repositories{

Bugly更新整合以及問題

ClassLoader 我們知道Java在執行時載入對應的類是通過ClassLoader來實現的,ClassLoader本身是一個抽象來,Android中使用PathClassLoader類作為Android的預設的類載入器,  PathClassLoader其實實現的就是簡單的從檔案

Tinker 修復 Andriod studio 3.0 配置和整合(三)Bugly整合

騰訊Tinker 熱修復 Andriod studio 3.0 Bugly整合和多渠道補丁管理髮布 本文說明 上一篇我說完了騰訊Tinker 熱修復之多渠道打包,這篇我們來初步瞭解下騰訊Tinker和Bugly結合來做熱修復多渠道補丁管理和整合。(其實在上週我已經整合測試完了dem

Tinker 修復 Andriod studio 3.0 配置和整合(二)多渠道打包和補丁釋出

騰訊Tinker 熱修復 Andriod studio 3.0 多渠道打包和釋出補丁方式推薦 本文說明 在之前我已經分享了Tinker 熱修復的 Andriod studio3.0 初次配置和整合,時隔這麼久來寫一下我對Thinker多渠道打包的理解和記錄,希望對大家有幫助。這篇文

Tinker 修復 Andriod studio 3.0 配置和整合(一)

本文說明 面試的時候經常問我有沒有用過熱修復?用誰的?能說下原理嗎?當時我回答得不好,畢竟以前的專案都沒有用,又不敢裝逼,mmp,但是基本流程還是知道的,所以我們來初探下Tinker 這個熱修復,如果我是Andriod studio 2.3的話,我還不怎麼想寫這個文章,畢竟太多了,沒有

Bugly更新整合總結

熱更新:多麼高大上的名字,Android 開發者應該都知道這麼個東西,原理呢!請自行百度,這裡只是整合總結,謝謝!!! 對於第三方SDK的使用,大家都知道用“步步高點讀機,哪裡不會點哪裡”—— 所以第一步肯定是看官方整合文件:地址:https://bugly.

Android studio3.0 命令列方式簡單整合Tinker修復外掛

簡介: 關於熱修復的介紹現在網上有很多,所以在此我就不過多BB,此篇部落格的特點有兩個,首先,這是一個針對Android studio3.0使用者的部落格,其次,這裡採用的是命令列的方式,這是方式在工作中並不經常使用,相反,在工作中基本都是使用gradle配置的方式,但是命

Android Bugly 更新

nor 現在 oar rri filter ble 實施 2.2.0 armeabi 這個是一位大佬教我的,我自己照著做寫博客 熱更新雖然看起來很復雜,但是Bugly通過集成,使得這個過程很簡單。我這裏不涉及多渠道熱更新,只講述最簡單的情況。 1.首先我們需要在Bugly上

Android Bugly更新接入(Kotlin語言)

Android 騰訊Bugly熱更新接入(Kotlin語言) 簡介 一、新增外掛依賴 二、gradle配置 三、新建tinker-support.gradle 四、初始化SDK 五、AndroidManifest.xml配置 六、混淆

[Android]Tinker修復框架簡單使用

前言目前我們所知的熱修復方案有阿里的AndFix、美團的Robust以及QZone的超級補丁方案,還有本篇的Tinker,如何在我們的自開發的軟體上選用合適的方案呢?先看看各家的框架效能對比,在作參考。總體來說:AndFix作為native解決方案,首先面臨的是穩定性與相容性

Bugly-更新破解記錄

package jiami; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; imp

微信Tinker更新詳細使用

先看一下效果圖 Tinker已知問題 由於原理與系統限制,Tinker有以下已知問題: Tinker不支援修改AndroidManifest.xml,Tinker不支援新增四大元件; 由於Google Play的開發者條款限制,不建議在GP渠道動態更

Bugly修復和更新的渠道包和加固問題

菜的坑 首先騰訊的熱修復是真的好用,釋出了補丁包之後真的可以實現使用者無感知更新APP新增內容或者修改bug,但是官方文件寫到最後加固和多渠道問題處理的並不清楚,並且上邊建議的方法很是麻煩,效果並不好,個人感覺是這樣,給點小建議,可以參考,這裡先給出官方

Android開發——整合Tinker更新框架出現java.lang.NoClassDefFoundError

前言 在Android開發當中,Tinker熱更新是很常見的使用框架,但是我今天在整合的時候卻出現異響不到的事情,出現bug了。應用啟動出現Tinker.UncaughtHandler: TinkerUncaughtHandler catch exception

一次整合使用Tinker更新的體驗

熱載入 簡單的說 如果Android要載入一個類 就會呼叫ClassLoader的findClass方法 在dex中查詢這個類 找到後加載到記憶體 而我們要做的就是在findClass的時候讓類載入找到的是我們修復過後的類,而不是未修復的類。

基於微信Tinker更新詳細說明

  先來吐槽一下,這個更新方法簡直6的沒話說,經我詳細的測試,可以更新類及新增類,刪除類什麼的,以及對XML資原始檔的更新,好像還能更新library,但是我還沒測試過,總之可以可以很強勢,但是在整合的過程中也很多坑,集了我一天多,報錯太多了,網上資料還不怎麼詳

雲服務器安裝JDKtomcat(ubuntu)

ack print 過程 文件名 eth installer 不同的 tor ins ##騰訊雲服務器安裝JDK與tomcat 關於文件無法創建或者傳輸的問題: 首先在命令行中進入root模式,輸入 sudo su 然後輸入你的賬戶密碼,進入root模式。 在對於你

《2018移動遊戲技術評審標準實踐案例》開放下載

進入2018年,中國移動遊戲市場增速放緩,競爭愈發激烈;同時現象級手遊層出不窮,手遊精品化趨勢更加明顯,對移動遊戲研發質量也提出了更高的要求。   在此背景下,騰訊遊戲學院彙集騰訊互娛技術團隊多年積累的技術評審標準和專案實踐經驗,系統化整理成冊,正式釋出《2018騰訊移動遊戲技術評審標

讀《2018移動遊戲技術評審標準實踐案例》簡記

最近,騰訊遊戲學院放出了《騰訊移動遊戲技術評審標準與實踐案例》。 正好最近不忙,就找時間通讀一遍。記錄一下有用的知識點。 鑑於書內容較多,所以拆分為多篇部落格。 標題好長,改為:2018騰訊移動遊戲實踐經驗。(實際上,書中評審標準就一張表。。) 目錄: 一、2018騰訊移動遊戲實

CodePush更新切換本地微軟服務

之前測試code-push-server時候將服務端地址寫成本地了 操作程式碼: code-push login http://192.168.1.100:3000        此後很長一段時間沒有使用,並且本地服務程式碼、資料庫