Flutter 颤振:Can';不要创建插件包
我正在尝试创建一个插件包,以便在Android(使用Kotlin)和iOS(使用Swift)中访问一些特定的硬件API。为此,我尝试按照这里的说明进行操作: 我现在只研究问题的快速方面,因此到目前为止所做的工作如下: 1)转到其他Dart软件包所在的目录(它们是非颤振软件包,工作正常),然后运行: 这创建了文档中描述的整个内容,没有报告任何错误 2)然后,如前一个链接中所述,我转到Flutter 颤振:Can';不要创建插件包,flutter,Flutter,我正在尝试创建一个插件包,以便在Android(使用Kotlin)和iOS(使用Swift)中访问一些特定的硬件API。为此,我尝试按照这里的说明进行操作: 我现在只研究问题的快速方面,因此到目前为止所做的工作如下: 1)转到其他Dart软件包所在的目录(它们是非颤振软件包,工作正常),然后运行: 这创建了文档中描述的整个内容,没有报告任何错误 2)然后,如前一个链接中所述,我转到my_plugin/example并运行flatter build ios——无代码设计,输出如下(显然正常):
my_plugin/example
并运行flatter build ios——无代码设计
,输出如下(显然正常):
3)因此,现在刚创建的插件试图将其添加到主项目中,因此,在其pubspec.yaml
文件中插入了以下行(插件位于。/pkgs
中,与我正在使用的颤振应用程序相关:
dependencies:
flutter:
sdk: flutter
my_plugin:
path: ../pkgs/my_plugin
4)现在刚刚尝试在物理设备和模拟器中运行颤振应用程序,始终显示以下错误:
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)
(这个想法来源于,是的:我尝试了至少一个不同的版本号)。错误仍然是一样的
生成的插件的pod文件是:
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
我做了同样的添加到颤振应用程序的播客文件。另外,指定iOS平台的第一行最初是注释的(这会生成一个警告),因此我还从两个pod文件中取消了该行的注释
这是颤振医生的输出:
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!
问题是如何使这项工作发挥作用。我甚至没有开始添加我自己的代码,但我仍然坚持使用非常基本的Flitter插件创建工具。另外,我不知道为什么我需要在我的包中包含所有脏的和复杂的示例子目录(因此,作为第二个问题,这并不重要:我可以在工作完成后删除它吗?)
编辑:我做了另一个测试:使用VSC的“颤振:新项目”命令(创建“颤振101”增量计数器示例)创建了一个空应用程序。然后再次按照上面描述的步骤创建插件包,并在新创建的项目中包含该包并在iOS模拟器中运行时出现相同的错误。因此,除了将新插件作为依赖项添加到示例应用程序之外,我这里绝对没有任何代码。尝试使用Xcode打开文件夹IOS/Runner/Runner.xcworkspac
中的IOS项目。然后在该目录的任意位置创建/添加一个新的Swift文件。然后,Xcode将自动将所需的更改应用到您的Flitter应用程序的swift部分
另一件事可能是您的PodFile被修改了:尝试通过以下链接将其与Google的主PodFile进行比较:在尝试了几种方法后,我在GitHub线程上发现了以下解决方案:
严格遵循这些步骤后,我们终于成功了
有几件有趣的事是:
我还尝试从Android Studio(而不是从命令行或VSC)创建插件。如果没有上一个链接中描述的步骤,它就无法工作,但这些步骤最终是在Android Studio创建的插件上完成的(因此我不知道这是否适用于VSC创建的插件,我必须说很可能是的,因为我没有看到命令结果上的任何差异)。我之所以提到这一点,是因为这个答案表明,这可能会解决问题:
源代码中有两个Podfile文件:一个用于插件,另一个用于应用程序本身。我修改了这两个项目,并打开了这两个XCode(自动生成的调用)项目
我还在pod文件中添加了以下行:config.build\u settings['SWIFT\u VERSION']='4.2'
,就在出现以下行之前:config.build\u settings['ENABLE\u BITCODE']='NO'
在这两个Podfile文件中,取消对平台:ios,'8.0'
的注释,并将版本号更改为9.0。这并没有解决问题,但在运行pod install
你是说在我的插件/example/ios/Runner.xcworkspace中?(my_plugin/example/ios/Runner文件夹中没有XCode工作区)是的,相同的目录,但是用XCode程序打开它,而不是用你正在使用的程序来构建你的Flatter应用程序。我在我之前的评论中在工作区中做了这件事,但它不起作用(完全相同的错误)。以防万一,做完这些之后,我转到我的颤振应用程序并运行“颤振包获取”。另外,runner示例在模拟器上运行(只是一个空白屏幕,但这似乎是一个pod问题,而不是XCode问题),您可以粘贴您的pod文件以查看其中的内容吗?谢谢。我编辑这个问题是为了包含插件的podfile(在Flutter应用程序中还有一个)。
config.build_settings['SWIFT_VERSION'] = '4.1'
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
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!