Fastlane自动打包ipa

Fastlane自动打包ipa

初始化fastlane

终端cd到项目根目录,执行fastlane init命令。在根目录下会自动创建Appfile和Fastfile两个文件。Appfile配置APP的基本信息,如appID, AppleID,teamID……详细信息查看https://docs.fastlane.tools/advanced/#appfile。Fastfile就是具体配置fastlane自动化任务的地方。

在Appfile中编写代码

复制以下代码到Fastfile中:

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
  desc "内测版本"
  lane :beta do
    puts("开始打包ipa文件");

    # 项目的Target名称
    target_name = "RNWithRedux";
    xcproject_path = "./" + target_name + ".xcodeproj"

    # 自动
    increment_build_number(xcodeproj: xcproject_path)

    # 可选值patch minor major
    increment_version_number(bump_type: "patch", xcodeproj: xcproject_path)

    # 设置应用名称
    app_name = "功夫熊猫";
    set_info_plist_value(path: "./RNWithRedux/Info.plist", key: "CFBundleDisplayName", value: app_name)

    # 打包ipa
    gym;

    # 获取ipa路径
    ipa_file_path = lane_context[SharedValues::IPA_OUTPUT_PATH]

    # 获取ipa基本信息
    app_name = get_ipa_info_plist_value(ipa: ipa_file_path, key: "CFBundleDisplayName")
    package_name = get_ipa_info_plist_value(ipa: ipa_file_path, key: "CFBundleIdentifier")
    version_name = get_ipa_info_plist_value(ipa: ipa_file_path, key: "CFBundleShortVersionString")
    version_code = get_ipa_info_plist_value(ipa: ipa_file_path, key: "CFBundleVersion")
    puts("iPA基本信息: app_name = " + app_name);
    puts("iPA基本信息: package_name = " + package_name);
    puts("iPA基本信息: version_name = " + version_name);
    puts("iPA基本信息: version_code = " + version_code);

    # 获取fastlane文件夹
    fastlane_directory = Dir.pwd

    # 在fastlane/packages文件夹下创建【应用名称+版本名称】的文件夹
    package_version_directory = app_name + version_name;
    create_package_version_dir_command = fastlane_directory +"/packages/" + package_version_directory;
    sh("mkdir", "-p", create_package_version_dir_command);

    # 重命名ipa
    ipa_name = app_name + "_" + version_code + "_" + version_name + ".ipa";
    ipa_path = fastlane_directory + "/packages/" + package_version_directory + "/" +  ipa_name;
    sh("mv", ipa_file_path, ipa_path);

    # 重命名dSYM文件
    dSYM_file_name = target_name + ".app.dSYM.zip";
    dSYM_file_path = fastlane_directory + "/packages/" + dSYM_file_name;
    dSYM_file_name = app_name + "_" + version_code + "_" + version_name + ".app.dSYM.zip";
    dSYM_path = fastlane_directory + "/packages/" + package_version_directory + "/" + dSYM_file_name;
    sh("mv", dSYM_file_path, dSYM_path);
    sh("open", fastlane_directory + "/packages");

    puts("ipa保存路径:" + ipa_path);
    say("iOS打包完成");
    notification(title: "iOS打包完成", message: "ipa路径:" + ipa_path);
  end
end

根据项目具体需求修改target_nameapp_name

自动签名

在Fastfile中,我们使用了gym来实现自动打包任务。若要实现自动签名,还需要执行bundle exec fastlane gym init命令,在【fastlane】目录下创建【Gymfile】文件。然后在Gymfile中配置,项目信息,下面是我在Gymfile中的配置信息:

scheme("RNWithFlowAndRedux")
silent(true)
clean(true)
output_directory("./fastlane/packages")

export_options({
  method: "ad-hoc"
})

打包ipa

在终端中执行bundle exec fastlane beta,ipa文件就自动打包成ipa文件了。

遇到的问题及解决方案

iOS项目的版本名称默认为"1.0",fastlane在自动增加版本号时,要求其格式为"1.x.x"的形式,所以在执行打包命前,需要在Xcode中打开项目,在【TARGETS】 - 【General】- 【Identity】- 【Version】中填入"1.0.0"。

另外,在React Native项目中,会同时创建一个tvOS的Target,版本号也需要改成"1.0.0"

你可能感兴趣的:(Fastlane自动打包ipa)