美文网首页Android
Bugly热修复,痛苦的接入

Bugly热修复,痛苦的接入

作者: 周蛋蛋 | 来源:发表于2019-06-04 17:48 被阅读0次

    说明

    • 要是打算接入热修复或者要学习热修复的同学,要是打算接入Tinker的,我劝你直接放弃,接入Bugly,都是马化腾家的,Bugly是依据Tinker的,底层都是一摸一样的,但是Bugly的文档要比Tinker的好一点哎,垃圾文档,所以,接入Bugly就行

    步骤

    • 官网申请APPID,不用我教
    • 工程build.graldle添加插件
      classpath "com.tencent.bugly:tinker-support:1.1.5"
    
    • 项目build.gradle添加依赖
    // 多dex配置
        implementation "com.android.support:multidex:1.0.1"
      //compile(name: 'bugly_crashreport_upgrade-1.3.2', ext: 'aar')
        // 远程仓库集成方式(推荐)
        //compile 'com.tencent.bugly:crashreport_upgrade:1.3.8'
        implementation 'com.tencent.bugly:crashreport_upgrade:latest.release'
        //1. 指定tinker依赖版本(注:应用升级1.3.5版本起,不再内置tinker)
        //2.为了便于解答问题,这里的tinker版本建议跟随此处demo设置,如果微信更新了tinker版本,bugly会定期同步更新
        implementation 'com.tencent.tinker:tinker-android-lib:1.9.9'
    
    • 创建tinker-suport.gradle文件,和app的build.gradle是同级别,拷贝我的就行


      image.png
    apply plugin: 'com.tencent.bugly.tinker-support'
    
    def bakPath = file("${buildDir}/bakApk/")
    
    /**
     * 此处填写每次构建生成的基准包目录
     */
    def baseApkDir = "app-0604-16-24-50"
    
    /**
     * 对于插件各参数的详细解析请参考,如果没有特殊需求下面的参数都可以不用更改;如果apk需要加固等可以参考具体描述设置参数
     */
    tinkerSupport {
    
        // 开启tinker-support插件,默认值true
        enable = true
        // 指定归档目录,默认值当前module的子目录tinker
        autoBackupApkDir = "${bakPath}"
        //建议设置true,用户就不用再自己管理tinkerId的命名,插件会为每一次构建的base包自动生成唯一的tinkerId,默认命名规则是versionname.versioncode_时间戳
        //具体参考https://github.com/BuglyDevTeam/Bugly-Android-Demo/wiki/Tinker-ID%E8%AF%A5%E6%80%8E%E4%B9%88%E8%AE%BE%E7%BD%AE
        autoGenerateTinkerId = true
        //tinkerId必须保证唯一性,如果两个base包的tinkerid是一样的,并且都联网激活了,那么后续补丁上传到后台的时候会出现匹配错误
        //tinkerId = "1.0.2-base"
        tinkerId = "1.0.2-patch"
        // 是否启用覆盖tinkerPatch配置功能,默认值false
        // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
        overrideTinkerPatchConfiguration = true
    
        // 编译补丁包时,必需指定基线版本的apk,默认值为空
        // 如果为空,则表示不是进行补丁包的编译
        // @{link tinkerPatch.oldApk }
        baseApk = "${bakPath}/${baseApkDir}/app-release.apk"
    
        // 对应tinker插件applyMapping
        baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"
    
        // 对应tinker插件applyResourceMapping
        baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"
    
    //    buildAllFlavorsDir = "${bakPath}/${baseApkDir}"
        // 是否开启加固模式,默认为false
        // isProtectedApp = true
    
        enableProxyApplication = false
    
        supportHotplugComponent = true
    
    // tinkerEnable功能开关
        tinkerEnable = true
    }
    
    /**
     * 一般来说,我们无需对下面的参数做任何的修改
     * 对于各参数的详细介绍请参考:
     * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
     */
    tinkerPatch {
        //oldApk ="${bakPath}/${appName}/app-release.apk"
        ignoreWarning = false
        useSign = true
        dex {
            dexMode = "jar"
            pattern = ["classes*.dex"]
            loader = []
        }
        lib {
            pattern = ["lib/*/*.so"]
        }
    
        res {
            pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
            ignoreChange = []
            largeModSize = 100
        }
    
        packageConfig {
        }
        sevenZip {
            zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
    //        path = "/usr/local/bin/7za"
        }
        buildConfig {
            keepDexApply = false
            //tinkerId = "1.0.1-patch"
            //applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" //  可选,设置mapping文件,建议保持旧apk的proguard混淆方式
            //applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配
        }
    }
    
    image.png
    • 在项目的build.gradle中引入tinker-suport.gradle
      依赖插件脚本
    apply from: 'tinker-support.gradle'
    
    image.png
    • 创建SampleApplicationLike
    package com.tencent.bugly.hotfix;
    
    import android.annotation.TargetApi;
    import android.app.Application;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.support.multidex.MultiDex;
    import android.widget.Toast;
    
    //import com.meituan.android.walle.WalleChannelReader;
    import com.tencent.bugly.Bugly;
    import com.tencent.bugly.beta.Beta;
    import com.tencent.bugly.beta.interfaces.BetaPatchListener;
    import com.tencent.tinker.entry.DefaultApplicationLike;
    
    import com.tencent.tinker.entry.DefaultApplicationLike;
    import java.util.Locale;
    
    /**
     * 自定义ApplicationLike类.
     *
     * 注意:这个类是Application的代理类,以前所有在Application的实现必须要全部拷贝到这里<br/>
     *
     * @author wenjiewu
     * @since 2016/11/7
     */
    public class SampleApplicationLike extends DefaultApplicationLike {
    
        public static final String TAG = "Tinker.SampleApplicationLike";
    
        public SampleApplicationLike(Application application, int tinkerFlags,
                boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
                long applicationStartMillisTime, Intent tinkerResultIntent) {
            super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime,
                    applicationStartMillisTime, tinkerResultIntent);
        }
    
    
        @Override
        public void onCreate() {
            super.onCreate();
            // 设置是否开启热更新能力,默认为true
            Beta.enableHotfix = true;
            // 设置是否自动下载补丁,默认为true
            Beta.canAutoDownloadPatch = true;
            // 设置是否自动合成补丁,默认为true
            Beta.canAutoPatch = true;
            // 设置是否提示用户重启,默认为false
            Beta.canNotifyUserRestart = true;
            // 补丁回调接口
            Beta.betaPatchListener = new BetaPatchListener() {
                @Override
                public void onPatchReceived(String patchFile) {
                    Toast.makeText(getApplication(), "补丁下载地址" + patchFile, Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onDownloadReceived(long savedLength, long totalLength) {
                    Toast.makeText(getApplication(),
                            String.format(Locale.getDefault(), "%s %d%%",
                                    Beta.strNotificationDownloading,
                                    (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)),
                            Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onDownloadSuccess(String msg) {
                    Toast.makeText(getApplication(), "补丁下载成功", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onDownloadFailure(String msg) {
                    Toast.makeText(getApplication(), "补丁下载失败", Toast.LENGTH_SHORT).show();
    
                }
    
                @Override
                public void onApplySuccess(String msg) {
                    Toast.makeText(getApplication(), "补丁应用成功", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onApplyFailure(String msg) {
                    Toast.makeText(getApplication(), "补丁应用失败", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onPatchRollback() {
    
                }
            };
    
            // 设置开发设备,默认为false,上传补丁如果下发范围指定为“开发设备”,需要调用此接口来标识开发设备
            Bugly.setIsDevelopmentDevice(getApplication(), true);
            // 多渠道需求塞入
            // String channel = WalleChannelReader.getChannel(getApplication());
            // Bugly.setAppChannel(getApplication(), channel);
            // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
            Bugly.init(getApplication(), "75dbc2164d", true);
    //"75dbc2164d"这个需要替换成你自己申请的Id
        }
    
    
        @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);
    
            // TODO: 安装tinker
            Beta.installTinker(this);
        }
    
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        public void registerActivityLifecycleCallback(
                Application.ActivityLifecycleCallbacks callbacks) {
            getApplication().registerActivityLifecycleCallbacks(callbacks);
        }
    
        @Override
        public void onTerminate() {
            super.onTerminate();
            Beta.unInit();
        }
    }
    
    
    • 让你自己的Application继承TinkerApplication
    public class SampleApplication extends TinkerApplication {
        public SampleApplication() {
            super(ShareConstants.TINKER_ENABLE_ALL, "com.tencent.bugly.hotfix.SampleApplicationLike",
                    "com.tencent.tinker.loader.TinkerLoader", false);
        }
    }
    
    image.png

    标的这些需要改成你自己的东西

    重点来了

    • 我们先配置好以后,打版我们的基版本(说白了就是我们线上运行的bug版本或者说就是修复之前的那个版本)
    image.png
    image.png
    • 这个最好和versionName保持同步,然后将tinkerId变为base这个


      image.png

      [图片上传中...(image.png-a022e8-1559640390459-0)]

    然后点击这个生成正式版本,正式版本是需要签名的,这个在这里不叙述

    • 生成基类APP


      image.png

      *然后你修改了APP的bug,需要生成补丁


      image.png
      1.修改第一步,变成patch.
      2.点击上面的编译
      image.png

      生成补丁

    • 选择上传补丁


      image.png

    记得选择这个上传,一定记得不要选择上面的那个,不然会报错

    又发现一个奇怪的事情,上面这套代码在有的项目有问题,出现的错误如下

    com.tencent.tinker.build.util.TinkerPatchException: loader classes are found in old secondary dex. Found classes: {Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_01;,Lcom/tencent/tinker/loader/shareutil/ShareResPatchInfo;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V23;,Lcom/tencent/tinker/loader/hotplug/ActivityStubManager;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil$1;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil;,Lcom/tencent/tinker/loader/shareutil/SharePatchFileUtil;,Lcom/tencent/tinker/loader/shareutil/ShareFileLockHelper;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_04;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$StreamConsumer$1;,Lcom/tencent/tinker/loader/TinkerDexLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_06;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$1;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$StreamConsumer;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager;,Lcom/tencent/tinker/loader/AndroidNClassLoader;,Lcom/tencent/tinker/loader/TinkerResourcesKey;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_09;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_07;,Lcom/tencent/tinker/loader/shareutil/ShareIntentUtil;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_00;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V24;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_05;,Lcom/tencent/tinker/loader/TinkerTestAndroidNClassLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_07;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_00_T;,Lcom/tencent/tinker/loader/hotplug/handler/MHMessageHandler;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$SectionHeader;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$ResultCallback;,Lcom/tencent/tinker/loader/TinkerResourceLoader;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V19;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_06;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_08;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$BinderInvocationHandler;,Lcom/tencent/tinker/loader/shareutil/ShareResPatchInfo$LargeModeInfo;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs;,Lcom/tencent/tinker/loader/hotplug/interceptor/TinkerHackInstrumentation;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V19;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_02;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_09;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_00;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_02_T;,Lcom/tencent/tinker/loader/TinkerTestDexLoad;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$ProgramHeader;,Lcom/tencent/tinker/loader/TinkerSoLoader;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor$CallbackWrapper;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_01_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_08;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile$ElfHeader;,Lcom/tencent/tinker/loader/TinkerResourcePatcher;,Lcom/tencent/tinker/loader/shareutil/ShareTinkerInternals;,Lcom/tencent/tinker/loader/hotplug/ComponentHotplug;,Lcom/tencent/tinker/loader/R;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_07;,Lcom/tencent/tinker/loader/TinkerDexOptimizer$OptimizeWorker;,Lcom/tencent/tinker/loader/hotplug/interceptor/InterceptFailedException;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_01;,Lcom/tencent/tinker/loader/BuildConfig;,Lcom/tencent/tinker/loader/TinkerLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_01;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V4;,Lcom/tencent/tinker/loader/hotplug/interceptor/Interceptor$ITinkerHotplugProxy;,Lcom/tencent/tinker/loader/shareutil/ShareSecurityCheck;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_09;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_00;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$FakeInterfaceHandler;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_08;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_06;,Lcom/wisdom/patient/base/BaseApplication;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V17;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_00;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor;,Lcom/tencent/tinker/loader/shareutil/ShareBsDiffPatchInfo;,Lcom/tencent/tinker/loader/shareutil/ShareReflectUtil;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_02;,Lcom/tencent/tinker/loader/TinkerUncaughtHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_02_T;,Lcom/tencent/tinker/loader/TinkerDexOptimizer;,Lcom/tencent/tinker/loader/shareutil/ShareConstants;,Lcom/tencent/tinker/loader/shareutil/SharePatchInfo;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_09;,Lcom/tencent/tinker/loader/hotplug/handler/PMSInterceptHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_05;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_01;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$V14;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_03;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager$AttrTranslator;,Lcom/tencent/tinker/loader/shareutil/ShareOatUtil$InstructionSet;,Lcom/tencent/tinker/loader/app/TinkerApplication;,Lcom/tencent/tinker/loader/hotplug/interceptor/ServiceBinderInterceptor$FakeClientBinderHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_02;,Lcom/tencent/tinker/loader/hotplug/UnsupportedEnvironmentException;,Lcom/tencent/tinker/loader/TinkerRuntimeException;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_04;,Lcom/tencent/tinker/loader/hotplug/interceptor/HandlerMessageInterceptor$MessageHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_06;,Lcom/tencent/tinker/loader/TinkerDexLoader$1;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_00_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_02_T;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_04;,Lcom/tencent/tinker/loader/hotplug/IncrementComponentManager$1;,Lcom/tencent/tinker/loader/hotplug/EnvConsts;,Lcom/tencent/tinker/loader/hotplug/handler/AMSInterceptHandler;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_05;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_03;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SIStub_02;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_07;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$STDStub_05;,Lcom/tencent/tinker/loader/AbstractTinkerLoader;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_02_T;,Lcom/tencent/tinker/loader/SystemClassLoaderAdder$1;,Lcom/tencent/tinker/loader/TinkerResourcesKey$V7;,Lcom/tencent/tinker/loader/shareutil/ShareDexDiffPatchInfo;,Lcom/tencent/tinker/loader/shareutil/ShareElfFile;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTKStub_08;,Lcom/tencent/tinker/loader/hotplug/ActivityStubs$SGTStub_04;,Lcom/tencent/tinker/loader/hotplug/interceptor/Interceptor;}
        at com.tencent.tinker.build.util.ExcludedClassModifiedChecker.checkIfExcludedClassWasModifiedInNewDex(ExcludedClassModifiedChecker.java:193)
        at com.tencent.tinker.build.decoder.DexDiffDecoder.patch(DexDiffDecoder.java:136)
        at com.tencent.tinker.build.decoder.UniqueDexDiffDecoder.patch(UniqueDexDiffDecoder.java:39)
        at com.tencent.tinker.build.decoder.ApkDecoder$ApkFilesVisitor.visitFile(ApkDecoder.java:176)
        ... 59 more
    

    com.tencent.tinker.build.util.TinkerPatchException: loader classes are found in old secondary dex. Found classes:
    如果说官方给的无法解决这个办法的话,试试我的这个


    image.png

    在tinker-support.gradle里面添加这句话就成功了

        ignoreWarning = true
    

    还有就是更新不是实时的,需要等10来分钟才行

    相关文章

      网友评论

        本文标题:Bugly热修复,痛苦的接入

        本文链接:https://www.haomeiwen.com/subject/hxlrxctx.html