美文网首页
阿里百川热修复--Sophix使用

阿里百川热修复--Sophix使用

作者: 若兮生生 | 来源:发表于2017-07-20 15:34 被阅读684次

    一:gradle配置
    1.Android studio 引入 需要使用PermissionsDispatcher,需要在project的 build.gradle中添加

    //这个运行时权限用的插件
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    

    2.在app module中的build.gradle中添加:

    //和dependencies同级别
    repositories {
        mavenLocal()
        //阿里云仓库
        maven {
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        }
        mavenCentral()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
    
    
    dependencies {
         compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:26.+'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
    
    //这是hotfix的依赖
        compile 'com.aliyun.ams:alicloud-android-hotfix:3.0.6'
    //下面这些是运行时权限的依赖
        compile("com.github.hotchemi:permissionsdispatcher:2.4.0") {
            exclude module: "support-v13"
        }
        apt "com.github.hotchemi:permissionsdispatcher-processor:2.4.0"
    }
    

    二:AndroidManifest.xml配置
    1.权限

        <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_EXTERNAL_STORAGE"/>
    

    2.启用自定义的Application,下面对MyApp说明

        <application
            android:name=".MyaApp"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    

    3.账号密码配置,这些在阿里百川创建应用时会给出。http://baichuan.taobao.com/ 登录进去后,点击控制台,创建应用后会分配。将下面value替换成分配的。第三个需要下载,特别长。

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

    三:Application配置,重写onCreat()方法,并且调用如下方法。

        public interface MsgDisplayListener {
            void handle(String msg);
        }
        public static MsgDisplayListener msgDisplayListener = null;
        public static StringBuilder cacheMsg = new StringBuilder();
    
        @Override
        public void onCreate() {
            super.onCreate();
            initHotfix();
        }
    
     private void initHotfix() {
            String appVersion;
            try {
                appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
            } catch (Exception e) {
                appVersion = "1.0.0";
            }
    
            SophixManager.getInstance().setContext(this)
                    .setAppVersion(appVersion)
                    .setAesKey(null)
                    //.setAesKey("0123456789123456")
                    .setEnableDebug(true)
                    .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                        @Override
                        public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                            String msg = new StringBuilder("").append("Mode:").append(mode)
                                    .append(" Code:").append(code)
                                    .append(" Info:").append(info)
                                    .append(" HandlePatchVersion:").append(handlePatchVersion).toString();
                            if (msgDisplayListener != null) {
                                msgDisplayListener.handle(msg);
                            } else {
                                cacheMsg.append("\n").append(msg);
                            }
                        }
                    }).initialize();
    
        }
    

    四:在Activity里面调用下载patch。注意6.0以上要对读写权限处理。下载时用到这个权限。

     SophixManager.getInstance().queryAndLoadNewPatch();
    

    五.生成补丁包。阿里提供了一个工具 http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_windows.zip
    解压后,运行 SophixPatchTool.exe 文件得到如下:

    1500535430(1).jpg

    选择旧的apk,新的apk,指定补丁的输出路径 点击go就会生成补丁文件

    六:上传补丁包
    在创建应用 应用管理的地方
    这里注意添加版本号要和打包时用的版本号一致。

    七:工具调试

    http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/hotfix_debug_tool-release.apk

    八:这里有官方详细的介绍
    https://help.aliyun.com/document_detail/53247.html?spm=5176.doc53248.6.548.uET8cz

    相关文章

      网友评论

          本文标题:阿里百川热修复--Sophix使用

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