Carthage enables projects to support and use third-party static libraries as dynamic libraries
Carthage is introduced
For specific use, you can check the official website, the document address github.com/Carthage/Ca… . If you don’t understand English, we can see a XiaGuan article translation: www.jianshu.com/p/e0c0ceee8… .
Why you want to use Carthage, what are the advantages over CocoaPods and how to install it? Please refer to the official website for details.
Make your project support Carthage
Configuration items
1. Create the framework
Create a Target on the existing project. Project -> File -> New -> Target, select Framework and name it CarthageTest
Carthagetestsdk.h. This file tells other people which header files we refer to. We don’t want it to automatically generate files
2. Configure your framework
- Choose your project
- Select the Target where the framework is located
- Select Build Phases
- Click on the + sign in the lower left corner of Header to add the Header file you want to expose to Public (by default, add it to Project)
- Add the implemented.m file to Compile source
Modify the Deployment Target for the framework
Changed to the same supported version as the project, 8.0 on my side. It is important to pay attention to this, otherwise, the compilation will report an error, and when the compilation does not report an error, the lower version of the system will not install. Error similar to:
ERROR: Install failed. Got error “ApplicationVerificationFailed” with code 0xe8008019: Failed to verify code signature of /private/var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.xY9xAg/extracted/Payload/Spec.app/Frameworks /MGLiveDetect.framework : 0xe8008019 (The application does not have a valid signature.)Build step ‘Execute shell’ marked build as failureFinished: FAILURE
Change the name of the framework
You can ignore it without changing it. Project Target -> Build Setting -> Product Module Name/Produce Name
5. Configure categories and support Bitcode
- If you use a category, you need to add it to Build Settings Linking Other Linker Flags
-all_load
- If you want your project to support Bitcode, you need to add it to Other C Flags
-fembed-bitcode
None of the above is required, so you can skip this step and skip to step 5.
6. Share your project
Manager Schemes and check Shared for CarthageTestSDK
Local validation
So at this point, basically you’ve done it, so let’s verify, let’s CD into the project folder, run it
$ carthage build --no-skip-current
Copy the code
After the command is executed, you will notice that there is a new Carthage folder in your project folder
1 | Carthage->Build->iOS->CarthageTestSDK.framework |
---|
This is the framework we need, so let’s verify it locally.
1. Drag the framework into the project and click Copy
2. Add dynamic libraries
Target ->Build Phases -> upper left + sign ->New Copy Files Phase Then under Copy Files Destination select Frameworks -> add your own dynamic library
3. Test verification is successful
Let’s compile first, no error, import header file, normal output, compile successfully. Verification succeeded.
4. Post and Tag
You don’t need to upload your code to the Carthage -> Build directory.
Git tag 0.1.0 Git pushCopy the code
Now that your library supports Carthage, you can let other developers use Carthage to manage your project dependencies.
The use of Carthage
1. Go to all the project folders first
$CD Project pathCopy the code
2. Create an empty Carthage file
$ touch Cartfile
Copy the code
3, Edit cartfile file and add the frames to depend on
github "https://github.com/AFNetworking/AFNetworking"
Copy the code
The specified version can do this
Making "https://github.com/AFNetworking/AFNetworking" ~ > 1.1.9Copy the code
4. Save and close cartfile file for installation
$ carthage update --no-use-binaries --platform ios
Copy the code
-- Platform ios: Only the ios platform is availableCopy the code
After installation, a folder called Carthage appears in the root directory containing the Build and Checkouts folders.
Build: The framework package in the iOS directory needs to be referenced.
Checkouts: Source code from Github, so in theory you can make any changes to the source code in this folder and then execute Carthage build again to package the corresponding framework based on the source code. Note that the source code is overwritten every time you perform a Carthage update. So you can add exceptions to prevent overwriting!!!! if you have special changes to make important
5, Project Target -> Build Setting -> Search Paths -> Framework Search Paths add This step will appear by default after the Carthage update. If so, skip to step 6.
$(PROJECT_DIR)/Carthage/Build/iOS
Copy the code
6, Target -> General at the bottom of the Linked Frameworks and Libraries manually Add [Add Other…] The framework you need.
7, Project Target -> Build Phases -> ‘+’ -> New Run Script Phase This step can not operate, directly operate step 8; Steps 7 and 8 require only one of the steps.
/usr/local/bin/Carthage copy-frameworks
Copy the code
To pay attention to at the same time, add the input Files), such as: $(SRCROOT)/Carthage/Build/iOS/AFNetworking framework.
Dyld: Library not loaded error if input Files are not added
8. [Note: If you don’t want to add a path through a script and use [input Files], you can add a path through [Copy File]. Project Target -> Build Phases -> ‘+’ -> New Copy File Phase
A third-party static library becomes a dynamic library
Since dynamic libraries do not increase the ipA package size, it is necessary to convert third-party static libraries into dynamic libraries. Below Tencent’s face recognition SDK to do verification. Website document: cloud.tencent.com/document/pr… .
Need SDK, you can directly find Tencent’s relevant personnel to take. I got the SDK package (iOS_ocr_sdk_v2.2.0), it can run directly, related code structure is as follows:
The following steps are similar to the above steps to get your project to support Carthage. See the illustration above for details.
1. Create the Framework
Create a Target on the existing project. Project -> File -> New -> Target, select Framework, name it WBOCRFramework, and delete the created extra WbocrFramework.h
2. Publish the SDK headers
Pull out the wBocrService. framework header file and place it in the libs folder. Note that although it is pulled out, the file path is the same as before.
Configure your framework
- Choose your project
- Select the Target where the framework is located
- Select Build Phases
- Click on the + sign in the lower left corner of Header to add the Header file you want to expose to Public (by default, add it to Project)
- Add the implemented.m file to Compile source. I don’t have a.m file, so I can just ignore it
- Add all liBS files to the Link Binary With Libraries directory. Add all dependent system frameworks to the Link Binary With Libraries directory.
Modify the Deployment Target for the framework
Changed to the same supported version as the project, 8.0 on my side. It is important to pay attention to this, otherwise, the compilation will report an error, and when the compilation does not report an error, the lower version of the system will not install. Error similar to:
ERROR: Install failed. Got error “ApplicationVerificationFailed” with code 0xe8008019: Failed to verify code signature of /private/var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.xY9xAg/extracted/Payload/Spec.app/Frameworks /MGLiveDetect.framework : 0xe8008019 (The application does not have a valid signature.)Build step ‘Execute shell’ marked build as failureFinished: FAILURE
Change the name of the framework
You can ignore it without changing it. Project Target -> Build Setting -> Product Module Name/Produce Name
6. Configure categories and support Bitcode
- Add this to Other Linker Flags for Build Settings Linking
-all_load
- The project needs to support bitcode, which needs to be added in Other C Flags
-fembed-bitcode
7. Share your project
Go to Manager Schemes and check shared for WBOCRFrame
8. Build and validate the framework
So at this point, basically you’ve done it, so let’s verify, let’s CD into the project folder, run it
$ carthage build --no-skip-current
Copy the code
However, we found that the report was wrong. On the terminal, it tells us, where is the error log output
Next, we go to the error log by looking at the log because the librecdetect.a file needs to support Bitcode.
The simplest solution is to turn on BitCode, as shown below:
After executing this command, you will notice that you have a Carthage folder in your project folder. This is the framework we need, so let’s verify locally. Follow the steps above to get your project to support Carthage.
Existing problems
1. The Bundle image resource is missing
Wbocrservice.bundle was added to the wbocrService.bundle. Through code verification analysis, it turns out that the SDK loaded the image, write dead from the root directory to read, written as follows:
But after we packaged it as a dynamic library, the Bundle file was no longer in the root directory, so we couldn’t find it at all.
Check out the github open source library MJRefresh (github.com/CoderMJLee/…) First read the Bundle path of the existing class and then find the path of the image. This way, bundle image resources are not lost no matter how many layers are encapsulated.
We can write it in an NSBundle category like this:
At present, through communication with Tencent, the problem of resource packs has been solved, and the solutions are as follows:
The SDK adds a judgment about resource bundles:
Check whether wbocrService. bundle exists in the main bundle
If 1 cannot be found, it will look for wbocrService.bundle in the directory corresponding to the SDK executable
In the case of dynamic library integration, resources will be in the second location, but it is important to note that the resource bundle must be added to the dynamic library
Problems encountered
1.Dyld: Library not loaded
dyld: Library not loaded: @rpath/CarthageTestSDK.framework/CarthageTestSDK
Referenced from: /Users/vipshop/Library/Developer/CoreSimulator/Devices/948C970C-2338-4BCB-A62D-E28A71C7A47B/data/Containers/Bundle/Appli cation/AB8C53FC-9779-44EA-8E23-23E1E63C6E89/PodTest.app/PodTest
Reason: image not found
Note that Copy File defaults to Resources and needs to be changed to Framework
Reproduced in: www.cnblogs.com/jys509/p/10…