說明
- 要是打算接入熱修復(fù)或者要學(xué)習(xí)熱修復(fù)的同學(xué),要是打算接入Tinker的,我勸你直接放棄,接入Bugly,都是馬化騰家的,Bugly是依據(jù)Tinker的,底層都是一摸一樣的,但是Bugly的文檔要比Tinker的好一點哎,垃圾文檔,所以,接入Bugly就行
步驟
- 官網(wǎng)申請APPID,不用我教
- 工程build.graldle添加插件
classpath "com.tencent.bugly:tinker-support:1.1.5"
- 項目build.gradle添加依賴
// 多dex配置
implementation "com.android.support:multidex:1.0.1"
//compile(name: 'bugly_crashreport_upgrade-1.3.2', ext: 'aar')
// 遠(yuǎn)程倉庫集成方式(推薦)
//compile 'com.tencent.bugly:crashreport_upgrade:1.3.8'
implementation 'com.tencent.bugly:crashreport_upgrade:latest.release'
//1. 指定tinker依賴版本(注:應(yīng)用升級1.3.5版本起,不再內(nèi)置tinker)
//2.為了便于解答問題乘客,這里的tinker版本建議跟隨此處demo設(shè)置乌逐,如果微信更新了tinker版本扶欣,bugly會定期同步更新
implementation 'com.tencent.tinker:tinker-android-lib:1.9.9'
-
創(chuàng)建tinker-suport.gradle文件,和app的build.gradle是同級別,拷貝我的就行
image.png
apply plugin: 'com.tencent.bugly.tinker-support'
def bakPath = file("${buildDir}/bakApk/")
/**
* 此處填寫每次構(gòu)建生成的基準(zhǔn)包目錄
*/
def baseApkDir = "app-0604-16-24-50"
/**
* 對于插件各參數(shù)的詳細(xì)解析請參考痴奏,如果沒有特殊需求下面的參數(shù)都可以不用更改布朦;如果apk需要加固等可以參考具體描述設(shè)置參數(shù)
*/
tinkerSupport {
// 開啟tinker-support插件,默認(rèn)值true
enable = true
// 指定歸檔目錄昏滴,默認(rèn)值當(dāng)前module的子目錄tinker
autoBackupApkDir = "${bakPath}"
//建議設(shè)置true,用戶就不用再自己管理tinkerId的命名,插件會為每一次構(gòu)建的base包自動生成唯一的tinkerId猴鲫,默認(rèn)命名規(guī)則是versionname.versioncode_時間戳
//具體參考https://github.com/BuglyDevTeam/Bugly-Android-Demo/wiki/Tinker-ID%E8%AF%A5%E6%80%8E%E4%B9%88%E8%AE%BE%E7%BD%AE
autoGenerateTinkerId = true
//tinkerId必須保證唯一性对人,如果兩個base包的tinkerid是一樣的,并且都聯(lián)網(wǎng)激活了拂共,那么后續(xù)補(bǔ)丁上傳到后臺的時候會出現(xiàn)匹配錯誤
//tinkerId = "1.0.2-base"
tinkerId = "1.0.2-patch"
// 是否啟用覆蓋tinkerPatch配置功能牺弄,默認(rèn)值false
// 開啟后tinkerPatch配置不生效,即無需添加tinkerPatch
overrideTinkerPatchConfiguration = true
// 編譯補(bǔ)丁包時宜狐,必需指定基線版本的apk势告,默認(rèn)值為空
// 如果為空,則表示不是進(jìn)行補(bǔ)丁包的編譯
// @{link tinkerPatch.oldApk }
baseApk = "${bakPath}/${baseApkDir}/app-release.apk"
// 對應(yīng)tinker插件applyMapping
baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"
// 對應(yīng)tinker插件applyResourceMapping
baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
// buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
// 是否開啟加固模式抚恒,默認(rèn)為false
// isProtectedApp = true
enableProxyApplication = false
supportHotplugComponent = true
// tinkerEnable功能開關(guān)
tinkerEnable = true
}
/**
* 一般來說,我們無需對下面的參數(shù)做任何的修改
* 對于各參數(shù)的詳細(xì)介紹請參考:
* 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-patch"
//applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" // 可選咱台,設(shè)置mapping文件,建議保持舊apk的proguard混淆方式
//applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可選俭驮,設(shè)置R.txt文件回溺,通過舊apk文件保持ResId的分配
}
}
image.png
- 在項目的build.gradle中引入tinker-suport.gradle
依賴插件腳本
apply from: 'tinker-support.gradle'
image.png
- 創(chuàng)建SampleApplicationLike
package com.tencent.bugly.hotfix;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;
import android.widget.Toast;
//import com.meituan.android.walle.WalleChannelReader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.interfaces.BetaPatchListener;
import com.tencent.tinker.entry.DefaultApplicationLike;
import com.tencent.tinker.entry.DefaultApplicationLike;
import java.util.Locale;
/**
* 自定義ApplicationLike類.
*
* 注意:這個類是Application的代理類,以前所有在Application的實現(xiàn)必須要全部拷貝到這里<br/>
*
* @author wenjiewu
* @since 2016/11/7
*/
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();
// 設(shè)置是否開啟熱更新能力混萝,默認(rèn)為true
Beta.enableHotfix = true;
// 設(shè)置是否自動下載補(bǔ)丁遗遵,默認(rèn)為true
Beta.canAutoDownloadPatch = true;
// 設(shè)置是否自動合成補(bǔ)丁,默認(rèn)為true
Beta.canAutoPatch = true;
// 設(shè)置是否提示用戶重啟逸嘀,默認(rèn)為false
Beta.canNotifyUserRestart = true;
// 補(bǔ)丁回調(diào)接口
Beta.betaPatchListener = new BetaPatchListener() {
@Override
public void onPatchReceived(String patchFile) {
Toast.makeText(getApplication(), "補(bǔ)丁下載地址" + patchFile, Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadReceived(long savedLength, long totalLength) {
Toast.makeText(getApplication(),
String.format(Locale.getDefault(), "%s %d%%",
Beta.strNotificationDownloading,
(int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)),
Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadSuccess(String msg) {
Toast.makeText(getApplication(), "補(bǔ)丁下載成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadFailure(String msg) {
Toast.makeText(getApplication(), "補(bǔ)丁下載失敗", Toast.LENGTH_SHORT).show();
}
@Override
public void onApplySuccess(String msg) {
Toast.makeText(getApplication(), "補(bǔ)丁應(yīng)用成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onApplyFailure(String msg) {
Toast.makeText(getApplication(), "補(bǔ)丁應(yīng)用失敗", Toast.LENGTH_SHORT).show();
}
@Override
public void onPatchRollback() {
}
};
// 設(shè)置開發(fā)設(shè)備车要,默認(rèn)為false,上傳補(bǔ)丁如果下發(fā)范圍指定為“開發(fā)設(shè)備”崭倘,需要調(diào)用此接口來標(biāo)識開發(fā)設(shè)備
Bugly.setIsDevelopmentDevice(getApplication(), true);
// 多渠道需求塞入
// String channel = WalleChannelReader.getChannel(getApplication());
// Bugly.setAppChannel(getApplication(), channel);
// 這里實現(xiàn)SDK初始化翼岁,appId替換成你的在Bugly平臺申請的appId
Bugly.init(getApplication(), "75dbc2164d", true);
//"75dbc2164d"這個需要替換成你自己申請的Id
}
@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);
// TODO: 安裝tinker
Beta.installTinker(this);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallback(
Application.ActivityLifecycleCallbacks callbacks) {
getApplication().registerActivityLifecycleCallbacks(callbacks);
}
@Override
public void onTerminate() {
super.onTerminate();
Beta.unInit();
}
}
- 讓你自己的Application繼承TinkerApplication
public class SampleApplication extends TinkerApplication {
public SampleApplication() {
super(ShareConstants.TINKER_ENABLE_ALL, "com.tencent.bugly.hotfix.SampleApplicationLike",
"com.tencent.tinker.loader.TinkerLoader", false);
}
}
image.png
標(biāo)的這些需要改成你自己的東西
重點來了
- 我們先配置好以后,打版我們的基版本(說白了就是我們線上運(yùn)行的bug版本或者說就是修復(fù)之前的那個版本)
image.png
image.png
-
這個最好和versionName保持同步,然后將tinkerId變?yōu)閎ase這個
image.png
[圖片上傳中...(image.png-a022e8-1559640390459-0)]
然后點擊這個生成正式版本,正式版本是需要簽名的,這個在這里不敘述
-
生成基類APP
image.png
*然后你修改了APP的bug,需要生成補(bǔ)丁
image.png
1.修改第一步,變成patch.
2.點擊上面的編譯
image.png
生成補(bǔ)丁
-
選擇上傳補(bǔ)丁
image.png
記得選擇這個上傳,一定記得不要選擇上面的那個,不然會報錯
又發(fā)現(xiàn)一個奇怪的事情,上面這套代碼在有的項目有問題,出現(xiàn)的錯誤如下
com.tencent.tinker.build.util.TinkerPatchException: loader classes are found in old secondary dex. Found classes: {Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_01;,Lcom/tencent/tinker/loader/shareutil/ShareResPatchInfo;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V23;,Lcom/tencent/tinker/loader/hotplug/ActivityStubManager;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil$1;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil;,Lcom/tencent/tinker/loader/shareutil/SharePatchFileUtil;,Lcom/tencent/tinker/loader/shareutil/ShareFileLockHelper;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_04;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$StreamConsumer$1;,Lcom/tencent/tinker/loader/TinkerDexLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_06;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$1;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$StreamConsumer;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager;,Lcom/tencent/tinker/loader/AndroidNClassLoader;,Lcom/tencent/tinker/loader/TinkerResourcesKey;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_09;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_07;,Lcom/tencent/tinker/loader/shareutil/ShareIntentUtil;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_00;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V24;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_05;,Lcom/tencent/tinker/loader/TinkerTestAndroidNClassLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_07;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_00_T;,Lcom/tencent/tinker/loader/hotplug/handler/MHMessageHandler;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$SectionHeader;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$ResultCallback;,Lcom/tencent/tinker/loader/TinkerResourceLoader;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V19;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_06;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_08;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$BinderInvocationHandler;,Lcom/tencent/tinker/loader/shareutil/ShareResPatchInfo$LargeModeInfo;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs;,Lcom/tencent/tinker/loader/hotplug/interceptor/TinkerHackInstrumentation;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V19;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_02;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_09;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_00;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_02_T;,Lcom/tencent/tinker/loader/TinkerTestDexLoad;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$ProgramHeader;,Lcom/tencent/tinker/loader/TinkerSoLoader;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor$CallbackWrapper;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_08;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$ElfHeader;,Lcom/tencent/tinker/loader/TinkerResourcePatcher;,Lcom/tencent/tinker/loader/shareutil/ShareTinkerInternals;,Lcom/tencent/tinker/loader/hotplug/ComponentHotplug;,Lcom/tencent/tinker/loader/R;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_07;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$OptimizeWorker;,Lcom/tencent/tinker/loader/hotplug/interceptor/InterceptFailedException;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_01;,Lcom/tencent/tinker/loader/BuildConfig;,Lcom/tencent/tinker/loader/TinkerLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_01;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V4;,Lcom/tencent/tinker/loader/hotplug/interceptor/Interceptor$ITinkerHotplugProxy;,Lcom/tencent/tinker/loader/shareutil/ShareSecurityCheck;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_09;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_00;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$FakeInterfaceHandler;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_08;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_06;,Lcom/wisdom/patient/base/BaseApplication;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V17;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_00;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor;,Lcom/tencent/tinker/loader/shareutil/ShareBsDiffPatchInfo;,Lcom/tencent/tinker/loader/shareutil/ShareReflectUtil;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_02;,Lcom/tencent/tinker/loader/TinkerUncaughtHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_02_T;,Lcom/tencent/tinker/loader/TinkerDexOptimizer;,Lcom/tencent/tinker/loader/shareutil/ShareConstants;,Lcom/tencent/tinker/loader/shareutil/SharePatchInfo;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_09;,Lcom/tencent/tinker/loader/hotplug/handler/PMSInterceptHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_05;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_01;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V14;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_03;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager$AttrTranslator;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil$InstructionSet;,Lcom/tencent/tinker/loader/app/TinkerApplication;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$FakeClientBinderHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_02;,Lcom/tencent/tinker/loader/hotplug/UnsupportedEnvironmentException;,Lcom/tencent/tinker/loader/TinkerRuntimeException;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_04;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor$MessageHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_06;,Lcom/tencent/tinker/loader/TinkerDexLoader$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_02_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_04;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager$1;,Lcom/tencent/tinker/loader/hotplug/EnvConsts;,Lcom/tencent/tinker/loader/hotplug/handler/AMSInterceptHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_05;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_02;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_07;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_05;,Lcom/tencent/tinker/loader/AbstractTinkerLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_02_T;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$1;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V7;,Lcom/tencent/tinker/loader/shareutil/ShareDexDiffPatchInfo;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_08;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_04;,Lcom/tencent/tinker/loader/hotplug/interceptor/Interceptor;}
at com.tencent.tinker.build.util.ExcludedClassModifiedChecker.checkIfExcludedClassWasModifiedInNewDex(ExcludedClassModifiedChecker.java:193)
at com.tencent.tinker.build.decoder.DexDiffDecoder.patch(DexDiffDecoder.java:136)
at com.tencent.tinker.build.decoder.UniqueDexDiffDecoder.patch(UniqueDexDiffDecoder.java:39)
at com.tencent.tinker.build.decoder.ApkDecoder$ApkFilesVisitor.visitFile(ApkDecoder.java:176)
... 59 more
com.tencent.tinker.build.util.TinkerPatchException: loader classes are found in old secondary dex. Found classes:
如果說官方給的無法解決這個辦法的話,試試我的這個
image.png
在tinker-support.gradle里面添加這句話就成功了
ignoreWarning = true