看github上的wiki,使用gradle接入Tinker。
根據提示導入tinker-sample-android demo。
調用assembleDebug編譯,我們會將編譯過的包保存在build/bakApk中。然後我們將它安裝到手機,點擊SHOW INFO按鈕,可以看到補丁並沒有加載.
生成後的apk存放位置在
修改代碼,例如將MainActivity中I am on patch onCreate的Log打開。然後我們需要修改build.gradle中的參數,將步驟一編譯保存的安裝包路徑拷貝到tinkerPatch中的oldApk參數中。
oldApk = getOldApkPath() 找到 getOldApkPath()方法
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-debug-0329-16-29-56.apk"
//proguard mapping file to build patch apk
tinkerApplyMappingPath = "${bakPath}/app-debug-1018-17-32-47-mapping.txt"
//resource R.txt to build patch apk, must input if there is resource changed
tinkerApplyResourcePath = "${bakPath}/app-debug-1018-17-32-47-R.txt"
//only use for build all flavor, if not, just ignore this field
tinkerBuildFlavorDirectory = "${bakPath}/app-1018-17-32-47"
}
def getOldApkPath() {
return hasProperty("OLD_APK") ? OLD_APK : ext.tinkerOldApkPath
}
可以看到getOldApkPath方法最後引用的是ext.tinkerOldApkPath,找到他,
tinkerOldApkPath = "${bakPath}/你生成apk的名字(如app-debug-0329-16-29-56).apk"
調用tinkerPatchDebug, 補丁包與相關日誌會保存在/build/outputs/tinkerPatch/。然後我們將patch_signed_7zip.apk推送到手機的sdcard中。下面截圖是推到手機目錄上和安裝apk和下載apk的adb命令,還有重要的一點是推送apk到手機sd中的路徑是從MainActivity的loadPatchButton觸發的,這裏圖推送apk的地址可以自己更改。ps:手機模擬器的sd卡地址是/storage/emulated/0/。
最後一個驗證是否load patch並且打印了i am on patch onCreatelog。
點擊LOAD PATCH按鈕, 如果看到patch success, please restart process的toast,即可鎖屏或者點擊KILL SELF按鈕(重啓app)。我們可以看到的確出現了I am on patch onCreate日誌,同時點擊SHOW INFO按鈕,顯示補丁包的確已經加載成功了。
ps:(●’◡’●)偷了張Tinker的圖。