一 . 准备工作
1. 在工程根目录中的build.gradle添加插件依赖
classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.8"
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// TinkerPatch 插件
classpath "com.tinkerpatch.sdk:tinkerpatch-gradle-plugin:1.2.8"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
2. 在app/build.gradle添加TinkerPatch SDK 库:
在defaultConfig里添加
multiDexEnabled true
multiDexKeepProguard file("tinkerMultidexKeep.pro")
// 多dex 打包的类库
defaultConfig {
applicationId "com.mingtao.mtedu"
minSdkVersion 19
targetSdkVersion 25
multiDexEnabled true
versionCode 28
versionName "3.1.3"
multiDexEnabled true
multiDexKeepProguard file("tinkerMultidexKeep.pro") //keep specific classes using proguard syntax
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
// ndk {
// abiFilters "arm64-v8a"
// abiFilters "armeabi-v7a"
// }
添加 TinkerPatch SDK 库的 denpendencies 依赖
// 多dex 打包的类库
api 'com.android.support:multidex:1.0.3'
compileOnly("com.tinkerpatch.tinker:tinker-android-anno:1.9.8")
api("com.tinkerpatch.sdk:tinkerpatch-android-sdk:1.2.8")
3. 在app目录下新建tinkerpatch.gradle
// 多dex 打包的类库
apply plugin: 'tinkerpatch-support'
/**
* TODO: 请按自己的需求修改为适应自己工程的参数
*/
def bakPath = file("${buildDir}/bakApk/")
def baseInfo = "app-3.1.3-0627-14-14-33" //正常打包后在app/build/bakApk目录下的名字替换此处
def variantName = "release"
/**
* 对于插件各参数的详细解析请参考
* http://tinkerpatch.com/Docs/SDK
*/
tinkerpatchSupport {
/** 可以在debug的时候关闭 tinkerPatch **/
/** 当disable tinker的时候需要添加multiDexKeepProguard和proguardFiles,
这些配置文件本身由tinkerPatch的插件自动添加,当你disable后需要手动添加
你可以copy本示例中的proguardRules.pro和tinkerMultidexKeep.pro,
需要你手动修改'tinker.sample.android.app'本示例的包名为你自己的包名, com.xxx前缀的包名不用修改
**/
tinkerEnable = true
reflectApplication = true
/**
* 是否开启加固模式,只能在APK将要进行加固时使用,否则会patch失败。
* 如果只在某个渠道使用了加固,可使用多flavors配置
**/
protectedApp = true
/**
* 实验功能
* 补丁是否支持新增 Activity (新增Activity的exported属性必须为false)
**/
supportComponent = true
autoBackupApkPath = "${bakPath}"
appKey = "替换自己的appKey"
/** 注意: 若发布新的全量包, appVersion一定要更新 **/
appVersion = "3.1.3"
def pathPrefix = "${bakPath}/${baseInfo}/${variantName}/"
def name = "${project.name}-${variantName}"
baseApkFile = "${pathPrefix}/${name}.apk"
baseProguardMappingFile = "${pathPrefix}/${name}-mapping.txt"
baseResourceRFile = "${pathPrefix}/${name}-R.txt"
/**
* 若有编译多flavors需求, 可以参照: https://github.com/TinkerPatch/tinkerpatch-flavors-sample
* 注意: 除非你不同的flavor代码是不一样的,不然建议采用zip comment或者文件方式生成渠道信息(相关工具:walle 或者 packer-ng)
**/
}
/**
* 用于用户在代码中判断tinkerPatch是否被使能
*/
android {
defaultConfig {
buildConfigField "boolean", "TINKER_ENABLE", "${tinkerpatchSupport.tinkerEnable}"
}
}
/**
* 一般来说,我们无需对下面的参数做任何的修改
* 对于各参数的详细介绍请参考:
* https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
*/
tinkerPatch {
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
}
}
并在app/build.gradle 最后面添加代码:
apply from: 'tinkerpatch.gradle'
4. 修改项目的application
-public class YourApplication extends Application {
+public class SampleApplicationLike extends DefaultApplicationLike {
@SuppressWarnings("unused")
@DefaultLifeCycle(application = "com.mingtao.mtedu.MTApplication",//把AndroidManifest.xml中application的name修改为这个
flags = ShareConstants.TINKER_ENABLE_ALL,
loadVerifyFlag = false)
public class SampleApplicationLike extends DefaultApplicationLike {
public SampleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
}
/**
* install multiDex before install tinker
* so we don't need to put the tinker lib classes in the main dex
*
* @param base
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
context = getApplication();
//you must install multiDex whatever tinker is installed!
MultiDex.install(base);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
getApplication().registerActivityLifecycleCallbacks(callback);
}
@Override
public void onCreate() {
super.onCreate();
initTinker();
......
//添加自己的application中初始化的代码
}
private void initTinker() {
if (BuildConfig.TINKER_ENABLE) {
//开始检查是否有补丁,这里配置的是每隔访问3小时服务器是否有更新。
TinkerPatch.init(this)
.reflectPatchLibrary()
.setPatchRollbackOnScreenOff(true)
.setPatchRestartOnSrceenOff(true)
.setFetchPatchIntervalByHours(3);
//每隔3个小时去访问后台时候有更新,通过handler实现轮训的效果
TinkerPatch.with().fetchPatchUpdateAndPollWithInterval();
}
}
}
在清单文件AndroidManifest.xml中把application的name改为SampleApplicationLike中定义的application全名
<application
android:name="***.MTApplication"//SampleApplicationLike中定义的名字
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
5. 在MainActivity或其他Activity中添加补丁下载代码
//加载补丁包
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip");
TinkerPatch.with().fetchPatchUpdate(true);
二 . 测试
1.打包
会在app/build/bakApk目录下生成一个文件夹
把app-release.apk安装到手机上
2.生成补丁
修改代码完成后在Android Studio右上角打开Gradle 双击tinkerPatchRelease
会在生成 app\build\outputs\apk\tinkerPatch
把生成的patch_signed_7zip.apk上传到Tinker后台