正文
flutter混编kotlin flutter hello world
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
Flutter iOS混编 解决flutter在iOS14+ debug模式上无法运行的问题
iOS项目引入Flutter后,安装Debug包,打开Flutter界面会出现"In iOS 14+, debug mode Flutter apps can only be launched from Flutter tooling, IDEs with Flutter plugins or from Xcode"问题。
解决办法如下:
用 [Xcode] 打开工程项目,在 Build Settings 的最下方找到 User-Defined,点击 + 按钮,添加一个键为 FLUTTER_BUILD_MODE ,debug设置profile模式,release设置release 模式。
创建flutter项目语言选择
在升级flutter1.9+之后,flutter默认的iOS项目为swift,Android的默认项目为kotlin
在创建时选择语言
Flutter工程化之iOS混编集成
在flutter官网上推荐了iOS项目中两种混编方式:
笔者在采用两种集成方式的过程中,因为iOS项目结构设计导致这两种简单的集成方式都有些麻烦,所以在实践中更改和优化了集成方式,使之在笔者的项目中能够更加简单和快速的集成。
问题:在不更改flutter tool中相关脚本的前提下,添加的Script Phase中的脚本相对路径错误,如果只是开发,手动更改下路径就可以了,但是在考虑到CI中不能每次在pod install之后都去更改,所以在开发调试中采用该集成方式,结合flutter attach的方式去调试。
通过编译相关的 xcframework + Cocoapods私有库的集成方式在CI中集成,这样QA的CI不需要配置flutter的相关依赖
根据flutter编译工具的提示: 上面的编译命令是打包flutter工程项目和插件的产物,在实际开发过程中可以发现是否引入了依赖Native的插件会导致贬义编译产物的不同。
根据上面的对比:
第一部分:基础的 Flutter Engine + Flutter App 编译后的产物 Flutter.xcframwork -- Flutter引擎的包 App.xcframework -- 工程项目对应的AOT的编译产物 第二部分:三方插件的注册中心 FlutterPluginRegistrant.xcframework -- 第三方插件的注册中心,其实是Native + iOS通信的集合 第三部分:依赖iOS Native的原生 FMDB . xcframwork path_provider_ios.xcframework sqflite.xcframework -- cached_network_image依赖的原生实现
根据上面的编译产物可以知道Flutter和App是编译后必有的包,后面的两个部分完全是服务于三方插件的,到这可以解答第二个问题:笔者App的混编过程中混编插件失效是因为笔者在NativeApp中重写了Flutter的容器,使用了FlutterEngineGroup动态创建多引擎去对应进入不同的功能模块,混合插件是因为重写过程中没有通过GeneratedPluginRegistrant注册插件,所以需要在Native的Flutter容器中注册插件,使之生效。
在这为什么使用commit的hash作为flutter-libs的依赖,因为pod install的时候会有缓存,除了版本好,commit hash也能保证每次CI编译通过 pod install 来更新flutter-libs依赖产物
完成!!!
flutter与原生混编(iOS)
一、创建项目及配置
1,创建ios项目,同时在同一个根目录下创建flutter项目
flutter项目使用命令flutter create -t module xxxflutter项目名
如图
2,将flutter项目以pod的形式加入ios项目
2.1,如果项目之前没有用过pod,则pod init创建podfile
如果使用过,直接进行步骤2.2。
2.2,podfile中添加
flutter_application_path是flutter项目的路径
2.3,target中加入(一次性将flutter的编译产物由此依赖进入iOS项目中,可跳过步骤3)
3,配置脚本(已完成步骤2.3可进行步骤4)
打开ios项目,在Build Phases中左上角添加Run Script
4,设置bitcode
Flutter 目前还不支持 BitCode,需要设置为No
二、混编使以后添加的每个模块都以package形式,在module中pubspec.yaml文件中引用(基于内存消耗考虑)
1,页面跳转
原生处理:
flutter在iOS中的使用,主要是以FlutterViewController为载体,并在其内部采用FlutterEngine对视图进行渲染。
导入头文件:
flutter处理:
在module项目的main里
定义channel,名字与原生保持一致
2,数据传递
二者之间的数据传递iOS使用FlutterBasicMessageChannel类,flutter使用BasicMessageChannel类,用法与MethodChannel类似
原生处理:
接收数据:
发送数据:
flutter处理:
问题:
网上搜的方法,在podfile最前边加eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)句话
执行pod install失败
于是又搜了下别的文章,添加的第二句话为load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb'),试了下成功了
不学Flutter,我们来用Kotlin KMM跨平台吧
官方推荐有三种不同的方法来组织团队在多平台库上的工作方式:
我觉得 1 比较适合,当然大公司 3 也适合,毕竟跨平台的共享模块是 kotlin 语言开发的。
在我们的 shared 里面,当然这个名字是我自己起的,androidApp 和 iosApp 也是可以自己起。我们看看新建一个 KMM 工程是什么样子。
Finish 就可以看到我们上述的文件界面了。
感觉还是不错的,而且和 flutter 和 RN 走的路线也不太一样。KMM 的当前的方式更多的是可以将一些逻辑层统一,比如 MVP 中的 M和 P 层,V 层还是可以让各端实现,当然如果强行在 KMM模块实现也是可以的。
但是现在 KMM 还不够成熟,也存在一些未知问题,不过我决定 开始大胆在项目中使用 。
2022-04-22iOS项目混编flutter,iOS真机debug模式下打不开flutter页面
最近项目中要集成flutter来进行混编,但是在集成后,突然遇到一个很神奇的问题,在debug模式下,用数据线连接真机打包可以打开flutter页面,但是一旦拔掉数据线,再打开flutter页面就不行了,开始以为是因为flutterSDK的原因,但是一查资料才发现,原来是因为debug模式下flutter实现了热重载,默认的编译方式是JIV,但是iOS14+之后的系统限制了JIV这种编译方式,所以连接Xcode重新run一个release包就可以了,因为flutter在release模式下的编译方式是AOT,iOS14+的系统是支持这种编译方式的,具体解决方案如下图
再运行就可以了。
当然还有另外一种解决方案,就是修改flutter的编译配置,强制设为release
关于flutter混编kotlin和flutter hello world的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。