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 execute pod install to take effect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
target 'XXXXXX' do
pod 'xxxx', '~> 1.1.1'
end

add_test_pod = ['React']

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if (config.name == 'Debug') and add_test_pod.include?(target.name)
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','TEST=1', 'QA=1']
end
end
end
end

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.
pods-project

Here is the Targets configuration of the Pods before the modification. Note that the DEBUG macro is read from the Project configuration using inheritance.
pods-targets-before

Here is the Targets configuration for the modified Pods.
pods-targets-after

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.