加快編譯速度-iOS組件二進(jìn)制庫/源碼切換方案

移動(dòng)端項(xiàng)目復(fù)雜到一定程度都會(huì)走上組件化的道路,組件一多就會(huì)出現(xiàn)聯(lián)編緩慢的問題。Android項(xiàng)目可以通過gradle,依賴源碼生成jar包,提高編譯速度。對(duì)于Objective-C語言的項(xiàng)目,想要加速編譯打包的速度,就需要將大量依賴的組件在打包的時(shí)候都使用靜態(tài)庫或者動(dòng)態(tài)庫依賴,加快編譯鏈接速度,以滿足持續(xù)集成或者是快速部署的要求。


iOS的項(xiàng)目進(jìn)行組件化,往往會(huì)使用cocoapods包管理工具,該方案以此為基礎(chǔ)。二進(jìn)制庫在iOS項(xiàng)目中,指的是靜態(tài)庫與動(dòng)態(tài)庫,當(dāng)組件提供靜態(tài)庫或動(dòng)態(tài)庫的時(shí)候,可以加速項(xiàng)目編譯與構(gòu)建,因?yàn)殪o態(tài)庫與動(dòng)態(tài)庫本身就是已經(jīng)編譯好的庫文件,從而能達(dá)到加速的目的。

目標(biāo)

  1. pod組件同時(shí)提供源碼與二進(jìn)制庫
  2. 項(xiàng)目進(jìn)行調(diào)試的時(shí)候,pod組件庫切換為源碼模式,方便開發(fā)進(jìn)行源碼斷點(diǎn)調(diào)試
  3. 項(xiàng)目進(jìn)行集成與構(gòu)建的時(shí)候,pod組件庫切換為二進(jìn)制模式,加快編譯構(gòu)建速度
  4. 開發(fā)組件的時(shí)候不因?yàn)樵创a與二進(jìn)制的切換方案而喪失依賴其他組件的能力

方案說明與步驟

對(duì)于某一個(gè)組件來說,組件的pod庫應(yīng)該包含源碼和靜態(tài)庫或動(dòng)態(tài)庫兩種文件,這樣才能夠在開發(fā)的過程中使用源碼進(jìn)行編譯調(diào)試,在編譯構(gòu)建的時(shí)候使用靜態(tài)庫或動(dòng)態(tài)庫。關(guān)鍵問題在于如何切換,cocoapods的pod庫是通過podfile文件指明依賴的庫與對(duì)應(yīng)版本,當(dāng)使用pod install的時(shí)候,cocoapods會(huì)通過podfile文件,到cocoapods的中央倉庫中找到該庫對(duì)應(yīng)的podspec文件,再通過podspec文件中的信息來構(gòu)建pod庫。
一個(gè)pod庫的podspec文件如下:

Pod::Spec.new do |s|
  s.name             = 'HBAuthentication'
  s.version          = '0.1.6-beta5'
  s.summary          = '基礎(chǔ)認(rèn)證組件'
  s.description      = <<-DESC
iOS認(rèn)證組件,相關(guān)文檔請(qǐng)?jiān)L問內(nèi)部wiki:
http://***.***.com/member/Auth
                       DESC

  s.homepage         = 'http://***.***.com/Hbec_IOS_common/HBAuthentication'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Neo' => '***@***.com' }
  s.source           = { :git => 'git@***.com:Hbec_IOS_common/HBAuthentication.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '7.0'

  s.resource_bundles = {
    'HBAuthentication' => ['HBAuthentication/Assets/*.{png,cer,json,der,p12}']
  }
  s.source_files = 'HBAuthentication/Classes/**'
end

cocoapods從中央倉庫拉取到對(duì)應(yīng)版本的podsepc文件以后,通過s.source獲得對(duì)應(yīng)版本tag的代碼git版本庫,而后通過s.source_files、s.resource_bundles指明該庫的源碼文件與資源文件對(duì)應(yīng)的路徑,從而最終進(jìn)行源碼依賴構(gòu)建編譯。如果該庫的為二進(jìn)制庫,則需要通過 s.public_header_files、s.ios.vendored_libraries來指明該庫的二進(jìn)制庫的頭文件、library文件的路徑。所以,該方案要求一個(gè)pod庫的工程文件中不僅僅要包含源代碼文件,還要包含將源代碼編譯成靜態(tài)庫或者動(dòng)態(tài)庫的二進(jìn)制文件,切換二進(jìn)制庫與源碼的時(shí)機(jī)應(yīng)該在 pod install 的時(shí)候,而表明是構(gòu)建源碼還是二進(jìn)制庫,則需要通過install的時(shí)候,修改podspec文件中的s.source_files、s.public_header_files、s.ios.vendored_bibraries屬性,來切換該pod庫包含的內(nèi)容。因?yàn)閜odspec文件本身為ruby文件,我們可以利用ENV對(duì)象,來獲取命令行中執(zhí)行pod install時(shí)候傳入的環(huán)境變量,例如可以在podspec文件中這樣寫:

  if ENV['SOURCECODE']
    s.source_files = 'HBAuthentication/Classes/**'
  else
    s.source_files = 'Example/HBAuthenticationBinary/Products/Binary-universal/include/**'
    s.public_header_files = '**/*.h'
    s.ios.vendored_libraries = '**/**.a'
  end

當(dāng)在命令行中傳入環(huán)境變量參數(shù)的時(shí)候 SOURCECODE=1 pod install 的時(shí)候,則podspec文件中if 語句通過ENV對(duì)象來獲取SOURCECODE參數(shù)來表明不同的文件包含屬性,從而能夠切換該pod庫源碼或者二進(jìn)制庫。
通過cocoapods的環(huán)境變量來控制組件庫spec文件的配置信息,后面會(huì)詳細(xì)說到。通過以上的分析,那么該方案大體上分為這幾個(gè)步驟:

  1. 創(chuàng)建pod項(xiàng)目
  2. 創(chuàng)建對(duì)應(yīng)的二進(jìn)制庫target
  3. 生成與源碼對(duì)應(yīng)的二進(jìn)制文件
  4. 設(shè)置pod庫的podspec文件,切換源碼和二進(jìn)制庫的配置
  5. 發(fā)布含有源碼和二進(jìn)制庫的pod庫

創(chuàng)建pod項(xiàng)目和創(chuàng)建對(duì)應(yīng)的二進(jìn)制庫target

通過 pod lib create HBAuthentication 創(chuàng)建出的pod庫項(xiàng)目,目錄大概如下:

.
├── Example
│   ├── HBAuthentication
│   ├── HBAuthentication.xcodeproj
│   ├── HBAuthentication.xcworkspace
│   ├── Podfile
│   ├── Podfile.lock
│   ├── Pods
│   └── Tests
├── HBAuthentication
│   ├── Assets
│   └── Classes
├── HBAuthentication.podspec
├── LICENSE
├── README.md
├── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
└── com.touker.hbauthentication.HBAuthentication.docset
    └── Contents

項(xiàng)目的源碼包含在Class文件中,如下:

├── Assets
│   └── content.json
└── Classes
    ├── HBAuthAPI.h
    ├── HBAuthAPI.m
    ├── HBAuthBridge.h
    ├── HBAuthBridge.m
    ├── HBAuthInfo.h
    ├── HBAuthInfo.m
    ├── HBAuthObject.h
    ├── HBAuthObject.m
    ├── HBAuthStoreManager.h
    ├── HBAuthStoreManager.m
    ├── HBAuthUtil.h
    ├── HBAuthUtil.m

一個(gè)源碼的pod庫項(xiàng)目大概就是這樣,現(xiàn)在需要?jiǎng)?chuàng)建對(duì)應(yīng)的二進(jìn)制庫,以靜態(tài)庫為例,在項(xiàng)目中添加對(duì)應(yīng)的靜態(tài)庫target:file->New->Target->iOS->Framework & Library->Cocoa Touch Static Library


target命名為:HBAuthenticationBinary,創(chuàng)建后項(xiàng)目目錄如下:

.
├── Example
│   ├── HBAuthentication
│   ├── HBAuthentication.xcodeproj
│   ├── HBAuthentication.xcworkspace
|   ├── HBAuthenticationBinary
│   ├── Podfile
│   ├── Podfile.lock
│   ├── Pods
│   └── Tests
├── HBAuthentication
│   ├── Assets
│   └── Classes
├── HBAuthentication.podspec
├── LICENSE
├── README.md
├── _Pods.xcodeproj -> Example/Pods/Pods.xcodeproj
└── com.touker.hbauthentication.HBAuthentication.docset
    └── Contents

緊接著將class文件夾下的源文件添加到HBAuthenticationBinary的target目錄下,添加的時(shí)候選擇不復(fù)制,添加文件索引。


此時(shí)項(xiàng)目文件如下:


緊接著設(shè)置Build Phases中的Compile Source與想要對(duì)外暴露的Headers,如下:


靜態(tài)庫的源碼設(shè)置已經(jīng)完成。

生成與源碼對(duì)應(yīng)的二進(jìn)制文件

靜態(tài)庫需要考慮支持的目標(biāo)架構(gòu),arm架構(gòu)或者是x86架構(gòu),前者用于真機(jī)后者用于模擬器調(diào)試,一般在不考慮靜態(tài)庫大小的情況下,可以將幾種架構(gòu)打成一個(gè)靜態(tài)庫,方便使用??梢酝ㄟ^xcodebuild命令行工具進(jìn)行打包和架構(gòu)合并,要生成.a文件,為了支持真機(jī)和模擬器的版本構(gòu)建,通過xcodebuild與lipo工具,來生成支持x86、arm64、armv7的靜態(tài)庫,將此操作寫成腳本,通過Aggregate Target來執(zhí)行,腳本如下:

set -e
set +u
### Avoid recursively calling this script.
if [[ $UF_MASTER_SCRIPT_RUNNING ]]
then
exit 0
fi
set -u
export UF_MASTER_SCRIPT_RUNNING=1
### Constants.
# RESOURCE_BUNDLE="HBAuthenticationBinary"
# 靜態(tài)庫target對(duì)應(yīng)的scheme名稱
SCHEMENAME="HBAuthenticationBinary"
# .a與頭文件生成的目錄,在項(xiàng)目中的HBAuthenticationBinary目錄下的Products目錄中
BASEBUILDDIR=$PWD/${SCHEMENAME}/Products
rm -fr "${BASEBUILDDIR}"
mkdir "${BASEBUILDDIR}"
# 支持全架構(gòu)的二進(jìn)制文件目錄
UNIVERSAL_OUTPUTFOLDER=${BASEBUILDDIR}/Binary-universal
# 支持真機(jī)的二進(jìn)制文件目錄
IPHONE_DEVICE_BUILD_DIR=${BASEBUILDDIR}/Binary-iphoneos
# 支持模擬器的二進(jìn)制文件目錄
IPHONE_SIMULATOR_BUILD_DIR=${BASEBUILDDIR}/Binary-iphonesimulator
### Functions
## List files in the specified directory, storing to the specified array.
#
# @param $1 The path to list
# @param $2 The name of the array to fill
#
##
list_files ()
{
    filelist=$(ls "$1")
    while read line
    do
        eval "$2[\${#$2[*]}]=\"\$line\""
    done <<< "$filelist"
}
### Take build target.
if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]
then
SF_SDK_PLATFORM=${BASH_REMATCH[1]} # "iphoneos" or "iphonesimulator".
else
echo "Could not find platform name from SDK_NAME: $SDK_NAME"
exit 1
fi
### Build simulator platform. (i386, x86_64)
# echo "========== Build Simulator Platform =========="
# echo "===== Build Simulator Platform: i386 ====="
# xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" CONFIGURATION_BUILD_DIR="${IPHONE_SIMULATOR_BUILD_DIR}/i386" SYMROOT="${SYMROOT}" ARCHS='i386' VALID_ARCHS='i386' $ACTION
echo "===== 構(gòu)建x86_64架構(gòu) ====="
xcodebuild -workspace "${PROJECT_NAME}.xcworkspace" -scheme "${SCHEMENAME}" -configuration "${CONFIGURATION}" -sdk iphonesimulator CONFIGURATION_BUILD_DIR="${IPHONE_SIMULATOR_BUILD_DIR}/x86_64" ARCHS='x86_64' VALID_ARCHS='x86_64' $ACTION
# Build device platform. (armv7, arm64)
echo "========== Build Device Platform =========="
echo "===== Build Device Platform: armv7 ====="
xcodebuild -workspace "${PROJECT_NAME}.xcworkspace" -scheme "${SCHEMENAME}" -configuration "${CONFIGURATION}" -sdk iphoneos CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/armv7" ARCHS='armv7 armv7s' VALID_ARCHS='armv7 armv7s' $ACTION
echo "===== Build Device Platform: arm64 ====="
xcodebuild -workspace "${PROJECT_NAME}.xcworkspace" -scheme "${SCHEMENAME}" -configuration "${CONFIGURATION}" -sdk iphoneos CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/arm64" ARCHS='arm64' VALID_ARCHS='arm64' $ACTION
### Build universal platform.
echo "========== Build Universal Platform =========="
## Copy the framework structure to the universal folder (clean it first).
rm -rf "${UNIVERSAL_OUTPUTFOLDER}"
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
## Copy the last product files of xcodebuild command.
cp -R "${IPHONE_DEVICE_BUILD_DIR}/arm64/lib${SCHEMENAME}.a" "${UNIVERSAL_OUTPUTFOLDER}/lib${SCHEMENAME}.a"
### Smash them together to combine all architectures.
lipo -create "${IPHONE_SIMULATOR_BUILD_DIR}/x86_64/lib${SCHEMENAME}.a" "${IPHONE_DEVICE_BUILD_DIR}/armv7/lib${SCHEMENAME}.a" "${IPHONE_DEVICE_BUILD_DIR}/arm64/lib${SCHEMENAME}.a" -output "${UNIVERSAL_OUTPUTFOLDER}/lib${SCHEMENAME}.a"

echo "========== Create Standard Structure =========="
cp -r "${IPHONE_DEVICE_BUILD_DIR}/arm64/usr/local/include/" "${UNIVERSAL_OUTPUTFOLDER}/include/"
# mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/lib/"
# cp "${UNIVERSAL_OUTPUTFOLDER}/lib${SCHEMENAME}.a" "${UNIVERSAL_OUTPUTFOLDER}/lib/lib${SCHEMENAME}.a"

將該腳本保存問build.sh,在Aggregate Target中設(shè)置Build Phases的Run Script,如下:


將靜態(tài)庫文件和頭文件輸出到 HBAuthenticationBinary/Products 目錄。
運(yùn)行Aggregate Target以后,HBAuthenticationBinary目錄結(jié)構(gòu)如下:

├── HBAuthenticationBinary
│   └── Products
│       ├── Binary-iphoneos
│       │   ├── arm64
│       │   │   ├── libHBAuthenticationBinary.a
│       │   │   ├── libPods-HBAuthenticationBinary.a
│       │   │   └── usr
│       │   │       └── local
│       │   │           └── include
│       │   │               ├── HBAuthAPI.h
│       │   │               ├── HBAuthBridge.h
│       │   │               ├── HBAuthInfo.h
│       │   │               ├── HBAuthObject.h
│       │   │               └── HBAuthStoreManager.h
│       │   └── armv7
│       │       ├── libHBAuthenticationBinary.a
│       │       ├── libPods-HBAuthenticationBinary.a
│       │       └── usr
│       │           └── local
│       │               └── include
│       │                   ├── HBAuthAPI.h
│       │                   ├── HBAuthBridge.h
│       │                   ├── HBAuthInfo.h
│       │                   ├── HBAuthObject.h
│       │                   └── HBAuthStoreManager.h
│       ├── Binary-iphonesimulator
│       │   └── x86_64
│       │       ├── libHBAuthenticationBinary.a
│       │       ├── libPods-HBAuthenticationBinary.a
│       │       └── usr
│       │           └── local
│       │               └── include
│       │                   ├── HBAuthAPI.h
│       │                   ├── HBAuthBridge.h
│       │                   ├── HBAuthInfo.h
│       │                   ├── HBAuthObject.h
│       │                   └── HBAuthStoreManager.h
│       └── Binary-universal
│           ├── include
│           │   ├── HBAuthAPI.h
│           │   ├── HBAuthBridge.h
│           │   ├── HBAuthInfo.h
│           │   ├── HBAuthObject.h
│           │   └── HBAuthStoreManager.h
│           └── libHBAuthenticationBinary.a

其實(shí)Binary-universal就是最終的靜態(tài)庫的文件,其他的Binary-iphonesimulator與Binary-iphoneos目錄下的文件都不需要包含到pod的git版本庫中。

設(shè)置pod庫的podspec文件,切換源碼和二進(jìn)制庫的配置

接下來設(shè)置podspec文件,內(nèi)容如下:

Pod::Spec.new do |s|
  s.name             = 'HBAuthentication'
  s.version          = '0.1.6-beta5'
  s.summary          = '基礎(chǔ)認(rèn)證組件'
  s.description      = <<-DESC
iOS認(rèn)證組件,相關(guān)文檔請(qǐng)?jiān)L問內(nèi)部wiki:
http://***.***.com/member/Auth
                       DESC

  s.homepage         = 'http://***.***.com/Hbec_IOS_common/HBAuthentication'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Neo' => '***@***.com' }
  s.source           = { :git => 'git@***.com:Hbec_IOS_common/HBAuthentication.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '7.0'

  s.resource_bundles = {
    'HBAuthentication' => ['HBAuthentication/Assets/*.{png,cer,json,der,p12}']
  }
  s.source_files = 'HBAuthentication/Classes/**'
end

  if ENV['SOURCECODE']
    puts '-----------'
    puts 'HBAuthentication Source Code'
  else
    puts '+++++++++++'
    puts 'HBAuthentication Binary'
      s.source_files = 'Example/HBAuthenticationBinary/Products/Binary-universal/include/**'
      s.public_header_files = 'Example/HBAuthenticationBinary/Products/Binary-universal/include/*.h'
      s.ios.vendored_libraries = 'Example/HBAuthenticationBinary/Products/Binary-universal/libHBAuthenticationBinary.a'
  end
  s.dependency 'CocoaLumberjack'
  s.dependency 'HBWebBridge'
end

發(fā)布含有源碼和二進(jìn)制庫的pod庫

此時(shí)在Example中測試該pod庫,在podfile中添加該庫依賴:

  pod 'HBAuthentication', :path => '../'

然后使用 pod install 此時(shí),會(huì)發(fā)現(xiàn)Pods的Development Pods目錄下的HBAuthentication為下圖:

說明為靜態(tài)庫依賴。
在Example項(xiàng)目中使用 SOURCECODE=1 pod install以后,則切換到源碼模式。

8CE3FB66-6D1E-426E-A041-96549F2D5465.png

分別運(yùn)行測試,都沒有問題,可以將工程文件提交到git倉庫,注意前面生成的其他架構(gòu)的文件目錄可以刪除,不提交到git版本庫中,因?yàn)槭嵌M(jìn)制文件,git對(duì)二進(jìn)制文件不能做到增量更新,隨著版本增加,git版本庫會(huì)越來越大,所以最好精簡靜態(tài)庫的大小,最后在自己的私有cocoapods倉庫中進(jìn)行pod庫的發(fā)布。私有cocoapods庫搭建參考官方文檔

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

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

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