如果發(fā)布到市場的應(yīng)用突然發(fā)現(xiàn)了一個已修復的小bug,如果為了修復它,傳統(tǒng)的方法是要發(fā)布一個新版本,不過有了tinker后可以用熱更新修復,用戶體驗更好.
一.Tinker 的簡單上手
以下是騰訊gihub官方地址
Tinker 接入指南
以下幾篇寫的非常好的blog
Tinker 熱修復框架 簡單上手教程
微信tinker快速集成
一定要先把官方的demo下載下來測試 直接運行 tinker-sample-android
官方demo
二.集成Tinker與各種坑
application
//這里的application是manifest里面的 不需要實際寫出類
@DefaultLifeCycle(application = "com.mi.mydemo.Application.MyTinkerApplication",
flags = ShareConstants.TINKER_ENABLE_ALL,
loadVerifyFlag = false)
//更改為繼承DefaultApplicationLike
public class MyApplication extends DefaultApplicationLike {
private static final String TAG = "MyApplication";
private static MyApplication instance;
private Context mContext;
public MyApplication(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent, Resources[] resources, ClassLoader[] classLoader, AssetManager[] assetManager) {
super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent, resources, classLoader, assetManager);
}
//以前application寫在oncreate的東西搬到這里來初始化
@Override
public void onBaseContextAttached(Context base) {
super.onBaseContextAttached(base);
MultiDex.install(base);
TinkerInstaller.install(this);
instance = this;
this.mContext = base;
SQLiteDatabase.loadLibs(base);//sqlcipher初始化
}
AndroidManifest
<application
//這個application不用寫出實體類出來,在上面MyApplication中
用注解聲明一個就好了
android:name=".Application.MyTinkerApplication"
Demo里面測試的是debug 如果是正式上線的需要點擊以下release,點擊后生成差分包如果太慢就重啟Androidstudio然后點擊release就好了
當然上一個版本的app版本也要用release
如果用release需要子啊gradle配置keystore
Gradle配置keystore
可能自己寫demo測試的時候 加載熱更新后軟件就重啟了,這種體驗非常不好. 這個要看看騰訊的官方demo,demo有個service繼承DefaultTinkerResultService 設(shè)置的模式是檢測軟件后臺了就更新,這樣用戶就沒有感知到軟件更新的過程
*
* 通過這個service來控制應(yīng)用熱更新完成后 在應(yīng)用后臺的時候自動更新
*
*/
public class SampleResultService extends DefaultTinkerResultService {
private static final String TAG = "Tinker.SampleResultService";
@Override
public void onPatchResult(final PatchResult result) {
if (result == null) {
TinkerLog.e(TAG, "SampleResultService received null result!!!!");
return;
}
TinkerLog.i(TAG, "SampleResultService receive result: %s", result.toString());
//first, we want to kill the recover process
TinkerServiceInternals.killTinkerPatchServiceProcess(getApplicationContext());
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (result.isSuccess) {
Toast.makeText(getApplicationContext(), "patch success, please restart process", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "patch fail, please check reason", Toast.LENGTH_LONG).show();
}
}
});
// is success and newPatch, it is nice to delete the raw file, and restart at once
// for old patch, you can't delete the patch file
if (result.isSuccess && result.isUpgradePatch) {
File rawFile = new File(result.rawPatchFilePath);
if (rawFile.exists()) {
TinkerLog.i(TAG, "save delete raw patch file");
SharePatchFileUtil.safeDeleteFile(rawFile);
}
//not like TinkerResultService, I want to restart just when I am at background!
//if you have not install tinker this moment, you can use TinkerApplicationHelper api
if (checkIfNeedKill(result)) {
if (Utils.isBackground()) { //后臺了再更新
TinkerLog.i(TAG, "it is in background, just restart process");
restartProcess();
} else {
//we can wait process at background, such as onAppBackground
//or we can restart when the screen off
TinkerLog.i(TAG, "tinker wait screen to restart process");
new ScreenState(getApplicationContext(), new ScreenState.IOnScreenOff() {
@Override
public void onScreenOff() {
restartProcess();
}
});
}
} else {
TinkerLog.i(TAG, "I have already install the newly patch version!");
}
}
//repair current patch fail, just clean!
if (!result.isSuccess && !result.isUpgradePatch) {
//if you have not install tinker this moment, you can use TinkerApplicationHelper api
Tinker.with(getApplicationContext()).cleanPatch();
}
}
/**
* you can restart your process through service or broadcast
*/
private void restartProcess() {
TinkerLog.i(TAG, "app is background now, i can kill quietly");
//you can send service or broadcast intent to restart your process
android.os.Process.killProcess(android.os.Process.myPid());
}
static class ScreenState {
interface IOnScreenOff {
void onScreenOff();
}
ScreenState(Context context, final IOnScreenOff onScreenOffInterface) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent in) {
String action = in == null ? "" : in.getAction();
TinkerLog.i(TAG, "ScreenReceiver action [%s] ", action);
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
context.unregisterReceiver(this);
if (onScreenOffInterface != null) {
onScreenOffInterface.onScreenOff();
}
}
}
}, filter);
}
}
}
三.總結(jié)
有了tinker可以軟件啟動的時候去檢測后臺有沒有配置熱更新 有的話先下載 然后更新修復bug.從此修復bug就是如此簡單.