一文接入Android阿里Sophix热更新

        最近公司项目渐趋成熟,已经不需要经常更新版本,并且更新版本对客户的影响特别大,但是日常维护难免需要更新代码,因此热修复的技术,就比较迫切了。

        经过一段时间的对比,我们最终决定使用阿里的Sophix方案,因为它实在是太强大,而且接入及其简单。

        阿里网址如下:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

         应用申请这些因为过于简单,我这里就不说了,直接上代码

1、引入官方库,在app的build.gradle下,直接插入

 compile 'com.aliyun.ams:alicloud-android-hotfix:3.3.5'

2、心间自定义Application文件名为SophixStubApplication,继承自SophixApplication,代码如下:

public class SophixStubApplication extends SophixApplication {
    private final String TAG = "SophixStubApplication";
    // 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
    @Keep
    @SophixEntry(自定义的Application.class)
    static class RealApplicationStub {}
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//         如果需要使用MultiDex,需要在此处调用。
         MultiDex.install(this);
        initSophix();
    }
    private void initSophix() {
        String appVersion = "0.0.0";
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setUsingEnhance() // 适配加固模式,如果app使用了加固则需要加上此方法
                .setAppVersion(appVersion)
                .setSecretMetaData(null, null, null)
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.i(TAG, "sophix load patch success!");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // 如果需要在后台重启,建议此处用SharePreference保存状态。
                            Log.i(TAG, "sophix preload patch success. restart app to make effect.");
                        }
                    }
                }).initialize();
        instance.queryAndLoadNewPatch();
    }
}

简单说一下这里,基本是固定格式,最重要的代码是

instance.queryAndLoadNewPatch();

顾名思义,查询并加载新的补丁。这个一定要调用的,不然无法加载补丁,记得把SophixStubApplication注册到AndroidManifest.xml中,举个例子:

 <application
        android:name="xxxxxx.SophixStubApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:maxAspectRatio="2.4"
        android:resizeableActivity="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">


 <meta-data
            android:name="com.taobao.android.hotfix.IDSECRET"
            android:value="" />
        <meta-data
            android:name="com.taobao.android.hotfix.APPSECRET"
            android:value="" />
        <meta-data
            android:name="com.taobao.android.hotfix.RSASECRET"
            android:value="" />

</application>

这样一套下来,阿里云热修复就已经成功集成到app中。

接再来再说说一些注意事项

1、不能热更新的场景

        1)、新增四大组件

        2)、修改了SophixStubApplication的内容

        3)、修改了AndroidManifest.xml的内容

        4)、其他配置文件

        除此之外,其他的一切皆可更新,包括.so文件

2、关于加固的app,如果采用了加固,去打补丁的时候一定要拿未加固前的apk去操作,否则无法加固

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值