Framework開發(fā)看我就夠了第二回:靜態(tài)庫、動(dòng)態(tài)庫,腳本生成

上回講到framework的創(chuàng)建和使用,這回我們來講講靜態(tài)庫和動(dòng)態(tài)庫。各位看官往下看。

1. 什么是靜態(tài)庫和動(dòng)態(tài)庫?

靜態(tài)庫:鏈接時(shí)完整地拷貝至執(zhí)行文件中,被多次使用就有多份冗余拷貝。

動(dòng)態(tài)庫:鏈接時(shí)不復(fù)制,程序運(yùn)行時(shí)由系統(tǒng)動(dòng)態(tài)加載到內(nèi)存,供程序調(diào)用,系統(tǒng)只加載一次,多個(gè)程序共用,節(jié)省內(nèi)存。

我們一開始創(chuàng)建的默認(rèn)framework就是動(dòng)態(tài)的,我們可以在Build settings->mach-O Type里修改static library來改成靜態(tài)庫。


截屏2020-05-25 下午2.22.39.png
截屏2020-05-25 下午2.24.07.png

我們改成static library后運(yùn)行,然后從products里把framework拷貝到之前那個(gè)sdk文件夾,因?yàn)槭庆o態(tài)庫,demo里配置改成Do Not Embed,運(yùn)行demo我們可以看到也能成功打印。


截屏2020-05-25 下午2.33.48.png
2. 腳本編寫

但是每次從products去拷貝framework有點(diǎn)麻煩,有沒有辦法可以自動(dòng)生成framework到我們項(xiàng)目指定的文件下底下呢?有,xcode里我們可以編寫腳本來實(shí)現(xiàn)。

1.首先我們先新建一個(gè)target。


截屏2020-05-25 下午3.36.05.png
截屏2020-05-25 下午3.38.30.png

2.然后添加一個(gè)run script phase


截屏2020-05-25 下午3.39.29.png

3.添加依賴


截屏2020-05-25 下午3.49.54.png

4.腳本編寫,我們先編寫個(gè)靜態(tài)庫的腳本


截屏2020-05-25 下午3.54.16.png

上一下代碼

# Sets the target folders and the final framework product.
FMK_NAME=TestFramework

# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/../sdk/framework/${FMK_NAME}.framework

# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework

SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework

# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphoneos clean

xcodebuild -configuration "Release" -target "TestFramework" -sdk iphonesimulator clean

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphoneos build

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphonesimulator build

# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi

mkdir -p "${INSTALL_DIR}"

cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"

# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"

rm -r "${WRK_DIR}"

5.最后,我們運(yùn)行腳本target,可以看到在項(xiàng)目sdk文件夾下生成了個(gè)framework


截屏2020-05-25 下午3.57.26.png

6.動(dòng)態(tài)庫和靜態(tài)庫前面步驟都一樣,除了腳本不一樣,就直接上腳本代碼

# Sets the target folders and the final framework product.
FMK_NAME=TestFramework

# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/../sdk/${FMK_NAME}.framework

# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework

# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphoneos clean build

# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi

mkdir -p "${INSTALL_DIR}"

cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"

# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"

rm -r "${WRK_DIR}"

請(qǐng)不要吝嗇你們的小????

最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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