记录一下热更新流程,方便以后使用。
tinker项目地址:
https://github.com/Tencent/tinker
项目里包含了lib的源码和sample,下下来,然后打开tinker-sample-android:
修改mainActivity中textview:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is old apk"/>
通过gradle生成release版的apk:
在app/build/bakApk目录下会生成apk,混淆映射文件和资源文件:
通过adb运行:
adb install -r -d F:\AndroidProgram\Demo\tinker\tinker-sample-android\app\build\bakApk\app-release-1025-10-46-35.apk
下面修改app的build.gradle文件:
ext {
//for some reason, you may want to ignore tinkerBuild, such as instant run debug build?
tinkerEnabled = true
//for normal build
//old apk file to build patch apk
tinkerOldApkPath = "${bakPath}/app-release-1025-10-46-35.apk"
//proguard mapping file to build patch apk
tinkerApplyMappingPath = "${bakPath}/app-release-1025-10-46-35-mapping.txt"
//resource R.txt to build patch apk, must input if there is resource changed
tinkerApplyResourcePath = "${bakPath}/app-release-1025-10-46-35-R.txt"
//only use for build all flavor, if not, just ignore this field
tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}
将打包生成的老apk的三个文件对应填入tinkerOldApkPath、tinkerApplyMappingPath、tinkerApplyResourcePath,然后我们准备新版本apk,修改代码,也是改mainactivity中的textview:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is new apk"/>
不要clean,不然android studio会删除之前打包好的apk,然后用gradle里的tinker打包:
会生成新的release版apk:
tinker对比新旧apk的差异生成一个补丁文件patch_signed_7zip.apk:
把这个补丁文件push到手机内存根目录:
adb push F:\AndroidProgram\Demo\tinker\tinker-sample-android\app\build\outputs\tinkerPatch\release\patch_signed_7zip.apk /sdcard/
回到运行的app中,点击LOAD PATCH:
然后点击KILL SELF,再次进入app,改动就生效了,同时删除了存储卡上的补丁文件:
demo里还提供了去掉补丁功能,点击CLEAN PATCH,再KILL SELF,重启app就是旧的版本。