美文网首页
XCode12制作Swift和OC混编静态库

XCode12制作Swift和OC混编静态库

作者: Bel李玉 | 来源:发表于2020-10-31 15:39 被阅读0次

    在本篇文章中主要来记录使用Xcode12来制作静态库的过程,及出现的问题。静态库分为两种.framework文件和.a文件,在本文中,我们以.framework为例。

    制作静态库

    1,新建framework工程,Language一栏我们选择Objective-C

    framework.png

    2,在工程中分别新建一个OC类和Swift

    OC类: LYPerson:

    @interface LYPerson : NSObject
    
    + (void) run;
    @end
    #import "LYPerson.h"
    
    @implementation LYPerson
    
    +(void)run {
        NSLog(@"正在跑步......");
    }
    @end
    

    Swift类:Calculator

    import Foundation
    
    public class Calculator {
        
        public static func add(_ sum1: Int, _ sum2: Int) -> Int {
            return sum1 + sum2;
        }
    }
    
    • swift class需要指定类为 public class,其方法也应为public,才可以被外界引用。

    • OC class 需要将暴露给外界使用的类设置为public

      OC类设置为public.jpg
    • 暴露的OC类倒入库的头文件LYKitDemo

    //! Project version number for LYKitDemo.
    FOUNDATION_EXPORT double LYKitDemoVersionNumber;
    
    //! Project version string for LYKitDemo.
    FOUNDATION_EXPORT const unsigned char LYKitDemoVersionString[];
    
    // In this header, you should import all the public headers of your framework using statements like #import <LYKitDemo/PublicHeader.h>
    
    #import "LYPerson.h"
    

    整体目录结构如下


    整体结构.jpg

    3,将项目的Build Configuration设置为 Release

    BuildConfiguration.jpg

    4,编译 simulator静态库
    选择任意一个模拟器,按下 command + B进行编译,编译成功后,在 Finder 中查看 LYKitDemo.framework

    模拟器.jpg
    5,查看 framework支持的架构
    lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo
    结果如下
    Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo are: i386 x86_64 arm64
    
    • Xcode12,通过模拟器编译出来的静态库支持i386,x86_64,arm64架构。

    6,编译真机静态库,选择Any iOS Device,按照步骤4中的步骤,在操作一边。并查看

    真机静态库.jpg
    同样使用lipo -info命令查看其支持的架构
     admin@localhost  ~/Desktop  lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo
    Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo are: armv7 arm64
    
    • 用真机编译出来的静态库,支持armv7 和 arm64架构。

    7, 将 模拟器真机下的静态库进行合并
    使用:lipo -create 静态库1的地址 静态库2的地址 -output 输出路径

     admin@localhost  ~/Desktop  lipo -create "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo" "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo" -output "/Users/admin/Desktop/IPA包/LYKitDemo"
    fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo and /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo have the same architectures (arm64) and can't be in the same fat output file
    

    在合并的时候发生了错误❌have the same architectures (arm64) and can't be in the same fat output file,是因为两个文件都支持arm64架构,我们需要重新编译一个不包含 arm64架构的模拟器静态库

    我们修改 Build Setting 中的 Excluded Architectures选项,将其值变为arm64

    模拟器下去除arm64架构.jpg

    再次编译,并查看其信息:

     lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo
    Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo are: i386 x86_64
    

    这样模拟器下的静态库只有 i386 和 x86_64架构,我们再次将模拟器和真机下的静态库进行合并:

    lipo -create "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo" "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo" -output "/Users/admin/Desktop/IPA包/LYKitDemo"
    

    这样我们能合并成功
    查看新的静态库的信息

     admin@localhost  ~/Desktop  lipo -info /Users/admin/Desktop/IPA包/LYKitDemo
    Architectures in the fat file: /Users/admin/Desktop/IPA包/LYKitDemo are: armv7 i386 x86_64 arm64
    

    8,使用 Run Script 合并完整的静态库信息
    8.1 在 Build Setting中新增 RunScript 选项

    新增runScript选项.png

    并将以下脚本信息写入

    if [ "${ACTION}" = "build" ]
    then
    INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}.framework
    
    DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework
    
    SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework
    
    
    if [ -d "${INSTALL_DIR}" ]
    then
    rm -rf "${INSTALL_DIR}"
    fi
    
    mkdir -p "${INSTALL_DIR}"
    
    cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
    #ditto "${DEVICE_DIR}/Headers" "${INSTALL_DIR}/Headers"
    
    lipo -create "${DEVICE_DIR}/${PROJECT_NAME}" "${SIMULATOR_DIR}/${PROJECT_NAME}" -output "${INSTALL_DIR}/${PROJECT_NAME}"
    
    #open "${DEVICE_DIR}"
    open "${SRCROOT}/Products"
    fi
    

    编译工程,就会得到合并后的静态库同时支持 模拟器和真机的静态库。

    脚本编译后的静态库.png

    9,将x86架构下的swiftModule拷贝至 modules文件中。
    modules的文件结构如下:

    modules结构.jpg

    常见错误

    dyld: Library not loaded: @rpath/LYKitDemo.framework/LYKitDemo
    Referenced from: /Users/admin/Library/Developer/CoreSimulator/Devices/39CF7B52-BCEE-4BB2-AF78-CCB581E06F91/data/Containers/Bundle/Application/CB27BBCF-D80B-44A9-A88D-7116E18C7CA9/LYCalculatorTest.app/LYCalculatorTest
    Reason: image not found

    需要将静态库由Do Not Embed 变为 Embed & Sign

    have the same architectures (arm64) and can't be in the same fat output file

    两个静态库包含了同一种架构,需要去除一个

    Could not find module 'LYKitDemo' for target 'x86_64-apple-ios-simulator'; found: arm64, armv7-apple

    这是因为静态库中,没有 x86swiftmodule文件。我们使用命令合并完两个静态库后,需要手动将 swiftmodule文件合并到一起。

    最后附上本文相关Demo: LYKitDemo :静态库工程,LYCalculatorTest:静态库使用工程。

    相关文章

      网友评论

          本文标题:XCode12制作Swift和OC混编静态库

          本文链接:https://www.haomeiwen.com/subject/bpdsvktx.html