解決SecurityEnvSDK與SGMain的衝突問題

問題

在集成友盟統計和阿里百川以後項目報以下錯誤python

duplicate symbol '_OBJC_CLASS_$_tdvSFHFKeychainUtils' in:
    /Users/.../Pods/UMCSecurityPlugins/thirdparties/SecurityEnvSDK.framework/SecurityEnvSDK(SecurityEnvSDK99999999.o)
    /Users/.../阿里百川/WXFrameworks/SGMain.framework/SGMain(SGMain99999999.o)
duplicate symbol '_OBJC_METACLASS_$_tdvSFHFKeychainUtils' in:
    /Users/.../Pods/UMCSecurityPlugins/thirdparties/SecurityEnvSDK.framework/SecurityEnvSDK(SecurityEnvSDK99999999.o)
    /Users/.../阿里百川/WXFrameworks/SGMain.framework/SGMain(SGMain99999999.o)
duplicate symbol '_OBJC_CLASS_$_SGDataCollectionLock' in:
    /Users/.../Pods/UMCSecurityPlugins/thirdparties/SecurityEnvSDK.framework/SecurityEnvSDK(SecurityEnvSDK99999999.o)
    /Users/.../阿里百川/WXFrameworks/SGMain.framework/SGMain(SGMain99999999.o)
duplicate symbol '_OBJC_METACLASS_$_SGDataCollectionLock' in:
    /Users/.../Pods/UMCSecurityPlugins/thirdparties/SecurityEnvSDK.framework/SecurityEnvSDK(SecurityEnvSDK99999999.o)
    /Users/.../阿里百川/WXFrameworks/SGMain.framework/SGMain(SGMain99999999.o)
複製代碼

報錯是說有重複類,解決的辦法比較簡單粗暴,就是把在Xcode裏全文搜索 -framework "SecurityEnvSDK",接着全文替換爲空字符串就能夠了。git

雖然解決這個問題的方式很簡單,可是每次 pod install 後都要作一遍該操做,這就很無語了 。github

那有什麼辦法能夠讓咱們不用本身去作這個煩瑣的事情呢?shell

改進

  1. 首先要搞清楚,上面的操做原理是怎麼回事?其它很簡單,就是將下面這兩個文件中 OTHER_LDFLAGS 所在行的內容裏,把 -framework "SecurityEnvSDK" 置爲空字符串。
Pods/Target Support Files/Pods-項目名/Pods-項目名.debug.xcconfig
Pods/Target Support Files/Pods-項目名/Pods-項目名.release.xcconfig
複製代碼
  1. 有什麼辦法能夠讓咱們在適當的時候去執行這個置空字符串的操做? Cocoapods 提供了一個很好用的 Hook 就是 post_install,這個鉤子的做用就是方便咱們在執行 pod install 以後去作一些其它配置,這裏咱們就用它來搞事情。

Podfile 文件中使用的是 ruby 語言,ruby 執行終端命令的代碼以下所示:ruby

post_install do |installer|
 # command = "echo 'hello world'"
  command = "終端命令"
  system(command)
end
複製代碼

OK,如今開始搞事!bash

步驟

  1. 在項目的目錄,即與Pods平級目錄中,新建一個文件,名爲 fix.py
.
├── ...
├── Podfile
├── Podfile.lock
├── Pods
│   ├── ...
│   └── ...
└── fix.py
複製代碼
  1. fix.py 中粘貼以下內容
# -*- coding: UTF-8 -*-
import sys, os, getopt, codecs

def get_current_file_name():
    """獲取當前文件名稱"""
    return os.path.split(__file__)[-1]

def replace_all_str(file_path, for_str, to_str):
    """ 全文搜索替換或單行替換 :param file_path: 文件路徑 :param for_str: 要被替換的內容 :param to_str: 替換以後的內容 """
    if not os.path.exists(file_path):
        # 文件不存在
        print('文件不存在')
        return
    bak_file_path = file_path+".bak"
    with codecs.open(file_path, 'r', encoding='utf-8') as f, codecs.open(bak_file_path, 'w', encoding='utf-8') as f_w:
        lines = f.readlines()
        for line in lines:
            if "OTHER_LDFLAGS" in line and for_str in line:
                line = line.replace(for_str, to_str)
            f_w.write(line)

    os.remove(file_path)
    os.rename(bak_file_path, file_path)

def throwParamError():
    print("請正確輸入命令: %s -p 項目名稱" % get_current_file_name())
    sys.exit(0)

def main(argv):
    project_name = ""
    try:
        opts, args = getopt.getopt(argv, "p:", ["project="])
    except getopt.GetoptError:
        throwParamError()
    for opt, arg in opts:
        # print("opt -- ", opt)
        # print("arg -- ", arg)
        if opt in ('-p', '--project'):
            project_name = arg

    if not len(project_name):
        throwParamError()
    
    path_str = "Pods/Target Support Files/Pods-%s/Pods-%s.%s.xcconfig"
    xcconfig_debug_path = path_str % (project_name, project_name, "debug")
    xcconfig_release_path = path_str % (project_name, project_name, "release")
    # print(xcconfig_debug_path)
    # print(xcconfig_release_path)
    be_fixed_str = '-framework "SecurityEnvSDK"'
    replace_all_str(xcconfig_debug_path,  be_fixed_str, '')
    replace_all_str(xcconfig_release_path,  be_fixed_str, '')
    print("%s is fixed successfully" %project_name)

if __name__ == "__main__":
    main(sys.argv[1:])
複製代碼
  1. 打開 Podfile,在內容最後添加以下內容
post_install do |installer|
  # 解決SecurityEnvSDK與SGMain的衝突問題
  command = "python fix.py -p 項目名稱"
  system(command)
end
複製代碼
  1. 執行pod install

好了,如今開始就又能夠繼續愉快的搬磚了~微信

GitHub

相關代碼文件能夠到這裏下載,若是以爲不錯,不妨給個 Star 鼓勵一下 github.com/LinXunFeng/…post

歡迎關注微信公衆號:全棧行動
相關文章
相關標籤/搜索