Flutter IOS 編譯步驟
一:準備環境
1、Mac 或者 mac虛擬機
虛擬機安裝參考:點擊鏈接
2、安裝Flutter SDK
執行命令flutter doctor查看環境
3、安裝Xcode
macOS Flutter環境 配置成功
二:修改配置文件
主要有兩個文件的配置需要修改
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/flutterFLUTTER_APPLICATION_PATH=/Users/metre/Desktop/workspace/projects/[項目名稱]FLUTTER_TARGET=/Users/metre/Desktop/workspace/projects/[項目名稱]/lib/main.dartFLUTTER_BUILD_DIR=buildSYMROOT=${SOURCE_ROOT}/../build/iosFLUTTER_FRAMEWORK_DIR=/Users/metre/Desktop/workspace/flutter/bin/cache/artifacts/engine/iosFLUTTER_BUILD_NAME=1.0.0FLUTTER_BUILD_NUMBER=1
2、.packages
項目根目錄下的.packages文件,里面的路徑也要改成mac下路徑。
三:使用xcode打開項目
Runner.xcworkspace
注意:不是選擇Runner.xcodeproj打開,而是Runner.xcworkspace,否則編譯會出錯。
選擇 [項目名稱]/ios/Runner.xcworkspace打開,選擇菜單Product—>Build
build
第一次build失敗后,在[項目名稱]/ios/下會生成一個Podfile文件 。
Podfile
如果沒有生成,可以在該目錄下手動創建一個Podfile文件,內容如下:
# Uncomment this line to define a global platform for your projectplatform: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,}defparse_KV_file(file,separator='=')file_abs_path=File.expand_path(file)if!File.exists?file_abs_pathreturn[];endpods_ary=[]skip_line_start_symbols=["#","/"]File.foreach(file_abs_path){|line|nextifskip_line_start_symbols.any?{|symbol|line=~/^\s*#{symbol}/}plugin=line.split(pattern=separator)ifplugin.length==2podname=plugin[0].strip()path=plugin[1].strip()podpath=File.expand_path("#{path}",file_abs_path)pods_ary.push({:name=>podname,:path=>podpath});elseputs"Invalid plugin specification:#{line}"end}returnpods_aryendtarget'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 Podsgenerated_xcode_build_settings=parse_KV_file('./Flutter/Generated.xcconfig')ifgenerated_xcode_build_settings.empty?puts"Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first."endgenerated_xcode_build_settings.map{|p|ifp[: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 Podsplugin_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')}endpost_installdo|installer|installer.pods_project.targets.eachdo|target|target.build_configurations.eachdo|config|config.build_settings['ENABLE_BITCODE']='NO'endendend
四:使用<終端>命令工具,build項目
image.png
先cd到項目的根目錄下,執行命令flutter packages get下載第三方庫。
然后cd 到 ios 目錄下,再通過命令pod install安裝第三方庫。
build
如果正常,會如上圖所示。
最后再回到Xcode上去編譯。
全部完成配置后,xcode的完整目錄接口應該如下圖所示:
完整的目錄結構
再編譯 Product—>Build,就成功了
編譯成功
五:可能遇到的問題
1、編譯問題: script phase “[CP] Embed Pods Frameworks”
Framework重復
:-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
去掉 Flutter.framework 的勾
2、真機Release包白屏問題:
解決辦法:先去項目根目錄,運用以下命令,再使用xcode打包:
flutter cleanflutter 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 installAnalyzing dependencies[!]CDN:trunk Repo update failed-40error(s):CDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.0/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.2/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.6.3/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.7.0/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.3/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.4/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded:https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/b/0/d/JPush/1.8.4.1/JPush.podspec.json Response:Timeout was reachedCDN:trunk URL couldn'tbedownloaded: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.
6、錯誤:[!]CDN:trunk Repo update failed
source 'https://github.com/CocoaPods/Specs.git'
7、NO pod?install
項目目錄? cd ios?pod?install
解決方案:
https://flutter.dev/docs/development/ios-project-migration
轉自鏈接:http://www.lxweimin.com/p/8e402987fffd