Bugly热更新集成,以及遇到的问题

这两天有在研究Bugly的热更新,集成后发现确实很方便,参考网址为:Bugly官网
一、添加插件依赖
工程根目录下“build.gradle”文件中添加
// tinkersupport 插件 , 其中 lastest.release 指拉取最新版本,也可以指定明确版本号,例如 1.0.4
classpath "com.tencent.bugly:tinker-support:1.0.8"
二、集成SDK
1)app module的“build.gradle”文件中添加
compile "com.android.support:multidex:1.0.2" // dex 配置 需要引入分包策略
compile 'com.tencent.bugly:crashreport_upgrade:1.3.1' //使用远程 Jcenter的策略引入依赖库
2)在app module的“build.gradle”文件中添加:
// 依赖插件脚本 apply from : 'tinker-support.gradle'
3)在app的根目录中创建tinker-support.gradle,内容如下所示

apply plugin: 'com.tencent.bugly.tinker-support'

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

/**
* 此处填写每次构建生成的基准包目录
* 使用Gradle/BuglyDemo/app/Tasks/build/assembleRelease 指令生成
* 存储到app/build/bakApk/对应时间点的目录中
*/
def baseApkDir = "app-1102-11-02-28" /** * 对于插件各参数的详细解析请参考 */ tinkerSupport { // 开启 tinker-support 插件,默认值 true enable = true // 指定归档目录,默认值当前 module 的子目录 tinker autoBackupApkDir = "${bakPath} " // 是否启用覆盖 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" // 构建基准包和补丁包都要指定不同的 tinkerId ,并且必须保证唯一性 tinkerId = "patch-2.3.0_2" // 构建多渠道补丁时使用 // buildAllFlavorsDir = "${bakPath}/${baseApkDir}" // 是否启用加固模式,默认为 false.(tinker-spport 1.0.7 起支持) // isProtectedApp = true // 是否开启反射 Application 模式 enableProxyApplication = 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-base" //applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" // 可选,设置 mapping 文件,建议保持旧 apk proguard 混淆方式 //applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可选,设置 R.txt 文件,通过旧 apk 文件保持 ResId 的分配 } }

//构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性
// 其中基础包打包时,建议使用“base-版本号-0”的形式更新包建议使用
//“pathc-版本号-补丁包号”
// 因为同一个基础包可能出现多个更新包的情况,建议使用标识标识出
// 更新包的补丁包号,便于与后台达成统一。

三、初始化SDK
enableProxyApplication = false 的情况
这是Tinker推荐的接入方式,一定程度上会增加接入成本,但具有更好的兼容性。
1)创建MyApplication,并继承自 TinkerApplication,最后在manifest中添加application的配置
/**
 * @author Lixm
 * @date 2017/7/6
 * @detail
 *   自定义Application.
 *
 * 注意:这个类集成TinkerApplication类,这里面不做任何操作,所有Application的代码都会放到ApplicationLike继承类当中<br/>
 * <pre>
 * 参数解析:
 * 参数1int tinkerFlags 表示Tinker支持的类型 dex onlylibrary only or all suuportdefault: TINKER_ENABLE_ALL
 * 参数2String delegateClassName Application代理类 这里填写你自定义的ApplicationLike
 * 参数3String loaderClassName  Tinker的加载器,使用默认即可
 * 参数4boolean tinkerLoadVerifyFlag  加载dex或者lib是否验证md5,默认为false
 * </pre>
 */

public class MyApplication extends TinkerApplication {

    public MyApplication() {
        super(ShareConstants.TINKER_ENABLE_ALL, "com.lixm.animationdemo.application.MyApplicationLike",
                "com.tencent.tinker.loader.TinkerLoader", false);
    }

}

2)创建MyApplicationLike实现类并继承自 DefaultApplicationLike ,实现原有Application中的所有功能,并配置相关的Bugly参数
  @Override
    public void onCreate() {
        super.onCreate();
        setDatabase();
        //xUtils进行初始化
        x.Ext.init(getApplication());
        //是否是开发、调试模式
        x.Ext.setDebug(true, true);//是否输出debug日志,开启debug会影响性能

        //设置是否开启热更新能力,默认为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) {
                LogUtil.w("补丁下载地址:"+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(), "补丁下载成功:"+msg, Toast.LENGTH_SHORT).show();
                LogUtil.w("补丁下载成功:"+msg);
            }

            @Override
            public void onDownloadFailure(String msg) {
                Toast.makeText(getApplication(), "补丁下载失败:"+msg, Toast.LENGTH_SHORT).show();
                LogUtil.w("补丁下载失败:"+msg);
            }

            @Override
            public void onApplySuccess(String msg) {
                Toast.makeText(getApplication(), "补丁应用成功:"+msg, Toast.LENGTH_SHORT).show();
                LogUtil.w("补丁应用成功:"+msg);
            }

            @Override
            public void onApplyFailure(String msg) {
                Toast.makeText(getApplication(), "补丁应用失败:"+msg, Toast.LENGTH_SHORT).show();
                LogUtil.w("补丁应用失败:"+msg);
            }

            @Override
            public void onPatchRollback() {
                LogUtil.w("==============onPatchRollback======");
            }
        };
        //设置开发设备,默认为false,上传补丁如果下发范围指定为"开发设备",需要调用此接口来标识开发设备
        Bugly.setIsDevelopmentDevice(getApplication(), true);
        //这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
        Bugly.init(getApplication(), "5ca40d59fb", true);
//        Bugly.init(getApplication(), "900029763", true);
        instance=this;
    }


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

 
  
 
 

注意:tinker需要你开启MultiDex,你需要在dependencies中进行配置 compile "com.android.support:multidex:1.0.1" 才可以使用MultiDex.install方法; SampleApplicationLike这个类是Application的代理类,以前所有在Application的实现必须要全部拷贝到这里,在 onCreate 方法调用SDK的初始化方法,在 onBaseContextAttached 中调用 Beta.installTinker(this);

enableProxyApplication = true的情况
直接在原有的application中添加bugly的配置参数,注意初始化方法使用如下方式
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    // you must install multiDex whatever tinker is installed!
    MultiDex.install(base);


    // 安装tinker
    Beta.installTinker();
}

四、AndroidManifest.xml配置
1)添加权限
<uses-permission android :name= "android.permission.READ_PHONE_STATE" />
<uses-permission android :name= "android.permission.INTERNET" />
<uses-permission android :name= "android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android :name= "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android :name= "android.permission.READ_LOGS" />
<uses-permission android :name= "android.permission.WRITE_EXTERNAL_STORAGE" />
2)添加特制activity和provider说明
<activity
    android:name="com.tencent.bugly.beta.ui.BetaActivity"
    android:configChanges="keyboardHidden|orientation|screenSize|locale"
    android:theme="@android:style/Theme.Translucent" />

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

其中provider_paths文件内容如下:
<? xml version= "1.0" encoding= "utf-8" ?>
<paths xmlns: android = "http://schemas.android.com/apk/res/android" >
<!-- /storage/emulated/0/Download/${applicationId}/.beta/apk-->
<external-path name= "beta_external_path" path= "Download/" />
<!--/storage/emulated/0/Android/data/${applicationId}/files/apk/-->
<external-path name= "beta_external_files_path" path= "Android/data/" />
</paths>
注:1.3.1及以上版本,可以不用进行以上配置,aar已经在AndroidManifest配置了,并且包含了对应的资源文件。
五、添加混淆配置
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}

-keep class android.support.**{*;}

至此,大部分配置均已完成,接下来就是实践的部分了
具体操作内容参照官网提示Bugly官网热更新详解地址点击打开链接
这里记录几点需要注意的地方:
1)使用Bugly前提,需要添加 multidex分包策略。
2)使用assembleRelease打包,需设置混淆策略。为了使开发人员运行的文件也包含打包,最好在build.gradle中配置好相关的打包签名策略。
3)正确的混淆打包后的文件包含app-release.apk,app-release-mapping.txt,app-release-R.txt 三个文件;未添加混淆策略打包后,没有app-release-mapping.txt,会造成后期应用策略失败。
4)生成热更新包时,主要基准包的路径配置,这里建议生成的准备上线的基准包做好备份。
5)后台上传的策略包使用的是build/outputs/patch中的patch_signed_7zip.apk
但是热更新不能作用于有四大组件变更的热更新操作。

有遇到的问题欢迎大家留言讨论,一起进步。。。

猜你喜欢

转载自blog.csdn.net/lxm20819/article/details/78425323
今日推荐