ios framework 开发 之 实战 一,合并失败了
保证public 文件目录独立
在 XCode 7 中,这一条已经自动实现了
Build Settings》Public Headers Folder Path
"$(PROJECT_NAME)Headers" for all configurations
避免代码、调试符号等被剥离
在 Build Setting 中设置
"Dead Code Stripping" => No (for all settings) "Strip Debug Symbols During Copy" => No (for all settings)//XCode7中,默认就是这样 "Strip Style" => Non-Global Symbols (for all settings)
支持全平台的版本
在Project 中的 Build Setting 中设置
"Build Active Architecture Only" => No
分为debug 和 release 版本,debug的时候可以设置为Yes.
把Framework作为依赖对象
set -e rm -rf "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers" mkdir -p "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers" # Link the "Current" version to "A" /bin/ln -sfh A "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/Current" /bin/ln -sfh Versions/Current/Headers "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Headers" /bin/ln -sfh "Versions/Current/${PRODUCT_NAME}" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/${PRODUCT_NAME}" # The -a ensures that the headers maintain the source modification date so that we don't constantly # cause propagating rebuilds of files that import these headers. /bin/cp -a "${TARGET_BUILD_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}/" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers"
该步操作只会编译一个平台的版本,但是发布的时候,并不知道第三方会用手机,还是用模拟器来使用。所以下面需要编译一个“fat library”。
编译 fat framework
适配各个平台
Step 1 : Create an Aggregate Target
File > New Target > iOS > Other and create a new Aggregate target
Step 2 : Add the Static Library as a Dependent Target
在Target Dependencies中添加当前的framework
Step 3 : 在 fat framework中添加一个新的run script
set -e set +u # Avoid recursively calling this script. if [[ $SF_MASTER_SCRIPT_RUNNING ]] then exit 0 fi set -u export SF_MASTER_SCRIPT_RUNNING=1 SF_TARGET_NAME=${PROJECT_NAME} SF_EXECUTABLE_PATH="lib${SF_TARGET_NAME}.a" SF_WRAPPER_NAME="${SF_TARGET_NAME}.framework" # The following conditionals come from # https://github.com/kstenerud/iOS-Universal-Framework if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]] then SF_SDK_PLATFORM=${BASH_REMATCH[1]} else echo "Could not find platform name from SDK_NAME: $SDK_NAME" exit 1 fi if [[ "$SDK_NAME" =~ ([0-9]+.*$) ]] then SF_SDK_VERSION=${BASH_REMATCH[1]} else echo "Could not find sdk version from SDK_NAME: $SDK_NAME" exit 1 fi if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]] then SF_OTHER_PLATFORM=iphonesimulator else SF_OTHER_PLATFORM=iphoneos fi if [[ "$BUILT_PRODUCTS_DIR" =~ (.*)$SF_SDK_PLATFORM$ ]] then SF_OTHER_BUILT_PRODUCTS_DIR="${BASH_REMATCH[1]}${SF_OTHER_PLATFORM}" else echo "Could not find platform name from build products directory: $BUILT_PRODUCTS_DIR" exit 1 fi # Build the other platform. xcrun xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk ${SF_OTHER_PLATFORM}${SF_SDK_VERSION} BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}" $ACTION # Smash the two static libraries into one fat binary and store it in the .framework xcrun lipo -create "${BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" -output "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}" # Copy the binary to the other architecture folder to have a complete framework in both. cp -a "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}"
如果使用 Cocoapods ,就需要替换掉 xcrun xcodebuild 这行
set -e set +u # Avoid recursively calling this script. if [[ $SF_MASTER_SCRIPT_RUNNING ]] then exit 0 fi set -u export SF_MASTER_SCRIPT_RUNNING=1 SF_TARGET_NAME=${PROJECT_NAME} SF_EXECUTABLE_PATH="lib${SF_TARGET_NAME}.a" SF_WRAPPER_NAME="${SF_TARGET_NAME}.framework" # The following conditionals come from # https://github.com/kstenerud/iOS-Universal-Framework if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]] then SF_SDK_PLATFORM=${BASH_REMATCH[1]} else echo "Could not find platform name from SDK_NAME: $SDK_NAME" exit 1 fi if [[ "$SDK_NAME" =~ ([0-9]+.*$) ]] then SF_SDK_VERSION=${BASH_REMATCH[1]} else echo "Could not find sdk version from SDK_NAME: $SDK_NAME" exit 1 fi if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]] then SF_OTHER_PLATFORM=iphonesimulator else SF_OTHER_PLATFORM=iphoneos fi if [[ "$BUILT_PRODUCTS_DIR" =~ (.*)$SF_SDK_PLATFORM$ ]] then SF_OTHER_BUILT_PRODUCTS_DIR="${BASH_REMATCH[1]}${SF_OTHER_PLATFORM}" else echo "Could not find platform name from build products directory: $BUILT_PRODUCTS_DIR" exit 1 fi # Build the other platform. xcrun xcodebuild ONLY_ACTIVE_ARCH=NO -workspace "${PROJECT_DIR}/${PROJECT_NAME}.xcworkspace" -scheme "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk ${SF_OTHER_PLATFORM}${SF_SDK_VERSION} BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}" $ACTION # Smash the two static libraries into one fat binary and store it in the .framework xcrun lipo -create "${BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_EXECUTABLE_PATH}" -output "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}" # Copy the binary to the other architecture folder to have a complete framework in both. cp -a "${BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}" "${SF_OTHER_BUILT_PRODUCTS_DIR}/${SF_WRAPPER_NAME}/Versions/A/${SF_TARGET_NAME}"