• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

纯Flutter项目集成Sophix-阿里热更新服务

武飞扬头像
布凃
帮助5

之前用的是腾讯的热更新服务,但过几天腾讯就要停止服务了,换成阿里云的热修复。以前没做过原生,搞得比较痛苦,记录一下!

只有集成安卓端 

平台操作

  1. 阿里云官网创建账号并认证阿里云官网
  2. 移动热修复页选择开通移动热修复服务(免费的也够用了)
  3. 添加项目及应用(只使用热修复服务可以不下载这个json文件)

    学新通

  4. 在右方研发工具中开启移动热修复学新通

代码集成

 官方文档挺详细的,不过我也记录一下我的步骤,方便以后复用(官方文档地址

  • 配置AndroidManifest.xml 
  1.  
    <uses-permission android:name="android.permission.INTERNET"/>
  2.  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3.  
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  4.  
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  5.  
    ······
  6.  
    <!-- SophixStubApplication为自定义新增的sophix入口类名字 -->
  7.  
    <application
  8.  
    android:name=".SophixStubApplication"
  9.  
    ······>
  10.  
    ······
  11.  
    <meta-data
  12.  
    android:name="com.taobao.android.hotfix.IDSECRET"
  13.  
    android:value="" />
  14.  
    <meta-data
  15.  
    android:name="com.taobao.android.hotfix.APPSECRET"
  16.  
    android:value="" />
  17.  
    <meta-data
  18.  
    android:name="com.taobao.android.hotfix.RSASECRET"
  19.  
    android:value="" />
  20.  
    ······
  21.  
    </application>
学新通
  •  配置build.gradle
  1.  
    dependencies {
  2.  
    ······
  3.  
    implementation 'com.aliyun.ams:alicloud-android-hotfix:3.3.5'
  4.  
    ······
  5.  
    }
  1.  
    repositories {
  2.  
    ······
  3.  
    maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases/' }
  4.  
    ······
  5.  
    }
  • 混淆配置 proguard-rules.pro
  1.  
    # sophix
  2.  
    #基线包使用,生成mapping.txt
  3.  
    -printmapping mapping.txt
  4.  
    #生成的mapping.txt在app/build/outputs/mapping/release路径下,移动到/app路径下
  5.  
    #修复后的项目使用,保证混淆结果一致
  6.  
    #-applymapping mapping.txt
  7.  
    #hotfix
  8.  
    -keep class com.taobao.sophix.**{*;}
  9.  
    -keep class com.ta.utdid2.device.**{*;}
  10.  
    -dontwarn com.alibaba.sdk.android.utils.**
  11.  
    #防止inline
  12.  
    -dontoptimize
  13.  
     
  14.  
    -keepclassmembers class com.my.demo.MyApplication {
  15.  
    public <init>();
  16.  
    }
  17.  
    # 如果不使用android.support.annotation.Keep则需加上此行
  18.  
    # -keep class com.my.pkg.SophixStubApplication$RealApplicationStub
学新通
  • Sophix入口类 

        贴一下官方代码 

  1.  
    import android.app.Application;
  2.  
    import android.content.Context;
  3.  
    import android.support.annotation.Keep;
  4.  
    import android.util.Log;
  5.  
    import com.taobao.sophix.PatchStatus;
  6.  
    import com.taobao.sophix.SophixApplication;
  7.  
    import com.taobao.sophix.SophixEntry;
  8.  
    import com.taobao.sophix.SophixManager;
  9.  
    import com.taobao.sophix.listener.PatchLoadStatusListener;
  10.  
    import com.my.pkg.MyRealApplication;
  11.  
    /**
  12.  
    * Sophix入口类,专门用于初始化Sophix,不应包含任何业务逻辑。
  13.  
    * 此类必须继承自SophixApplication,onCreate方法不需要实现。
  14.  
    * 此类不应与项目中的其他类有任何互相调用的逻辑,必须完全做到隔离。
  15.  
    * AndroidManifest中设置application为此类,而SophixEntry中设为原先Application类。
  16.  
    * 注意原先Application里不需要再重复初始化Sophix,并且需要避免混淆原先Application类。
  17.  
    * 如有其它自定义改造,请咨询官方后妥善处理。
  18.  
    */
  19.  
    public class SophixStubApplication extends SophixApplication {
  20.  
    private final String TAG = "SophixStubApplication";
  21.  
    // 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
  22.  
    @Keep
  23.  
    @SophixEntry(MyRealApplication.class)
  24.  
    static class RealApplicationStub {}
  25.  
    @Override
  26.  
    protected void attachBaseContext(Context base) {
  27.  
    super.attachBaseContext(base);
  28.  
    // 如果需要使用MultiDex,需要在此处调用。
  29.  
    // MultiDex.install(this);
  30.  
    initSophix();
  31.  
    }
  32.  
    private void initSophix() {
  33.  
    String appVersion = "0.0.0";
  34.  
    try {
  35.  
    appVersion = this.getPackageManager()
  36.  
    .getPackageInfo(this.getPackageName(), 0)
  37.  
    .versionName;
  38.  
    } catch (Exception e) {
  39.  
    }
  40.  
    final SophixManager instance = SophixManager.getInstance();
  41.  
    instance.setContext(this)
  42.  
    .setAppVersion(appVersion)
  43.  
    .setSecretMetaData(null, null, null)
  44.  
    .setEnableDebug(true)
  45.  
    .setEnableFullLog()
  46.  
    .setPatchLoadStatusStub(new PatchLoadStatusListener() {
  47.  
    @Override
  48.  
    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
  49.  
    if (code == PatchStatus.CODE_LOAD_SUCCESS) {
  50.  
    Log.i(TAG, "sophix load patch success!");
  51.  
    } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
  52.  
    // 如果需要在后台重启,建议此处用SharePreference保存状态。
  53.  
    Log.i(TAG, "sophix preload patch success. restart app to make effect.");
  54.  
    }
  55.  
    }
  56.  
    }).initialize();
  57.  
    }
  58.  
    }
学新通

         我们需要在PatchLoadStatusListener中添加

  1.  
    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
  2.  
    if (code == PatchStatus.CODE_LOAD_SUCCESS) {
  3.  
    String filesPath = getBaseContext().getFilesDir().getAbsolutePath();
  4.  
    String parentPath = getBaseContext().getFilesDir().getParentFile().getAbsolutePath();
  5.  
    String patchFilePath = FlutterPatch.findLibraryFromSophix(getBaseContext(), filesPath "/sophix", "libapp.so");
  6.  
    SharedPreferences settings = getBaseContext().getSharedPreferences("FlutterSharedPreferences", 0);
  7.  
    int sophixPatchVersion = settings.getInt("flutter.sophixPatchVersion",-99);
  8.  
    boolean isNewPatch = sophixPatchVersion == -99 || handlePatchVersion != sophixPatchVersion;
  9.  
    if (!new File(parentPath "/libapp.so").exists() || isNewPatch) {
  10.  
    //拷贝libapp.so到配置好的加载路径
  11.  
    FlutterPatch.copyFileByPath(patchFilePath, parentPath "/libapp.so");
  12.  
    File sof = new File(parentPath "/libapp.so");
  13.  
    sof.setExecutable(true);
  14.  
    sof.setReadable(true);
  15.  
    SharedPreferences.Editor editor = settings.edit();
  16.  
    editor.putInt("flutter.sophixPatchVersion", handlePatchVersion);
  17.  
    editor.commit();
  18.  
    Log.i(TAG, "sophix补丁加载成功!版本号为" handlePatchVersion);
  19.  
    } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
  20.  
    // 如果需要在后台重启,建议此处用SharePreference保存状态。
  21.  
    long now = System.currentTimeMillis();
  22.  
    SharedPreferences settings = getBaseContext().getSharedPreferences("LoadNewPatch", 0);
  23.  
    SharedPreferences.Editor editor = settings.edit();
  24.  
    editor.putLong("LoadNewPatchTime", now);
  25.  
    editor.commit();
  26.  
    Looper.prepare();
  27.  
    Toast.makeText(getBaseContext(),"检测到版本更新完成,重启后生效",Toast.LENGTH_SHORT).show();
  28.  
    Looper.loop();
  29.  
    Log.i(TAG, "sophix补丁预加载成功. 重启后生效.本次加载时间为" now);
  30.  
    } else if (code == PatchStatus.CODE_REQ_NOUPDATE || code == PatchStatus.CODE_REQ_NOTNEWEST
  31.  
    || code == PatchStatus.CODE_DOWNLOAD_BROKEN || code == PatchStatus.CODE_UNZIP_FAIL
  32.  
    || code == PatchStatus.CODE_REQ_UNAVAIABLE || code == PatchStatus.CODE_REQ_SYSTEMERR) {
  33.  
    long now = System.currentTimeMillis();
  34.  
    SharedPreferences settings = getBaseContext().getSharedPreferences("LoadNewPatch", 0);
  35.  
    SharedPreferences.Editor editor = settings.edit();
  36.  
    editor.putLong("LoadNewPatchTime", now);
  37.  
    editor.commit();
  38.  
    Log.i(TAG, "sophix查询结束.本次加载时间为" now);
  39.  
    }
  40.  
    }
学新通

        TIPS         

        监听中使用Toast需要在前后加上Looper.prepare();Looper.loop();        

        (否则会报错 Can't toast on a thread that has not called Looper.prepare())        

        发布时设置setEnableDebug(false)        

        LoadNewPatchTime是我写入缓存中的上一次更新时间        

        设置时间的状态码可能有遗漏        

        flutter使用so需要复制到项目路径下,与原生位置不同        

        !!!更新包与基准包需要有原生代码的区别才能生效!!!        

  •  MainActivity

        queryAndLoadNewPatch方法是计费接口,我在main中设置定时调用(newPatch中)

  1.  
    class MainActivity: FlutterActivity() {
  2.  
    private var mHandler: Handler = Handler()
  3.  
    private var mTimeCounterRunnable: Runnable = Runnable { }
  4.  
     
  5.  
    override fun onCreate(savedInstanceState: Bundle?) {
  6.  
    super.onCreate(savedInstanceState)
  7.  
    GeneratedPluginRegistrant.registerWith(this)
  8.  
    // 全局变量 定时任务
  9.  
    mHandler = Handler(Looper.getMainLooper())
  10.  
    mTimeCounterRunnable = object : Runnable {
  11.  
    override fun run() { //在此添加需轮询的接口
  12.  
    FlutterPatch.findNewPatch(baseContext) //执行的任务
  13.  
    mHandler.postDelayed(this, (10 * 60 * 1000).toLong())
  14.  
    }
  15.  
    }
  16.  
    mHandler.post(mTimeCounterRunnable)
  17.  
    }
  18.  
     
  19.  
    override fun onDestroy() {
  20.  
    //关闭定时任务
  21.  
    mHandler.removeCallbacks(mTimeCounterRunnable)
  22.  
    super.onDestroy()
  23.  
    }
  24.  
    }
学新通

        另一种定时任务方法

  1.  
    class MainActivity: FlutterActivity() {
  2.  
    private lateinit var mHandler: Handler
  3.  
    private lateinit var thread:Thread
  4.  
     
  5.  
    override fun onCreate(savedInstanceState: Bundle?) {
  6.  
    super.onCreate(savedInstanceState)
  7.  
    GeneratedPluginRegistrant.registerWith(this)
  8.  
    // 全局变量 定时任务
  9.  
    mHandler = Handler(Looper.getMainLooper())
  10.  
    thread = object:Thread(){
  11.  
    override fun run() {
  12.  
    while (!this.isInterrupted){
  13.  
    try{
  14.  
    //执行的任务
  15.  
    mHandler.post { FlutterPatch.findNewPatch(baseContext)}
  16.  
    sleep((10 * 60 * 1000).toLong())
  17.  
    }catch (e:InterruptedException){
  18.  
    interrupt();
  19.  
    e.printStackTrace();
  20.  
    }
  21.  
    }
  22.  
    }
  23.  
    }
  24.  
    thread.start();
  25.  
    }
  26.  
     
  27.  
    override fun onDestroy() {
  28.  
    //关闭定时任务
  29.  
    mHandler.removeCallbacks(thread)
  30.  
    thread.interrupt();
  31.  
    super.onDestroy()
  32.  
    }
  33.  
    }
学新通
  • FlutterPatch.java
  1.  
    public class FlutterPatch {
  2.  
    private static final String TAG = "FlutterPatch";
  3.  
    private static String libPathFromSophix = "";
  4.  
     
  5.  
    /**
  6.  
    * 更新补丁
  7.  
    */
  8.  
    public static void findNewPatch(Context context) {
  9.  
    Log.i(TAG, "--------------------版本1.0.0-0------------------");
  10.  
    Log.i(TAG, "--------------------补丁版本-0------------------");
  11.  
    SharedPreferences settings = context.getSharedPreferences("LoadNewPatch", 0);
  12.  
    long now = System.currentTimeMillis();
  13.  
    Log.i(TAG, "--当前时间-" now);
  14.  
    long last = settings.getLong("LoadNewPatchTime", 0L);
  15.  
    Log.i(TAG, "--上一次更新时间-" last);
  16.  
    if(last == 0L || timeCompare(last,now,80)) {
  17.  
    // 第一次进入app或者距离上次拉取时间大于80分钟 拉取更新
  18.  
    SophixManager.getInstance().queryAndLoadNewPatch();
  19.  
    }
  20.  
    }
  21.  
     
  22.  
    ///相差分钟
  23.  
    private static boolean timeCompare(long date1, long date2, int basicDiff) {
  24.  
    long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
  25.  
    long nh = 1000 * 60 * 60;// 一小时的毫秒数
  26.  
    long nm = 1000 * 60;// 一分钟的毫秒数
  27.  
    long ns = 1000;// 一秒钟的毫秒数
  28.  
    //格式化时间
  29.  
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  30.  
    String startDate = simpleDateFormat.format(date1);//开始的时间戳
  31.  
    String endDate = simpleDateFormat.format(date2);//结束的时间戳
  32.  
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  33.  
    try {
  34.  
    Date beginTime = df.parse(startDate);
  35.  
    Date endTime = df.parse(endDate);
  36.  
    assert endTime != null;
  37.  
    assert beginTime != null;
  38.  
    long diff = endTime.getTime() - beginTime.getTime();
  39.  
    long mins = diff / nm;
  40.  
    return mins >= basicDiff;
  41.  
    } catch (ParseException e) {
  42.  
    e.printStackTrace();
  43.  
    }
  44.  
    return false;
  45.  
    }
  46.  
     
  47.  
    public static String findLibraryFromSophix(Context context, String relativePath, String libName) throws UnsatisfiedLinkError {
  48.  
    File file = new File(relativePath "/libs/libapp.so");
  49.  
    if (file.exists() && !file.isDirectory()) {
  50.  
    libName = file.getAbsolutePath();
  51.  
    Log.i(TAG, "so路径 is " libName);
  52.  
    } else {
  53.  
    Log.i(TAG, "so路径 is not exist");
  54.  
    }
  55.  
    return libName;
  56.  
    }
  57.  
     
  58.  
    public static void copyFileByPath(String fromPath, String toPath) {
  59.  
    File fromFile = new File(fromPath);
  60.  
    File toFile = new File(toPath);
  61.  
    if (!fromFile.exists()) {
  62.  
    return;
  63.  
    }
  64.  
    if (!fromFile.isFile()) {
  65.  
    return;
  66.  
    }
  67.  
    if (!fromFile.canRead()) {
  68.  
    return;
  69.  
    }
  70.  
    if (!toFile.getParentFile().exists()) {
  71.  
    toFile.getParentFile().mkdirs();
  72.  
    }
  73.  
    if (toFile.exists()) {
  74.  
    toFile.delete();
  75.  
    }
  76.  
    try {
  77.  
    FileInputStream fosfrom = new FileInputStream(fromFile);
  78.  
    FileOutputStream fosto = new FileOutputStream(toFile);
  79.  
    byte[] bt = new byte[1024];
  80.  
    int c;
  81.  
    while((c=fosfrom.read(bt)) > 0){
  82.  
    fosto.write(bt,0,c);
  83.  
    }
  84.  
    //关闭输入、输出流
  85.  
    fosfrom.close();
  86.  
    fosto.close();
  87.  
    } catch (FileNotFoundException e) {
  88.  
    e.printStackTrace();
  89.  
     
  90.  
    } catch (IOException e) {
  91.  
    e.printStackTrace();
  92.  
    }
  93.  
    }
  94.  
     
  95.  
    }
学新通

        findNewPatch中的版本日志是我用来修改基准包与补丁包原生代码差异的地方,毕竟flutter项目基本不会改变原生代码, 所以需要手动修改一下        


 学新通

打补丁包时需要去掉检查初始化勾选 


参考链接

混栈开发之Android端Flutter热更新-Sophix篇学新通https://juejin.cn/post/6859019562931716110带你不到80行代码搞定Flutter热更新学新通https://cloud.tencent.com/developer/article/1531498Android 开发 复制文件学新通https://blog.csdn.net/afufufufu/article/details/125561452安卓TV插件化9.0内联崩溃原因及解决方案学新通https://blog.csdn.net/weixin_38753262/article/details/126397331

相关阅读 

阿里推出业界首个非侵入式热修复方案Sophix,颠覆移动端传统更新流程!

热修复技术可谓是百花齐放

阿里云SOPHIX 3.0版本热更新快速入门

Android疑难杂症——因内联优化导致9.0机型Native Crash

[Flutter] 如何替换so文件来动态替换Flutter代码

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhgkeg
系列文章
更多 icon
同类精品
更多 icon
继续加载