Flutter:无法创建插件包

时间:2019-04-19 15:21:39

标签: flutter

我正在尝试创建一个插件包,以便在Android(使用Kotlin)和iOS(使用Swift)中访问某些特定的硬件API。为此,我试图按照此处的说明进行操作:

https://flutter.dev/docs/development/packages-and-plugins/developing-packages

我现在只在解决问题的Swift方面,所以到目前为止,已完成以下工作:

1)转到其他Dart软件包所在的目录(非Flutter软件包,可以正常运行)并运行:

flutter create --org com.example --template=plugin my_plugin

这创建了文档中所述的全部内容,并且未报告任何错误。

2)然后,如前一个链接所述,我转到my_plugin/example并运行flutter build ios --no-codesign,输出如下(显然还可以吗?):

 Warning: Building for device with codesigning disabled. You will have to manually codesign before deploying to device.
 Building com.example.myPluginExample for device (ios-release)...
 Running pod install...                                              6.5s
 ├─Building Dart code...                                     2.9s
 ├─Generating dSYM file...                                   1.0s
 ├─Stripping debug symbols...                                0.6s
 ├─Assembling Flutter resources...                           1.0s
 └─Compiling, linking and signing...                         9.0s
 Xcode build done.                                           19.8s

3)因此,现在使用刚刚创建的插件,尝试将其添加到主项目中,并因此在其pubspec.yaml文件中插入以下几行(该插件位于../pkgs关于我正在使用的Flutter应用程序:

dependencies:
  flutter:
    sdk: flutter
  my_plugin:
    path: ../pkgs/my_plugin

4)现在只是尝试在物理设备和模拟器中运行Flutter应用程序,并且始终显示以下错误:

Launching lib/main.dart on iPhone Xʀ in debug mode...
CocoaPods' output:
↳
      Preparing
    Analyzing dependencies
    Inspecting targets to integrate
      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)
    Finding Podfile changes
      A my_plugin
      - Flutter
    Fetching external sources
    -> Fetching podspec for `Flutter` from `.symlinks/flutter/ios`
    -> Fetching podspec for `my_plugin` from `.symlinks/plugins/my_plugin/ios`
    Resolving dependencies of `Podfile`
    Comparing resolved specification to the sandbox manifest
      A Flutter
      A my_plugin
    Downloading dependencies
    -> Installing Flutter (1.0.0)
    -> Installing my_plugin (0.0.1)
      - Running pre install hooks
    [!] Unable to determine Swift version for the following pods:
    - `my_plugin` does not specify a Swift version and none of the targets (`Runner`) integrating it have the `SWIFT_VERSION` attribute set. Please contact the author or set the `SWIFT_VERSION` attribute in at least one of the targets that integrate this pod.
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:115:in `verify_swift_pods_swift_version'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/installer/xcode/target_validator.rb:37:in `validate!'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:459:in `validate_targets'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/installer.rb:138:in `install!'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/command/install.rb:48:in `run'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/claide-1.0.2/lib/claide/command.rb:334:in `run'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/lib/cocoapods/command.rb:52:in `run'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/gems/cocoapods-1.6.1/bin/pod:55:in `<top (required)>'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/bin/pod:22:in `load'
    /usr/local/Cellar/cocoapods/1.6.1/libexec/bin/pod:22:in `<main>'
Error output from CocoaPods:
↳
        WARNING: CocoaPods requires your terminal to be using UTF-8 encoding.
        Consider adding the following to ~/.profile:
        export LANG=en_US.UTF-8

Error running pod install
Error launching application on iPhone Xʀ.
Exited (sigterm)

5),我尝试通过将以下行添加到整个目录结构(属于该应用程序和属于新插件“ example”的文件-这两个文件都是Flutter工具自动创建的,并且直到今天都没有收到任何编辑-):

Podfile

(该想法取自https://stackoverflow.com/a/52194702/6348097,是的:我尝试了至少一个不同的版本号)。错误仍然相同。

生成的插件的Podfile是:

config.build_settings['SWIFT_VERSION'] = '4.1'

我在Flutter应用程序的Podfile中添加了相同的内容。另外,最初对指定iOS平台的第一行进行了注释(并且正在生成警告),因此我也未从两个podfile中注释该行。

这是platform :ios, '9.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' project 'Runner', { 'Debug' => :debug, 'Profile' => :release, 'Release' => :release, } def parse_KV_file(file, separator='=') file_abs_path = File.expand_path(file) if !File.exists? file_abs_path return []; end pods_ary = [] skip_line_start_symbols = ["#", "/"] File.foreach(file_abs_path) { |line| next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } plugin = line.split(pattern=separator) if plugin.length == 2 podname = plugin[0].strip() path = plugin[1].strip() podpath = File.expand_path("#{path}", file_abs_path) pods_ary.push({:name => podname, :path => podpath}); else puts "Invalid plugin specification: #{line}" end } return pods_ary end target 'Runner' do use_frameworks! # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock # referring to absolute paths on developers' machines. system('rm -rf .symlinks') system('mkdir -p .symlinks/plugins') # Flutter Pods generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig') if generated_xcode_build_settings.empty? puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first." end generated_xcode_build_settings.map { |p| if p[:name] == 'FLUTTER_FRAMEWORK_DIR' symlink = File.join('.symlinks', 'flutter') File.symlink(File.dirname(p[:path]), symlink) pod 'Flutter', :path => File.join(symlink, File.basename(p[:path])) end } # Plugin Pods plugin_pods = parse_KV_file('../.flutter-plugins') plugin_pods.map { |p| symlink = File.join('.symlinks', 'plugins', p[:name]) File.symlink(p[:path], symlink) pod p[:name], :path => File.join(symlink, 'ios') } end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '4.1' config.build_settings['ENABLE_BITCODE'] = 'NO' end end end 的输出:

flutter doctor

问题在于如何使这项工作有效。我什至没有开始添加自己的代码,但是我对基本的Flutter的插件创建工具一无所知。另外,我也不知道为什么我的软件包中需要所有肮脏而复杂的Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.14.3 18D109, locale en-ES) [✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3) [✓] iOS toolchain - develop for iOS devices (Xcode 10.2) [✓] Android Studio (version 3.3) [✓] VS Code (version 1.33.1) [✓] Connected device (1 available) • No issues found! 子目录(因此,第二个问题并不重要:可以在工作后将其删除吗?)

编辑:我做了另一个测试:使用VSC的“ Flutter:新建​​项目”命令创建一个空应用程序(该示例创建“ Flutter 101”增量计数器示例)。然后再次按照上述步骤操作,以创建插件程序包,并且在将该程序包包含在新创建的项目中并在iOS模拟器中运行该程序包时,遇到相同的错误。因此,除了添加新插件作为示例应用程序的依赖项之外,我那里绝对没有代码。

2 个答案:

答案 0 :(得分:0)

尝试使用Xcode打开文件夹ios/Runner/Runner.xcworkspac中的IOS项目。然后在该目录中的任何位置创建/添加新的Swift文件。然后Xcode将自动应用所需的更改来支持您的flutter应用程序的快速部分。

另一件事可能是您的PodFile进行了某种修改:尝试通过以下链接将其与Google的主PodFile进行比较:PodFile Master File

答案 1 :(得分:0)

尝试了几项都无济于事之后,我在GitHub线程上找到了此解决方案:

https://github.com/ko2ic/image_downloader/issues/9#issuecomment-471608482

严格遵循这些步骤,终于奏效了。

一些有趣的事情是:

  1. 我还尝试从Android Studio(而不是从命令行或VSC)创建插件。如果没有上一个链接中描述的步骤,它就无法工作,但是这些步骤最终是在该Android Studio创建的插件上完成的(所以我只是不知道这是否适用于VSC创建的插件,我必须说这很可能是的,因为我没有看到命令结果的任何差异)。我之所以提及此问题,是因为此答案表明可以解决此问题:https://stackoverflow.com/a/54919007/6348097

  2. 源文件中有2个Podfile文件:一个用于插件,另一个用于应用程序本身。我修改了两者,并打开了两个XCode(自动生成的所有项目)。

  3. 我还将以下行添加到Podfile:config.build_settings['SWIFT_VERSION'] = '4.2',正好在以下行的唯一出现之前:config.build_settings['ENABLE_BITCODE'] = 'NO'

  4. 在两个Podfile文件中,取消注释platform :ios, '8.0',并将versino更改为9.0。这并不能解决问题,但是在运行pod install

  5. 的某个时候修复了警告