接入全流程
配置Gradle
1、項目gradle
2、app moudle
apply plugin: 'com.android.application'
android {
signingConfigs {
release {
keyAlias 'tinker'
keyPassword 'tinker123456'
storeFile file('tinkerDemo.jks')
storePassword 'tinker123456'
}
}
compileSdkVersion 28
defaultConfig {
applicationId "smt.com.tinkerdemo"
minSdkVersion 21
targetSdkVersion 28
versionCode 0
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
// 編譯選項
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
// recommend
dexOptions {
jumboMode = true
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// 多dex配置
implementation 'com.android.support:multidex:1.0.2'
// 本地集成aar方式
// compile(name: 'bugly_crashreport_upgrade-1.3.6', ext: 'aar')
// 遠程依賴集成方式(推薦)
implementation 'com.tencent.bugly:crashreport_upgrade:1.3.6'
// 指定tinker依賴版本(注:應用升級1.3.5版本起,不再內置tinker)
implementation 'com.tencent.tinker:tinker-android-lib:1.9.9'
}
// 依賴插件腳本
apply from: 'tinker-support.gradle'
3、tinker-support.gradle配置
apply plugin: 'com.tencent.bugly.tinker-support'
def bakPath = file("${buildDir}/bakApk/")
/**
* 此處填寫每次構建生成的基準包目錄
*/
def baseApkDir = "app-0716-17-36-08" -----------------------------------------------@1
/**
* 對於插件各參數的詳細解析請參考
*/
tinkerSupport {
// 開啓tinker-support插件,默認值true
enable = true
// 指定歸檔目錄,默認值當前module的子目錄tinker
autoBackupApkDir = "${bakPath}"
// 是否啓用覆蓋tinkerPatch配置功能,默認值false
// 開啓後tinkerPatch配置不生效,即無需添加tinkerPatch
overrideTinkerPatchConfiguration = true ------------------------------------------@2
// 編譯補丁包時,必需指定基線版本的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 = "patch-1.1.0" ---------------------------------------------------------------------@3
// 構建多渠道補丁時使用
// buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
// 是否啓用加固模式,默認爲false.(tinker-spport 1.0.7起支持)
// isProtectedApp = true
// 是否開啓反射Application模式
enableProxyApplication = false -----------------------------------------------------@4
// 是否支持新增非export的Activity(注意:設置爲true才能修改AndroidManifest文件)
supportHotplugComponent = true
}
/**
* 一般來說,我們無需對下面的參數做任何的修改
* 對於各參數的詳細介紹請參考:
* https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
*/
tinkerPatch { --------------------------------------------------- @5
//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的分配
}
}
說明
@1:此處爲基準包的路徑:作用很重要、很重要、很重要,它的作用就是補丁包是以這個路徑下的apk文件爲基準生成的補丁包
@2:如果設置爲false,則@5處的配置不會生效;如果設置爲true,則@5處的配置生效
@3:此處一定要保持唯一性,無論是和當前的基準包,還是之前的版本,一定要保持唯一性,唯一性、唯一性、唯一性,每次生成補丁包,每次、每次、每次,都要保持唯一
@4:如果設置爲true,我們不需要繼承TinkerApplication,只需要繼承原生Application即可,Tinker會採用反射,修改Application;如果設置爲false,我們需要自定義MyApplicationLike繼承DefaultApplicationLike,MyApplication繼承TinkerApplication
MyApplicationLike
public class MyApplicationLike extends DefaultApplicationLike {
public MyApplicationLike(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();
Bugly.setIsDevelopmentDevice(getApplication(),true); ------------------@6
// 這裏實現SDK初始化,appId替換成你的在Bugly平臺申請的appId
Bugly.init(getApplication(), "98552355", true);
}
@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();
}
}
@6:此配置是設置當前設備爲開發設備,對應bugly熱更新更新補丁下發範圍。**開發設備:**補丁下發只會下發給運行了調用該配置爲true的設備;**全量設備:**是補丁分發給所有設備
MyApplication
public class MyApplication extends TinkerApplication {
/**
* current build.
*
*/
public MyApplication() {
super(ShareConstants.TINKER_ENABLE_ALL, "smt.com.tinkerdemo.MyApplicationLike",
"com.tencent.tinker.loader.TinkerLoader", false);
}
}