1.什么是Tinker?
Tinker是微信官方的Android热补丁解决方案,它支持动态下发代码、So库以及资源,让应用能够在不需要重新安装的情况下实现更新。当然,你也可以使用Tinker来更新你的插件。
2.快速集成Tinker
1.添加Gradle依赖
1.在项目的build.gradle中,添加tinker-patch-gradle-plugin
的依赖
buildscript {
dependencies {
classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.5')
}
}
2.然后在app的gradle文件app/build.gradle,我们需要添加tinker的库依赖以及apply tinker的gradle插件.
dependencies {
//可选,用于生成application类
provided('com.tencent.tinker:tinker-android-anno:1.7.5')
//tinker的核心库
compile('com.tencent.tinker:tinker-android-lib:1.7.5')
}
2.配置Gradle签名打包
1.签名文件的生成
点击Android studio中的: Buider->Generate Signed APk..–>Create New
填完信息后最终生成:tinker.jks
签名文件
2.配置Gridle
signingConfigs {
release {
try {
storeFile file("./keystore/tinker.jks")
storePassword "tinker"
keyAlias "tinker"
keyPassword "tinker"
} catch (ex) {
throw new InvalidUserDataException(ex.toString())
}
}
debug {
storeFile file("./keystore/debug.keystore")
}
}
buildTypes {
release {
minifyEnabled true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled false
signingConfig signingConfigs.debug
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
3.添加混淆
//在proguard-rules.pro文件中添加混淆
-keepattributes SourceFile,LineNumberTable
4.启动分包功能和给BuildConfig.java添加字段(调试时可能会用上)
/**
* 这里配置的数据将会赋值给BuidConfig.java类中
*/
defaultConfig {
applicationId "xmg.com.androidtinker"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
/**
* you can use multiDex and install it in your ApplicationLifeCycle implement
*/
multiDexEnabled true
/**
* not like proguard, multiDexKeepProguard is not a list, so we can't just
* add for you in our task. you can copy tinker keep rules at
* build/intermediates/tinker_intermediates/tinker_multidexkeep.pro
*/
multiDexKeepProguard file("keep_in_main_dex.txt")
/**
* buildConfig can change during patch!
* we can use the newly value when patch
*/
buildConfigField "String", "MESSAGE", "\"I am the base apk\""
// buildConfigField "String", "MESSAGE", "\"I am the patch apk\""
/**
* client version would update with patch
* so we can get the newly git version easily!
* 这里是给BuildConfig.java 添加字段TINKER_ID与PLATFORM,字段的类型都是String,并给字段赋值:"${}" " "
*/
buildConfigField "String", "TINKER_ID", "\"${getTinkerIdValue()}\""
buildConfigField "String", "PLATFORM", "\"all\""
}
5.添加:keep_in_main_dex.txt
文件到app的根目录下
# you can copy the tinker keep rule at
# build/intermediates/tinker_intermediates/tinker_multidexkeep.pro
-keep class com.tencent.tinker.loader.** {
*;
}
-keep class tinker.sample.android.app.SampleApplication {
*;
}
-keep public class * implements com.tencent.tinker.loader.app.ApplicationLifeCycle {
*;
}
-keep public class * extends com.tencent.tinker.loader.TinkerLoader {
*;
}
-keep public class * extends com.tencent.tinker.loader.app.TinkerApplication {
*;
}
# here, it is your own keep rules.
# you must be careful that the class name you write won't be proguard
# but the tinker class above is OK, we have already keep for you!
3.添加集成Tinker的代码
1.添加SampleApplicationLike和Application
推荐使用tinker-android-anno
来自动生成 Application
,然而你仅仅需要给你的SampleApplicationLike 添加注解 。
@DefaultLifeCycle(
application = "xmg.com.androidtinker.SampleApplication", //自动生成Application的全路径
flags = S