1.添加依赖
在项目的build.gradle中,添加tinker-patch-gradle-plugin的依赖
buildscript {
dependencies {
classpath ('com.tencent.tinker:tinker-patch-gradle-plugin:1.7.9')
}
}
然后在app的gradle文件app/build.gradle,我们需要添加tinker的库依赖以及apply tinker的gradle插件.
dependencies {
//可选,用于生成application类
provided('com.tencent.tinker:tinker-android-anno:1.7.9')
//tinker的核心库
compile('com.tencent.tinker:tinker-android-lib:1.7.9')
}
...
...
//apply tinker插件
apply plugin: 'com.tencent.tinker.patch'
2.添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.app_bulid.gradle
拷贝sample中的
4.自定义application实现DefaultApplicationLike或者拷贝SampleApplicationLike到自己项目中
同时要在AndroidManifest.xml中修改appName为com.zhongchao.myhotfixtest4.SampleApplication即自己修改过得名字
注意:此时application是没有的,会报红,但会在编译时生成,因此不用担心
5.初始化Tinker
@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);
//installTinker after load multiDex
//or you can put com.tencent.tinker.** to main dex
TinkerInstaller.install(this);
Tinker tinker = Tinker.with(getApplication());
}
此处做了修改,没有用实例中的,最重要的是这句TinkerInstaller.install(this);
6.加载补丁
注意:加载补丁要等生成补丁,并且推送到手机上以后,才能加载
loadPatchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
}
});
7.签名打包apk
8.改动并配置路径
注意:原apk路径
def bakPath = file("${buildDir}/bakApk/")
tinkerOldApkPath = "${bakPath}/app-release-0504-11-40-20.apk"
//proguard mapping file to build patch apk
tinkerApplyMappingPath = "${bakPath}/app-release-0504-11-40-20-mapping.txt"
//resource R.txt to build patch apk, must input if there is resource changed
tinkerApplyResourcePath = "${bakPath}/app-release-0504-11-40-20-R.txt"
这些都要更改
9.生成补丁
studio右侧,点击gradle–>tinker–>tinkerPath…debug或release
生成之后推送即可
问题:tinkerId
自己修改一下即可
def getTinkerIdValue() {
return hasProperty("TINKER_ID") ? TINKER_ID : "tinker_id_2333"
}
tinker已知问题
由于原理与系统限制,Tinker有以下已知问题:
- Tinker不支持修改AndroidManifest.xml,Tinker不支持新增四大组件;
- 由于Google Play的开发者条款限制,不建议在GP渠道动态更新代码;
- 在Android N上,补丁对应用启动时间有轻微的影响;
- 不支持部分三星android-21机型,加载补丁时会主动抛出”TinkerRuntimeException:checkDexInstall failed”;
- 对于资源替换,不支持修改remoteView。例如transition动画,notification icon以及桌面图标。