iOS解决CocoaPods安装OpenSSL失败的问题

前言

前阵子在搞持续构建,购买了黑苹果主机服务器,实现了内网穿透,在服务器上搭建了 Jenkins,iOS ipa 打包使用 fastlane,最后构建任务跑起来,在 fastlane pod install 这一步失败了。CocoaPods 安装 OpenSSL 出错,换了好几个版本的 OpenSSL 都不行,最终在 OpenSSL 的 Github Issues 里找到了解决办法。

错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
[!] /bin/bash -c
set -e
VERSION="1.0.2j"
SDKVERSION=`xcrun --sdk iphoneos --show-sdk-version 2> /dev/null`
MIN_SDK_VERSION_FLAG="-miphoneos-version-min=7.0"
BASEPATH="${PWD}"
CURRENTPATH="/tmp/openssl"
ARCHS="i386 x86_64 armv7 armv7s arm64"
DEVELOPER=`xcode-select -print-path`
mkdir -p "${CURRENTPATH}"
mkdir -p "${CURRENTPATH}/bin"
cp "file.tgz" "${CURRENTPATH}/file.tgz"
cd "${CURRENTPATH}"
tar -xzf file.tgz
cd "openssl-${VERSION}"
for ARCH in ${ARCHS}
do
CONFIGURE_FOR="iphoneos-cross"
if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ] ;
then
PLATFORM="iPhoneSimulator"
if [ "${ARCH}" == "x86_64" ] ;
then
CONFIGURE_FOR="darwin64-x86_64-cc"
fi
else
sed -ie "s!static volatile sig_atomic_t intr_signal;!static volatile intr_signal;!" "crypto/ui/ui_openssl.c"
PLATFORM="iPhoneOS"
fi
export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
export CROSS_SDK="${PLATFORM}${SDKVERSION}.sdk"
echo "Building openssl-${VERSION} for ${PLATFORM} ${SDKVERSION} ${ARCH}"
echo "Please stand by..."
export CC="${DEVELOPER}/usr/bin/gcc -arch ${ARCH} ${MIN_SDK_VERSION_FLAG}"
mkdir -p "${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"
LOG="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/build-openssl-${VERSION}.log"
LIPO_LIBSSL="${LIPO_LIBSSL} ${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/libssl.a"
LIPO_LIBCRYPTO="${LIPO_LIBCRYPTO} ${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/libcrypto.a"
./Configure ${CONFIGURE_FOR} --openssldir="${CURRENTPATH}/bin/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" > "${LOG}" 2>&1
sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} !" "Makefile"
make >> "${LOG}" 2>&1
make all install_sw >> "${LOG}" 2>&1
make clean >> "${LOG}" 2>&1
done
echo "Build library..."
rm -rf "${BASEPATH}/lib/"
mkdir -p "${BASEPATH}/lib/"
lipo -create ${LIPO_LIBSSL} -output "${BASEPATH}/lib/libssl.a"
lipo -create ${LIPO_LIBCRYPTO} -output "${BASEPATH}/lib/libcrypto.a"
echo "Copying headers..."
rm -rf "${BASEPATH}/opensslIncludes/"
mkdir -p "${BASEPATH}/opensslIncludes/"
cp -RL "${CURRENTPATH}/openssl-${VERSION}/include/openssl" "${BASEPATH}/opensslIncludes/"
cd "${BASEPATH}"
echo "Building done."
echo "Cleaning up..."
rm -rf "${CURRENTPATH}"
echo "Done."
Building openssl-1.0.2j for iPhoneSimulator 10.3 i386
Please stand by...

是不是看得一脸懵逼?CocoaPods 直接把一整段 bash script 当做 error 信息全部抛出来了,根本无从下手。

解决

使用 OpenSSL-Universal 代替 OpenSSL,直接修改 Podfile 即可。

1
2
#pod 'OpenSSL'
pod 'OpenSSL-Universal', '~> 1.0'

再次 pod install,发现居然可以无缝替换,结果 Build Success。

后话

最近顺丰大当家的 iOS 开发人员拉取我们源码的时候,也出现了pod install失败的问题。

于是我让他尝试把 OpenSSL-Universal 注释掉,用回 OpenSSL,果然他就编译成功了。
有的设备上用 OpenSSL,有的设备上却要用 OpenSSL-Universal,谁是谁非傻傻分不清。

pod install 还是有一些弊端的,以后做组件化开发,还是得抽一个 Third-Party 模块组件出来,专门放一些公共的依赖类库框架。

  • 本文作者: Bingo
  • 本文链接: https://blog.bingo.ren/56.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!