Flutter混合工程自动化

374 阅读2分钟

1、常规iOS调用Flutter

1.1、iOS配置Podfile中Flutter环境

image.png

  • 需要本地电脑配置Flutter环境

2、生成flutter.framework

  • 不需要iOS端配置Flutter环境
  1. cd到flutter项目路径

  2. 生成framework: % flutter build ios-framework --output=../firstflutter_app(要生成的文件名) image.png

  3. 生成DebugProfileRelease三个文件夹,Debug内容最丰富、Release最精简高效、Profile介于两者之间,其中会有App.xcframeworkFlutter.xcframework用来给iOS开发者 image.png

  4. iOS端将获得的framework嵌入到工程 image.png

3、CocoaPods混合工程

  • Flutter环境不打包成framework,而是变成pod库
  1. cd到flutter项目路径

  2. 生成CocoaPods的framework: % flutter build ios-framework --cocoapods --output=../firstflutter_app(要生成的文件名)

    image.png

    image.png

  3. 将生成的Debug或者Release文件夹拷贝到iOS项目目录下

  4. 终端cd进去执行% pod init生成Podfile文件 image.png

    • 如果遇到报错 pod init Multiple Xcode projects found, please specify one,使用pod init name.xcodeproj来解决
  5. 修改Podfile文件 image.png

  6. 装载环境:% pod install

  7. 将Flutter的framework加载进来 image.png

4、混合工程自动化

  • 自动化需要Git仓库免密登录,以前是SSH KeyGPG Key,现在需要个人令牌Personal access tokens,然后通过仓库将Flutter代码打包成framework给iOS项目使用
  1. 创建Git仓库

    • 演示起名"FlutterCI"
  2. 创建个人令牌 image.png

    • 生成的令牌 image.png
    • 将产生的令牌保存在环境变量中,当做账号使用
  3. cd到包含iOS项目和Flutter项目的文件夹路径,执行Git仓库中的命令:(需要科学🪜)

    • 中间会要求输入账号密码,将令牌输入
    echo "# FlutterCI" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git branch -M main
    git remote add origin https://github.com/Git用户名/FlutterCI.git
    git push -u origin main
    
  4. 仓库创建自动化动作 image.png

  5. 编写脚本内容

  • 仓库实现将Flutter项目以CocoaPods方式生成framework(设置以"push"命令为触发条件)
    name: FlutterCI #CI名称
    on: [push] #触发条件push操作!
    
    jobs:
      check:
        name: Test on ${{ matrix.os }}
        #运行在哪个平台这里是MacOS平台
        runs-on: macos-latest
    
        steps:
          - uses: actions/checkout@v1
          #三方flutter的Action,它可以在服务器配置一个Flutter环境
          - uses: subosito/flutter-action@v1
            with:
              flutter-version: '2.5.3'
              channel: 'stable'
          #想让我们CI做的事情!
          - run: cd flutter项目文件夹名 && flutter build ios-framework --cocoapods --output=../iOS项目文件夹名/Flutter 
          - run: |
             git add .
             git commit -m 'update flutter framework'
    
          - name: Push changes
            uses: ad-m/github-push-action@master
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
    
    image.png

到这里一个简单的CI就完成了,当Native或Flutter工程师push时,就会自动执行脚本更新flutter的framework