You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在KMM共享库中自动自定义合成PodFile

KMM共享库iOS部署目标版本问题解决方法

问题说明

构建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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:15:30