我正在尝试创建一个插件包,以便在 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
文件中,插入以下行(对于我正在使用的 Flutter 应用程序,该插件位于 ../pkgs
中:
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) 我试图通过将以下行添加到我可以在整个目录结构中看到的仅有的 2 个 Podfile
文件(属于应用程序和属于新插件“示例”的应用程序 - 这两个文件都是由 Flutter 的工具自动创建的,直到今天都没有收到任何编辑 -):
config.build_settings['SWIFT_VERSION'] = '4.1'
(这个想法来自 https://stackoverflow.com/a/52194702/6348097,是的:我尝试了至少一个不同的版本号)。错误还是一样。
生成的插件的 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 应用的 Podfile 中添加了相同的内容。此外,指定 iOS 平台的第一行最初是注释的(这会产生警告),所以我还取消了两个 podfile 中的该行的注释。
这是flutter doctor
的输出:
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!
问题只是如何使这项工作。我什至没有开始添加自己的代码,但我坚持使用非常基本的 Flutter 插件创建工具。另外,我不知道为什么我的包中需要所有肮脏而复杂的 example
子目录(所以作为一个并不重要的次要问题:我可以在这工作后将其删除吗?)
编辑:我做了另一个测试:使用 VSC 的“Flutter: New Project”命令创建了一个空应用程序(创建了“Flutter 101”增量计数器示例)。然后再次按照上述步骤创建插件包,并在新创建的项目中包含该包并在 iOS 模拟器中运行它时出现相同的错误。所以那里绝对没有我的代码,除了将新插件添加为示例应用程序的依赖项。
最佳答案
尝试用Xcode打开文件夹ios/Runner/Runner.xcworkspac
中的IOS项目。然后在该目录的任何位置创建/添加一个新的 Swift 文件。然后 Xcode 将自动应用所需的更改以支持您的 Flutter 应用程序的 swift 部分。
另一件事可能是您的 PodFile 以某种方式被修改:尝试通过此链接将其与 Google 的主 PodFile 进行比较:PodFile Master File
关于Flutter:无法创建插件包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764088/