Inject TEST macros into Pods
When we develop iOS applications, we often use CocoaPods
for third-party library management.
In addition to external third-party libraries, it is common for CocoaPods
to be used as a library management tool for individuals or companies.
We usually use macros such as DEBUG
for environment-sensitive branch compilation.
CocoaPods
provides DEBUG
macro injection by default to facilitate our local debugging.
However, in actual development projects, in addition to using the DEBUG
macro to identify local debugging, macros such as TEST
or QA
are also used to identify the test
build package.
At this time, we expect that the library in Pods can also inject TEST
macros on demand.
In fact, CocoaPods provides enough customization capabilities for project files.
In simple terms, you can do something like the following
Note that the use of
Debug
Configuration here is only for example, in practice, test packages often use different Configurations.
This part of the code is usually at the end of the
Podfile
. After the modification, you need to executepod install
to take effect.
1 | target 'XXXXXX' do |
We can check whether the configuration of the preprocessor macro (GCC_PREPROCESSOR_DEFINITIONS
) in the Targets
of Pods is as expected.
Here is the Project
configuration for Pods.
Here is the Targets
configuration of the Pods before the modification. Note that the DEBUG
macro is read from the Project
configuration using inheritance
.
Here is the Targets
configuration for the modified Pods.
The modification method here is to adopt the principle of modification with the smallest impact scope.
The affected area of the change is the Target
configuration of a specific part of the library in the Pods project.
You can also directly modify the Project
configuration of Pods, or all Target
configurations, as needed.
In addition, it should be noted that preprocessing
macros are only valid for third-party libraries compiled from source code, after all, they affect the compilation process.
Has no effect if the imported library is a compiled binary. In this case you need to consider contacting the person who exported the binary library to provide a different binary library.