如何在KMM共享库中自动自定义合成PodFile
问题说明
构建KMM共享库时出现以下版本兼容警告:
shared/build/cocoapods/synthetic/IOS/Pods/Pods.xcodeproj: warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 10.0, but the range of supported deployment target versions is 11.0 to 16.1.99. (in target 'FirebaseCore' from project 'Pods')
当前共享模块的podspec已设置spec.ios.deployment_target = '14.1',但自动生成的合成Podfile未包含platform声明和post_install脚本,导致依赖库的部署目标版本不匹配。需要实现自动生成符合需求的Podfile,无需手动修改。
解决方案
通过配置KMM的Gradle CocoaPods插件,自动生成带指定内容的Podfile:
1. 修改共享模块的Gradle配置
根据你使用的Gradle脚本类型,在共享模块的build.gradle.kts或build.gradle中添加以下配置:
Kotlin DSL(build.gradle.kts)
kotlin { cocoapods { // 保留原有配置,新增以下内容 ios.deploymentTarget = "14.1" // 自定义生成的Podfile内容 podfile { // 追加platform声明 append("platform :ios, '14.1'") // 追加post_install钩子脚本 append(""" post_install do |installer| installer.pods_project.targets.each do |target| puts target.name target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.1' config.build_settings['ENABLE_BITCODE'] = 'Yes' config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym' config.build_settings['VALID_ARCHS[sdk=iphonesimulator*]'] = 'x86_64' end end end """.trimIndent()) } } }
Groovy(build.gradle)
kotlin { cocoapods { // 保留原有配置,新增以下内容 ios.deploymentTarget = "14.1" // 自定义生成的Podfile内容 podfile { // 追加platform声明 append("platform :ios, '14.1'") // 追加post_install钩子脚本 append(''' post_install do |installer| installer.pods_project.targets.each do |target| puts target.name target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.1' config.build_settings['ENABLE_BITCODE'] = 'Yes' config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym' config.build_settings['VALID_ARCHS[sdk=iphonesimulator*]'] = 'x86_64' end end end ''') } } }
2. 重新生成Podfile
执行Gradle的syncFramework任务,或在iOS项目目录下运行pod install,此时生成的shared/build/cocoapods/synthetic/IOS/Podfile会自动包含platform声明和post_install脚本,完全符合需求样式,无需手动修改。
补充说明
- podspec中的
ios.deployment_target仅用于约束依赖库的版本范围,不会直接写入合成Podfile的platform字段,也无法添加post_install钩子,因此必须通过Gradle插件配置实现。 - 上述配置会将自定义内容追加到插件默认生成的Podfile内容之后,确保所有依赖和配置生效。
内容的提问来源于stack exchange,提问作者Rahul

