Saturngod

What's on my mind

Static Library to Framework

I have no idea , new version of cocoa pods was not working with static library. It was working before and I didn’t change any code in podspec. Xcode couldn’t find my library when it was building.

So, I decided to change current static library to the framework. I wrote about how to build enable bit code for static library.

I don’t want to change current my code and current code cannot change to framework because it was using other static library inside , too.

I changed my build code like following


# build.sh 
folder="Release"
project_name="mycustomlib"
lib_name="libmycustom"
name_simulator=$lib_name"_simulator"
name_device=$lib_name"_device"

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -target $project_name build
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -sdk iphonesimulator -target $project_name build
rm -r build/Production-SDK
cp -r build/$folder-iphoneos build/Production-SDK
cp build/$folder-iphonesimulator/$lib_name.a build/Production-SDK/$name_simulator.a
mv build/Production-SDK/$lib_name.a build/Production-SDK/$name_device.a
cd build/Production-SDK/
lipo -create $name_simulator.a $name_device.a -output $lib_name.a
rm $name_device.a
rm $name_simulator.a
file $lib_name.a

# Time For Building Framework
rm -rf $lib_name".framework"
mkdir $lib_name".framework"

LIBTOOL_FLAGS="-static"
libtool -no_warning_for_no_symbols $LIBTOOL_FLAGS -o $lib_name.framework/$lib_name $lib_name.a
cp -r ./usr/local/include $lib_name.framework/Headers/
cp ../../fwInfo.plist $lib_name.framework/Info.plist

open .

You need to have fwInfo.plist for framework plist file. You can create easily framework plist file from the Xcode. File > Project > Framework & Library > Cocoa Touch Framework

After that copy the info.plist and change to fwInfo.plist. Put in same path with build.sh.

After running the build.sh , you will see framework file , too.

Enable Bitcode in library file (.a)

After enable bit code in Xcode 7 , I couldn’t build .a file with embed-bitcode. Same problem for the zbar library , too. So, I cannot use my own library with .a file or cannot use the zbar for production. After searching from google , I found a way to build. We need to use the command line with xcodebuild.

I wrote the build.sh like following and same folder with myproject.xcodeproj

folder="Release"
project_name="mycustomlib"
lib_name="libmycustom"
name_simulator=$lib_name"_simulator"
name_device=$lib_name"_device"

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -target $project_name build
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -sdk iphonesimulator -target $project_name build
rm -r build/Production-SDK
cp -r build/$folder-iphoneos build/Production-SDK
cp build/$folder-iphonesimulator/$lib_name.a build/Production-SDK/$name_simulator.a
mv build/Production-SDK/$lib_name.a build/Production-SDK/$name_device.a
cd build/Production-SDK/
lipo -create $name_simulator.a $name_device.a -output $lib_name.a
rm $name_device.a
rm $name_simulator.a
file $lib_name.a
open .

In the build.sh , folder is your release folder name. zbar is using Distribution-iphoneos and Distribution-iphonesimulator .

If you using your own , it may be Release-iphoneos and Release-iphonesimulator. lib_name is your .a library name. In Zbar , it’s using libzbar . project_name is your traget to build. Sometime , lib_name and project_name is different in the project.

After chasing this two, you can run in terminal like

sh ./build.sh

Need to wait until finishing the build and then it will show your lib.a file. In the terminal , you can check , it’s showing what platform are supporting.

If it doesn’t support i386,x86_64,armv7,arm64 , you need to open your project. Go to Build Settings -> Valid Archtectures. Add all architectures like following

  • armv7
  • armv7s
  • i386
  • arm64
  • x86_64

Don’t forget to change Build Active Architecture Only to NO

Xcode 5 Build With Post Action

I build the library(SDK) file for iOS. I have a one problem. That is to support both simulator and device. After building both simulator and device , I need to do

  1. copy the files from Debug-iphoneos folder to release folder
  2. change mylib.a to mylibDevice.a
  3. copy mylib.a from Debug-iphonesimulator folder to release folder
  4. change mylibSimulator.a
  5. After that merge with lipo -create mylibDevice.a mylibSimulator.a -output mylib.a

I always need to do when release the library.

It’s really troublesome for doing many time.

So, I decided to create the script in post-action.

Go to the Product > Scheme > Edit Scheme.

And then Duplicate Scheme because we will only do post action when it ready to release. Give a name , release build.

Chose the release build scheme and Build > Post Action.

post action

Click the + icon.

plus icon

Need to choose the target build in Provide build setting from .

I create a script like that

rm -r ~/Desktop/mylib
mkdir ~/Desktop/mylib
mkdir ~/Desktop/mylib/Device+Simulator
mkdir ~/Desktop/mylib/Device


cd ${BUILT_PRODUCTS_DIR}
cd ..
cd Debug-iphoneos
cp mylib.a ~/Desktop/eb/Device+Simulator/mylibDevice.a
cp -r mylib.bundle ~/Desktop/eb/Device+Simulator/mylib.bundle
cp -r usr ~/Desktop/eb/Device+Simulator/usr

cp mylib.a ~/Desktop/eb/Device/mylib.a
cp -r mylib.bundle ~/Desktop/eb/Device/mylib.bundle
cp -r usr ~/Desktop/eb/Device/usr

cd ..
cd Debug-iphonesimulator
cp mylib.a ~/Desktop/eb/Device+Simulator/mylibSimulator.a

cd ~/Desktop/mylib/Device+Simulator
lipo -create mylibDevice.a mylibSimulator.a -output mylib.a
rm mylibSimulator.a
rm mylibDevice.a

After ready to publish , I built the app that use mylib static library project for both device and simulator.

And then choose the release build and build again.

After that I got mylib static library on my desktop. Nothing to do anymore and ready to use.