已有項(xiàng)目配置flutter(iOS篇進(jìn)階)

前篇:已有項(xiàng)目配置flutter(iOS篇)

該文檔已過時,可點(diǎn)擊查看下面的最新官方文檔鏈接

官方參考文檔

官方給出了flutter在iOS上的簡單導(dǎo)入,但是當(dāng)出現(xiàn)post_install僅能有一個的問題該怎么解決了?
解決思路:既然不能出現(xiàn)多次post_install, 那么只好將需要post_install的地方合并了

  • 看官方配置pod
# “path/to” 是自己項(xiàng)目的路徑
flutter_application_path = 'path/to/my_flutter/'
  load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  • 根據(jù)flutter_application_path,找到自己項(xiàng)目下的podhelper.rb,將其復(fù)制到ios項(xiàng)目根目錄下。
  • 修改podhelper.rb中涉及到的路徑。
  • 重新配置Podfile中的podhelper.rb路徑flutter_application_path
flutter_application_path ||= File.join(__dir__, '..', 'flutter_demo')
framework_dir = File.join(flutter_application_path, '.ios', 'Flutter')
  • 對比Podfilepodhelper.rbpost_install代碼塊的不同,然后合并到Podfile,并將podhelper.rb中的post_install代碼塊注釋掉。
  • 這是本人在flutter 1.0時的做法,現(xiàn)在flutter升級到了1.7.8, 還能正常運(yùn)行。
  • 下面貼出Podfile的部分代碼與podhelper.rb的全部代碼,包括本人的注釋。注意看注釋哦
    我的flutter項(xiàng)目名稱是flutter_demo,ios項(xiàng)目名稱TestFlutter。兩個項(xiàng)目在同一個文件夾下,這樣方便使用相對路徑。

Podfile 部分代碼

# 配置flutter:
# - swift與oc的橋接配置‘use_frameworks!’上邊已經(jīng)配置,這里就不配置了
# - bitcode 需要設(shè)置為NO, 工程中也已經(jīng)設(shè)置,這里也不設(shè)置了。pod里的設(shè)置方法可參考:http://m.itdecent.cn/p/bf3002de6a5e。個人不建議在pod中設(shè)置
# - 如果podfile中沒使用post_install, 則可以直接使用下面兩句代碼就行了。但是已經(jīng)使用了,所以需要從新配置。
# flutter_application_path = '../flutter_demo/'
# eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
# - 將需要使用的podhelper.rb復(fù)制到ios項(xiàng)目中,然后按照iOS項(xiàng)目下podhelper.rb中寫的步驟繼續(xù)配置
# - 以下代碼是導(dǎo)入flutter
#eval(File.read(File.join(__dir__, 'podhelper.rb')), binding)
# - framework_dir路徑配置
flutter_application_path ||= File.join(__dir__, '..', 'flutter_demo')
framework_dir = File.join(flutter_application_path, '.ios', 'Flutter')

post_install do |installer|
    installer.pods_project.targets.each do |target|
        # 強(qiáng)制設(shè)置SWIFT_VERSION為'5'
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '5.0'
        end
        
        if [].include? target.name
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '4.2'
            end
        end

        if target.name == 'Params' then
            target.build_configurations.each do |config|
                config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
            end
        elsif target.name == 'SVProgressHUD'
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SV_APP_EXTENSIONS'
            end
        elsif target.name == 'SSMessage'
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SSMESSAGE_NEWALERT=1'
            end
            # flutter
          # elsif target.name == 'Flutter' || target.name == 'TestFlutter'
          #   target.build_configurations.each do |config|
          #       config.build_settings['ENABLE_BITCODE'] = 'NO'
          #       xcconfig_path = config.base_configuration_reference.real_path
          #       File.open(xcconfig_path, 'a+') do |file|
          #           file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
          #       end
          #   end
        end

                target.build_configurations.each do |config|
                    config.build_settings['ENABLE_BITCODE'] = 'NO'
                    xcconfig_path = config.base_configuration_reference.real_path
                    File.open(xcconfig_path, 'a+') do |file|
                        file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
                    end
                end

    end
end

podhelper.rb全部代碼

# 文件改動解析:
# - 該文件從目標(biāo)項(xiàng)目flutter_demo中復(fù)制過來
# - 方法parse_KV_file(), flutter_root() 不做修改
# - 由于該文件的路徑改變了,所以需要修改flutter_application_path的值。__dir__ 應(yīng)該表示當(dāng)前路徑
# - podfile不能使用多個post_install,所以post_install中的操作,移動到podfile中操作,并且將該文件中的操作注釋掉。需要注意的是,修改其中的路徑值。
# -
# -
# -

def parse_KV_file(file, separator='=')
    file_abs_path = File.expand_path(file)
    if !File.exists? file_abs_path
        return [];
    end
    pods_array = []
    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_array.push({:name => podname, :path => podpath});
         else
            puts "Invalid plugin specification: #{line}"
        end
    }
    return pods_array
end

def flutter_root(f)
    generated_xcode_build_settings = parse_KV_file(File.join(f, File.join('.ios', 'Flutter', 'Generated.xcconfig')))
    if generated_xcode_build_settings.empty?
        puts "Generated.xcconfig must exist. Make sure `flutter packages get` is executed in ${f}."
        exit
    end
    generated_xcode_build_settings.map { |p|
        if p[:name] == 'FLUTTER_ROOT'
            return p[:path]
        end
    }
end

# If this wasn't specified, assume it's two levels up from the directory of this script.
flutter_application_path ||= File.join(__dir__, '..', 'flutter_demo')
framework_dir = File.join(flutter_application_path, '.ios', 'Flutter')

engine_dir = File.join(framework_dir, 'engine')
if !File.exist?(engine_dir)
    # Copy the debug engine to have something to link against if the xcode backend script has not run yet.
    debug_framework_dir = File.join(flutter_root(flutter_application_path), 'bin', 'cache', 'artifacts', 'engine', 'ios')
    FileUtils.mkdir_p(engine_dir)
    FileUtils.cp_r(File.join(debug_framework_dir, 'Flutter.framework'), engine_dir)
    FileUtils.cp(File.join(debug_framework_dir, 'Flutter.podspec'), engine_dir)
end

pod 'Flutter', :path => engine_dir
pod 'FlutterPluginRegistrant', :path => File.join(framework_dir, 'FlutterPluginRegistrant')

symlinks_dir = File.join(framework_dir, '.symlinks')
FileUtils.mkdir_p(symlinks_dir)
plugin_pods = parse_KV_file(File.join(flutter_application_path, '.flutter-plugins'))
plugin_pods.map { |r|
    symlink = File.join(symlinks_dir, r[:name])
    FileUtils.rm_f(symlink)
    File.symlink(r[:path], symlink)
    pod r[:name], :path => File.join(symlink, 'ios')
}

# Ensure that ENABLE_BITCODE is set to NO, add a #include to Generated.xcconfig, and
# add a run script to the Build Phases.
# TODO(dnfield): Figure out a way to deliver the Build Phase scripts without manual user intervention.
# post_install do |installer|
#     installer.pods_project.targets.each do |target|
#         target.build_configurations.each do |config|
#             config.build_settings['ENABLE_BITCODE'] = 'NO'
#             xcconfig_path = config.base_configuration_reference.real_path
#             File.open(xcconfig_path, 'a+') do |file|
#                 file.puts "#include \"#{File.realpath(File.join(framework_dir, 'Generated.xcconfig'))}\""
#             end
#         end
#     end
# end

  • 為什么復(fù)制podhelper.rb,而不是直接修改了??
    因?yàn)?code>Flutter Module經(jīng)常重構(gòu).android,不知道什么時候podhelper.rb就被還原了。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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