美文网首页
Flutter-已有iOS工程中加入Flutter之远程Pod产

Flutter-已有iOS工程中加入Flutter之远程Pod产

作者: yuyangkk | 来源:发表于2020-04-26 17:24 被阅读0次

    之前写了一篇基于本地环境集成flutter的文章Flutter-已有iOS工程中加入Flutter之module方式集成,接着上篇,我研究了下不依赖flutter开发环境的方式集成,推送到远程pod仓库,以Framework形式在cocoapods中集成。

    开始

    1. 创建flutter_module:flutter create -t module flutter_module

    KKdeMacBook-Pro:remotepod kaye$ flutter create -t module flutter_module
    Creating project flutter_module... androidx: true
    flutter_module/test/widget_test.dart (created)
    flutter_module/flutter_module.iml (created)
    flutter_module/.gitignore (created)
    flutter_module/.metadata (created)
    flutter_module/pubspec.yaml (created)
    flutter_module/README.md (created)
    flutter_module/lib/main.dart (created)
    flutter_module/flutter_module_android.iml (created)
    flutter_module/.idea/libraries/Flutter_for_Android.xml (created)
    flutter_module/.idea/libraries/Dart_SDK.xml (created)
    flutter_module/.idea/modules.xml (created)
    flutter_module/.idea/workspace.xml (created)
    Running "flutter pub get" in flutter_module...                      1.7s
    Wrote 12 files.
    
    All done!
    Your module code is in flutter_module/lib/main.dart.
    

    添加依赖库,比如:

    dependencies:
      flutter:
        sdk: flutter
    
      # The following adds the Cupertino Icons font to your application.
      # Use with the CupertinoIcons class for iOS style icons.
      cupertino_icons: ^0.1.2
      shared_preferences: ^0.5.6
    

    运行flutter pub get安装依赖库

    KKdeMacBook-Pro:flutter_module kaye$ flutter pub get
    Running "flutter pub get" in flutter_module...                      0.4s
    KKdeMacBook-Pro:flutter_module kaye$ 
    

    build获取产物

    KKdeMacBook-Pro:flutter_module kaye$ flutter build ios --debug --no-codesign
    Warning: Building for device with codesigning disabled. You will have to manually codesign
    before deploying to device.
    Building com.example.flutterModule for device (ios)...
    Running Xcode build...                                                  
                                                       
     ├─Assembling Flutter resources...                           6.8s
     └─Compiling, linking and signing...                         4.5s
    Xcode build done.                                           17.6s
    Built /Users/kaye/Desktop/remotepod/flutter_module/build/ios/iphoneos/Runner.app.
    

    产物路径:


    基础库路径.png 依赖插件路径.png

    我们先将各个产品,手动copy出来,放到一个文件夹ios_frameworks中备用。
    note:这里我们先不用脚本,手动获取,验证一下pod能不能成功

    2. 创建Pod:pod lib create KKFlutterPod

    KKdeMacBook-Pro:remotepod kaye$ pod lib create KKFlutterPod
    Cloning `https://github.com/CocoaPods/pod-template.git` into `KKFlutterPod`.
    Configuring KKFlutterPod template.
    
    ------------------------------
    
    To get you started we need to ask a few questions, this should only take a minute.
    
    If this is your first time we recommend running through with the guide: 
     - https://guides.cocoapods.org/making/using-pod-lib-create.html
     ( hold cmd and double click links to open in a browser. )
    
    
    What platform do you want to use?? [ iOS / macOS ]
     > ios
    
    What language do you want to use?? [ Swift / ObjC ]
     > objc
    
    Would you like to include a demo application with your library? [ Yes / No ]
     > no
    
    Which testing frameworks will you use? [ Specta / Kiwi / None ]
     > none
    
    Would you like to do view based testing? [ Yes / No ]
     > no
    
    What is your class prefix?
     > KK
    
    Running pod install on your new library.
    
    Analyzing dependencies
    Fetching podspec for `KKFlutterPod` from `../`
    Downloading dependencies
    Installing KKFlutterPod (0.1.0)
    Generating Pods project
    Integrating client project
    
    [!] Please close any current Xcode sessions and use `KKFlutterPod.xcworkspace` for this project from now on.
    Sending stats
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
    
    [!] Automatically assigning platform `ios` with version `9.3` on target `KKFlutterPod_Tests` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
    
     Ace! you're ready to go!
     We will start you off by opening your project in Xcode
      open 'KKFlutterPod/Example/KKFlutterPod.xcworkspace'
    
    To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
    To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
    KKdeMacBook-Pro:remotepod kaye$ 
    

    将上一步的ios_frameworks文件夹移动到Pod仓库中,目录结构如下:

    Pod库中Framework目录.png

    3. 创建远程Pod对应的Git仓库

    3.1 创建仓库

    这里自己测试使用的是GitHub,创建过程就不描述了。

    3.2 关联仓库

    tip:或者你也可以先把远程的Pod仓库创建好,clone下来,然后把创建的KKFlutterPod里面除了.git之外的所有文件拷贝到你clone的git仓库中,从而省去关联的关系

    KKdeMacBook-Pro:flutter_module kaye$ cd ../KKFlutterPod/
    KKdeMacBook-Pro:KKFlutterPod kaye$ git remote add origin https://github.com/yuyangkk/flutterpod.git
    

    修改KKFlutterPod.podsepc文件,在其末尾添加如下:

    s.static_framework = true
    p = Dir::open("ios_frameworks")
    arr = Array.new
    arr.push('ios_frameworks/*.framework')
    s.ios.vendored_frameworks = arr
    
    podsepc文件.png
    3.3 提交Pod到远程
    git add .
    git commit -m "初始化"
    git pull origin master
    git push -u origin master
    
    git tag -m "v0.1.0" -a 0.1.0
    git push --tags
    
    3.4 使用
    pod 'KKFlutterPod', :git => 'https://github.com/yuyangkk/KKFlutterPod.git'
    

    4. 脚本化

    每次我们修改了flutter代码,都要重新打包出来Framework,然后打上tag推送到远程,这个过程虽然简单,但是重复性很强,所以考虑写成脚本实现,以免有漏掉某些步骤!
    ps:脚本有点不太规范,是一边看语法,一遍写的,所以,有问题还请指出来

    4.1. 编译Framework并导出到指定位置
    #cd ~/Desktop/remotepod/flutter_module
    #获取当前脚本目录
    projectURL=$(cd $(dirname $0); pwd)
    
    #echo $projectURL
    
    cd ${projectURL}
    
    #out是指将编译号的文件copy到指定的位置的路径
    if [ -z $out ]; then
        out='../KKFlutterPod/KKFlutterPod/ios_frameworks'
    fi
    
    echo "准备输出所有文件到目录: $out"
    
    echo "清除所有已编译文件"
    #find . -d -name build | xargs rm -rf
    flutter clean
    rm -rf $out
    rm -rf build
    
    flutter packages get
    
    echo "编译flutter"
    flutter build ios --debug --no-codesign
    #如果是release包,将debug改为release
    #flutter build ios --release --no-codesign
    echo "编译flutter完成"
    mkdir $out
    
    cp -r build/ios/Debug-iphoneos/*/*.framework $out
    cp -r .ios/Flutter/App.framework $out
    cp -r .ios/Flutter/engine/Flutter.framework $out
    
    #本地Pod库中提交代码,并修改version,打上标签,标签和version保持一致,自增
    #上传到pod,请执行脚本push_pod_spec.sh
    
    
    4.2. 将导出的Framework上传到远程pod仓库
    projectURL=$(cd $(dirname $0); pwd)
    
    #echo $projectURL
    
    cd ${projectURL}
    
    cd ../KKFlutterPod
    
    echo "准备更新本地仓库"
    git pull
    
    if [ $? -eq 0 ]; then
        echo "拉取远程代码成功"
    else
        echo "拉取远程代码失败"
        exit 1
    fi
    
    cd ${projectURL}
    
    #修改KKFlutterPod.podspec中s.version          = '0.1.0'
    
    # 读取KKFlutterPod.podspec的第11行
    # a=`cat KKFlutterPod.podspec |head -ne12`
    # echo $a
    a=`sed -n 11p KKFlutterPod.podspec `
    # echo $a #s.version = '0.1.4'
    # echo "删除最右边.开始向及其左边的数据"
    vs=${a##*.}
    # echo $vs
    oldNum=${vs%%\'*}
    # echo '当前版本:'$oldNum
    newNum=$((oldNum+1))
    # echo '新版本:'$newNum
    
    # echo "替换版本号"
    v=0.1.$newNum
    version="  s.version          = '$v'"
    echo $version
    sed -i '' "s/$a/$version/" KKFlutterPod.podspec
    
    # echo "add 并且commit"
    
    # git status
    git rm --cached
    git add .
    git commit -m "修改版本$v"
    
    # echo "添加tag"
    git tag -a $v -m "$v"
    
    # echo "连同tag推送到远程"
    git push
    git push --tags
    

    自此,差不多应该可以跑起来,run一下试试,如果遇见build错误,请查看一下Xcode的bitcode是否打开了,如果打开请关闭。

    5. 问题

    5.1 undefined symbol:OBJC_CLASS$_GeneratedPluginRegistrant
    flutter插件编译错误.png

    这个问题,猜测是因为导出的包是不支持模拟器的,因为通过flutter开发环境导出的包,是可以支持模拟器的,所以猜测是我写的脚本有问题,等待后续优化下,目前是只支持真机。

    5.2 有关Framework包的大小

    通过flutter build ios-framework --output=导出包路径,获取到debug、profile、release三种情况下的包,其中包含的Flutter.framework的包大小分别是95MB、66.8MB、369.8MB,如果用release的包,推送到远程pod库的时候,会超过git的100MB限制,可能会失败,所以我们暂时只用了debug模式下的包,推送到远程。
    当然也有可能是我使用的命令有缺陷,如果有什么可以解决release环境下,包过大的问题,请务必留言给我,感谢~~~

    错误待续。。。

    相关文章

      网友评论

          本文标题:Flutter-已有iOS工程中加入Flutter之远程Pod产

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