关于打包静态库

由于公司业务需要,与鹅厂进行合作,为了防止泄密,提供可以连接控制设备的 SDK 给他们是使用。

普及一下静态库和动态库的定义及区别

静态库即静态链接库(Windows 下的 .lib,Linux 和 Mac 下的 .a)。之所以叫做静态,是因为静态库在编译的时候会被直接拷贝一份,复制到目标程序里,这段代码在目标程序里就不会再改变了。静态库的好处很明显,编译完成之后,库文件实际上就没有作用了。目标程序没有外部依赖,直接就可以运行。当然其缺点也很明显,就是会使用目标程序的体积增大。

动态库动态库即动态链接库(Windows 下的 .dll,Linux 下的 .so,Mac 下的 .dylib)。与静态库相反,动态库在编译时并不会被拷贝到目标程序中,目标程序中只会存储指向动态库的引用。等到程序运行时,动态库才会被真正加载进来。动态库的优点是,不需要拷贝到目标程序中,不会影响目标程序的体积,而且同一份库可以被多个程序使用(因为这个原因,动态库也被称作共享库)。同时,编译时才载入的特性,也可以让我们随时对库进行替换,而不需要重新编译代码。动态库带来的问题主要是,动态载入会带来一部分性能损失,使用动态库也会使得程序依赖于外部环境。如果环境缺少动态库或者库的版本不正确,就会导致程序无法运行(Linux 下喜闻乐见的 lib not found 错误)。

创建动态库framework的步骤》》》》》》见图

1、新建项目


Snip20161019_1.png

2、将代码导入到项目中,并将公开的接口暴露


Snip20161019_2.png

注意:开放接口的头文件,一定要导入这个新建文件的头文件中,否则其他项目导入framework时会出现:找不到子模块的警告⚠️


Snip20161019_12.png

3、设置适合所有框架


Snip20161019_3.png

4、因为动态库打包出来只能是在真机或模拟器使用,为了方便调试,可以添加一个脚本命令,生成一个同时支持真机和模拟机的framework



Snip20161019_5.png


Snip20161019_6.png


Snip20161019_7.png

脚本命令

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.

UF_TARGET_NAME=${PROJECT_NAME}

FRAMEWORK_VERSION=

"A"

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

IPHONE_DEVICE_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos

IPHONE_SIMULATOR_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-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

"===== Build Simulator Platform: x86_64 ====="

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}/x86_64"

SYMROOT=

"${SYMROOT}"

ARCHS=

'x86_64'

VALID_ARCHS=

'x86_64'

$ACTION

### Build device platform. (armv7, arm64)

echo

"========== Build Device Platform =========="

echo

"===== Build Device Platform: armv7 ====="

xcodebuild -project

"${PROJECT_FILE_PATH}"

-target

"${TARGET_NAME}"

-configuration

"${CONFIGURATION}"

-sdk iphoneos BUILD_DIR=

"${BUILD_DIR}"

OBJROOT=

"${OBJROOT}"

BUILD_ROOT=

"${BUILD_ROOT}"

CONFIGURATION_BUILD_DIR=

"${IPHONE_DEVICE_BUILD_DIR}/armv7"

SYMROOT=

"${SYMROOT}"

ARCHS=

'armv7 armv7s'

VALID_ARCHS=

'armv7 armv7s'

$ACTION

echo

"===== Build Device Platform: arm64 ====="

xcodebuild -project

"${PROJECT_FILE_PATH}"

-target

"${TARGET_NAME}"

-configuration

"${CONFIGURATION}"

-sdk iphoneos BUILD_DIR=

"${BUILD_DIR}"

OBJROOT=

"${OBJROOT}"

BUILD_ROOT=

"${BUILD_ROOT}"

CONFIGURATION_BUILD_DIR=

"${IPHONE_DEVICE_BUILD_DIR}/arm64"

SYMROOT=

"${SYMROOT}"

ARCHS=

'arm64'

VALID_ARCHS=

'arm64'

$ACTION

### Build device platform. (arm64, armv7)

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/${PROJECT_NAME}.framework"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework"

### Smash them together to combine all architectures.

lipo -create

"${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/i386/${PROJECT_NAME}.framework/${PROJECT_NAME}"

"${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/x86_64/${PROJECT_NAME}.framework/${PROJECT_NAME}"

"${BUILD_DIR}/${CONFIGURATION}-iphoneos/armv7/${PROJECT_NAME}.framework/${PROJECT_NAME}"

"${BUILD_DIR}/${CONFIGURATION}-iphoneos/arm64/${PROJECT_NAME}.framework/${PROJECT_NAME}"

-output

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

### Create standard structure for framework.

#

# If we don't have "Info.plist -> Versions/Current/Resources/Info.plist", we may get error when use this framework.

#

# MyFramework.framework

# |-- MyFramework -> Versions/Current/MyFramework

# |-- Headers -> Versions/Current/Headers

# |-- Resources -> Versions/Current/Resources

# |-- Info.plist -> Versions/Current/Resources/Info.plist

# `-- Versions

#    |-- A

#    |  |-- MyFramework

#    |  |-- Headers

#    |  |  `-- MyFramework.h

#    |  `-- Resources

#    |      |-- Info.plist

#    |      |-- MyViewController.nib

#    |      `-- en.lproj

#    |          `-- InfoPlist.strings

#    `-- Current -> A

#

echo

"========== Create Standard Structure =========="

mkdir -p

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mv

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mv

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Headers"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

mkdir -p

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"

declare -a UF_FILE_LIST

list_files

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/"

UF_FILE_LIST

for

file_name

in

"${UF_FILE_LIST[@]}"

do

if

[[

"${file_name}"

==

"Info.plist"

]] || [[

"${file_name}"

=~ .*\.lproj$ ]]

then

mv

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${file_name}"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources/"

fi

done

mv

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/${FRAMEWORK_VERSION}/"

ln -sfh

"Versions/Current/Resources/Info.plist"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Info.plist"

ln -sfh

"${FRAMEWORK_VERSION}"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Versions/Current"

ln -sfh

"Versions/Current/${PROJECT_NAME}"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

ln -sfh

"Versions/Current/Headers"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Headers"

ln -sfh

"Versions/Current/Resources"

"${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Resources"

### Open the universal folder.

open

"${UNIVERSAL_OUTPUTFOLDER}"

5、生成framework文件,选中“StyleBox”文件,运行程序


Snip20161019_9.png


Snip20161019_11.png

到这里,framework的创建已经成功了,只要右击“show in Finder”,就可以找得framework并导入其他项目中


Paste_Image.png

项目实际使用中,直接导入framework,并且需要动态库需要在此添加:


Snip20161019_14.png

当动态库中,包含资源文件如nib文件或图片的....


Snip20161019_13.png

以上是创建framework的基本流程

作者:MR_詹

链接:http://www.jianshu.com/p/7a49a6373bf6

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值