Flutter IOS 编译步骤
一:准备环境
1、Mac 或者 mac虚拟机
虚拟机安装参考:点击链接
2、安装Flutter SDK
执行命令 flutter doctor
查看环境
3、安装Xcode
二:修改配置文件
主要有两个文件的配置需要修改
1、Generated.xcconfig
ios/Flutter/
下的 Generated.xcconfig
文件里,所涉及的到路径,必须要改成mac对于的路径。
// This is a generated file; do not edit or check into version control.
FLUTTER_ROOT=/Users/metre/Desktop/workspace/flutter
FLUTTER_APPLICATION_PATH=/Users/metre/Desktop/workspace/projects/[项目名称]
FLUTTER_TARGET=/Users/metre/Desktop/workspace/projects/[项目名称]/lib/main.dart
FLUTTER_BUILD_DIR=build
SYMROOT=${SOURCE_ROOT}/../build/ios
FLUTTER_FRAMEWORK_DIR=/Users/metre/Desktop/workspace/flutter/bin/cache/artifacts/engine/ios
FLUTTER_BUILD_NAME=1.0.0
FLUTTER_BUILD_NUMBER=1
2、.packages
项目根目录下的.packages文件,里面的路径也要改成mac下路径。
三:使用xcode打开项目
注意:不是选择 Runner.xcodeproj 打开,而是 Runner.xcworkspace ,否则编译会出错。
选择 [项目名称] /ios/Runner.xcworkspace 打开,选择菜单Product—>Build
第一次build失败后,在 [项目名称]/ios/
下会生成一个 Podfile
文件 。
如果没有生成,可以在该目录下手动创建一个Podfile
文件,内容如下:
# Uncomment this line to define a global platform for your project
platform :ios, '10.3'
# 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
# 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['ENABLE_BITCODE'] = 'NO'
end
end
end
四:使用<终端>命令工具,build项目
先cd到项目的根目录下,执行命令 flutter packages get
下载第三方库。
然后cd 到 ios 目录下,再通过命令 pod install
安装第三方库。
如果正常,会如上图所示。
最后再回到Xcode上去编译。
全部完成配置后,xcode的完整目录接口应该如下图所示:
再编译 Product—>Build,就成功了
五:可能遇到的问题
1、编译问题: script phase “[CP] Embed Pods Frameworks”
:-1: Multiple commands produce '/Users/GenlotRD/Library/Developer/Xcode/DerivedData/Runner-ahulhxjyhsnwsxgyjageaweqzpvb/Build/Products/Debug-iphonesimulator/Runner.app/Frameworks/Flutter.framework':
1) Target 'Runner' has copy command from '/Users/GenlotRD/IOS/vlt-channel-app/ios/Flutter/Flutter.framework' to '/Users/GenlotRD/Library/Developer/Xcode/DerivedData/Runner-ahulhxjyhsnwsxgyjageaweqzpvb/Build/Products/Debug-iphonesimulator/Runner.app/Frameworks/Flutter.framework'
2) That command depends on command in Target 'Runner': script phase “[CP] Embed Pods Frameworks”
解决办法:去掉 Flutter.framework
2、真机Release包白屏问题:
解决办法:先去项目根目录,运用以下命令,再使用xcode打包:
flutter clean
flutter build ios --release
3、错误:ios/Flutter/flutter_assets: No such file or directory
error: /Users/xxx/Downloads/projectName/ios/Flutter/flutter_assets: No such file or directory
解决办法:
由于现在使用的flutter版本生成的flutter_assets是放到App.framework里的,把flutter_assets从App.framework里拷贝出来放到Flutter文件夹里就可以了。
4、错误: CDN: trunk URL couldn't be downloaded
little:ios RD$ pod install
Analyzing dependencies
[!] CDN: trunk Repo update failed - 40 error(s):
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.0/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.2/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.3/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.7.0/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.3/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.4/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.4.1/JPush.podspec.json Response: Timeout was reached
CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.5/JPush.podspec.json Response: Timeout was reached
编辑Podfile文件
vi Podfile
头部加上:
source 'https://cdn.cocoapods.org/'
5、错误:Building for iOS, but the linked and embedded framework
Building for iOS, but the linked and embedded framework 'App.framework' was built for iOS Simulator.
解决方案:
https://flutter.dev/docs/development/ios-project-migration