Bugly 热修复

1.导入依赖及插件


根Gradle
moduel  Gradle
moduel  Gradle

2.配置插件脚本   tinker-support.gradle

applyplugin:'com.tencent.bugly.tinker-support'

def bakPath = file("${buildDir}/bakApk/")

def baseApkDir ="app-0825-11-40-17"

tinkerSupport {

    enable =true  // 开启tinker-support插件,默认值true

  autoBackupApkDir ="${bakPath}"    // 指定归档目录,默认值当前module的子目录tinker

    //建议设置true,用户就不用再自己管理tinkerId的命名,插件会为每一次构建的base包自动生成唯一的tinkerId

    autoGenerateTinkerId =true

    //tinkerId必须保证唯一性,如果两个base包的tinkerid是一样的,并且都联网激活了,那么后续补丁上传到后台的时候会出现匹配错误

    tinkerId ="if autoGenerateTinkerId=true ,no need set here"

    // 是否启用覆盖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

}

/**

* 一般来说,我们无需对下面的参数做任何的修改

*/

tinkerPatch {

//oldApk ="${bakPath}/${appName}/app-debug.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-debug-mapping.txt" //  可选,设置mapping文件,建议保持旧apk的proguard混淆方式

//applyResourceMapping = "${bakPath}/${appName}/app-debug-R.txt" // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配

    }

}

3.配置AndroidMainfirst

AndroidMainfirst
provider_paths

4.配置Application

public class SampleApplicationextends TinkerApplication {

public SampleApplication() {

super(ShareConstants.TINKER_ENABLE_ALL, "com.xx.xxx.SampleApplicationLike",

              "com.tencent.tinker.loader.TinkerLoader", false);

    }}

SampleApplicationLike 是Application的代理类,以前所有在Application的实现必须要全部拷贝到这里

public class SampleApplicationLike extends DefaultApplicationLike {

public static final StringTAG ="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();

        Beta.enableHotfix =true;  // 设置是否开启热更新能力,默认为true

        Beta.canAutoDownloadPatch =true;  // 设置是否自动下载补丁,默认为true

        Beta.canAutoPatch =true;  // 设置是否自动合成补丁,默认为true

        Beta.canNotifyUserRestart =true;// 设置是否提示用户重启,默认为false

// 补丁回调接口

        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(), "d9e0ae879b", true);

    }

@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();

    }

}

MainActivity中添加

override fun onBackPressed() {

super.onBackPressed()

Log.e("MainActivity", "onBackPressed")

Beta.unInit()

}

5.打包及生成补丁

1.打包(未发现bug 补丁生成之前):


2.修复bug 生成补丁

生成的补丁包在build/outputs/patch目录下:


6.上传补丁及修复


打开应用后程序关闭后台后再次启动时自动显示:(补丁刚上传之后需等待10分左右会有结果)

你可能感兴趣的:(Bugly 热修复)