使用Tinker开源框架做热处理修复

来源:互联网 发布:网络冲洗照片靠谱吗 编辑:程序博客网 时间:2024/05/17 22:01

如果发布到市场的应用突然发现了一个已修复的小bug,如果为了修复它,传统的方法是要发布一个新版本,不过有了tinker后可以用热更新修复,用户体验更好.

一.Tinker 的简单上手

以下是腾讯gihub官方地址

Tinker 接入指南

一定要先把官方的demo下载下来测试 直接运行 tinker-sample-android

官方demo

二.集成Tinker与各种坑

application

//这里的application是manifest里面的 不需要实际写出类@DefaultLifeCycle(application = "com.mi.mydemo.Application.MyTinkerApplication",        flags = ShareConstants.TINKER_ENABLE_ALL,        loadVerifyFlag = false)//更改为继承DefaultApplicationLikepublic 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就好了

1.png


当然上一个版本的app版本也要用release

2.png

如果用release需要子啊gradle配置keystore

Gradle配置keystore

可能自己写demo测试的时候 加载热更新后软件就重启了,这种体验非常不好. 这个要看看腾讯的官方demo,demo有个service继承DefaultTinkerResultService 设置的模式是检测软件后台了就更新,这样用户就没有感知到软件更新的过程

* * 通过这个service来控制应用热更新完成后 在应用后台的时候自动更新  * */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);        }    }}

三.总结

有了tinker可以软件启动的时候去检测后台有没有配置热更新 有的话先下载 然后更新修复bug.从此修复bug就是如此简单.

原创粉丝点击