Flutter混合开发和Android动态更新实践

Flutter混合开发和Android动态更新实践

感谢闲鱼和csdn的文章给的思路:

闲鱼flutter混合工程持续集成的最佳实践

深入理解 Flutter 的编译原理与优化

Flutter混合开发和动态更新的探索历程 Android版

本篇是实践性文章包含两部分

  1. 将Flutter工程编译后的文件集成到Android项目
  2. 将Flutter代码热更新

不涉及源码和理论,想要了解的话可以阅读上面的文章

将Flutter工程编译后的文件集成到Android项目

​ 因为Flutter无法完全替代原生绝大部分项目都是Flutter和Native混合开发的模式,所以存在一部分同学只做Native开发,并不熟悉Flutter技术。如果直接采用Flutter工程来开发,那这部分原生开发同学也需要配置Flutter环境,学习Flutter。

​ 由于上述原因,所以采用flutter项目和原生项目相互独立开发,将flutter项目编译后的文件以module的形式集成到Android项目中。

原理:Android项目依赖flutter的文件

  1. Flutter库

    主要是flutter.jar,文件位于Flutter SDK目录下flutter/bin/cache/artifacts/engine

  2. Flutter工程编译文件

    isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr、flutter_assets下所有文件

    位于Flutter工程build/app/intermediates/flutter/release/下

  3. Flutter Plugin编译文件(暂时没用到,待补充)

集成步骤

  1. 在原生项目中新建一个module例如fluttermodule

  2. 将flutter.jar放入libs文件夹

  3. 将isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr和flutter_assets下所有文件放入assets/flutter_assets/下

    为了方便可以在flutter项目中用gradle脚本实现,例如

    task copyAndroidFlutterModule << {
        println "project.rootDir = ${project.rootDir}/"
    
        println "buildDir = ${this.buildDir}/"
        def flutterOutputDir = "${this.buildDir}/flutter_android_output/flutter_module/"
        def flutterPluginFile = new File(flutterOutputDir)
        if (!flutterPluginFile.exists()) {
            flutterPluginFile.mkdirs()
        } else {
            flutterPluginFile.deleteDir()
        }
        def flutterReleaseBuildDir = "${this.buildDir}/app/intermediates/flutter/release/"
        println "flutterReleaseBuildDir=$flutterReleaseBuildDir"
        //复制snapshot_data和snapshot_instr
        project.copy {
            from flutterReleaseBuildDir
            include "*snapshot_data"
            include "*snapshot_instr"
            into flutterOutputDir
        }
        //复制flutter_assets
        project.copy {
            from "${flutterReleaseBuildDir}flutter_assets/"
            into flutterOutputDir
        }
    }
    
  4. 将application指定为FlutterApplication或者自定义类集成FlutterApplication

    
      
    
  5. 新建Activity继承FlutterActivity

    package com.gmail.jackxuechen.fluttermodel;
    
    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    
    public class FlutterRootActivity extends FlutterActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
      }
    }
    
  6. 集成成功

Android动态更新

原理:替换/data/data/包名/app_flutter/flutter_assets/

下的isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr即可实现flutter项目的更新

为了方便可以写个gradle脚本将flutter工程编译的isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr文件放到一个单独文件夹

task copyAndroidFlutterAssets << {
    println "project.rootDir = ${project.rootDir}/"
    println "buildDir = ${this.buildDir}/"
    def flutterOutputDir = "${this.buildDir}/flutter_android_output/flutter_assets/"
    def flutterPluginFile = new File(flutterOutputDir)
    if (!flutterPluginFile.exists()) {
        flutterPluginFile.mkdirs()
    } else {
        flutterPluginFile.deleteDir()
    }
    def flutterReleaseBuildDir = "${this.buildDir}/app/intermediates/flutter/release/"
    println "flutterReleaseBuildDir=$flutterReleaseBuildDir"
    //复制snapshot_data和snapshot_instr
    project.copy {
        from flutterReleaseBuildDir
        include "*snapshot_data"
        include "*snapshot_instr"
        into flutterOutputDir
    }
}

注意事项

  1. vm_snapshot_data、vm_snapshot_instr和flutter sdk版本有关,生成更新文件时应保持和集成到原生项目中的flutter sdk版本一致

  2. 因为isolate_snapshot_data、isolate_snapshot_instr、vm_snapshot_data、vm_snapshot_instr可能是在FlutterApplication中处理(需要看源码目前还没研究到)的。如果直接替换相关文件而没有冷启动,可能会导致崩溃,不过可以通过升级逻辑实现冷启更新,类型腾讯tinker。

  3. 如果flutter项目中添加新的依赖原生的插件,则无法通过替换flutter编译后的文件升级,强制升级会崩溃

你可能感兴趣的:(Flutter混合开发和Android动态更新实践)