Flutter IOS 編譯

Flutter IOS 編譯步驟

一:準備環(huán)境

1、Mac 或者 mac虛擬機
虛擬機安裝參考:點擊鏈接

2、安裝Flutter SDK

執(zhí)行命令 flutter doctor 查看環(huán)境

3、安裝Xcode

macOS Flutter環(huán)境 配置成功

二:修改配置文件

主要有兩個文件的配置需要修改

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.xcworkspace

注意:不是選擇 Runner.xcodeproj 打開,而是 Runner.xcworkspace ,否則編譯會出錯。

選擇 [項目名稱] /ios/Runner.xcworkspace 打開,選擇菜單Product—>Build

build

第一次build失敗后,在 [項目名稱]/ios/下會生成一個 Podfile 文件 。

Podfile

如果沒有生成,可以在該目錄下手動創(chuàng)建一個Podfile 文件,內(nèi)容如下:

# 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項目

image.png

先cd到項目的根目錄下,執(zhí)行命令 flutter packages get 下載第三方庫。

然后cd 到 ios 目錄下,再通過命令 pod install 安裝第三方庫。

build

如果正常,會如上圖所示。

最后再回到Xcode上去編譯。

全部完成配置后,xcode的完整目錄接口應該如下圖所示:

完整的目錄結(jié)構(gòu)

再編譯 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 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

解決辦法:

由于現(xiàn)在使用的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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容