xcodebuild 不会编译项目,除非它使用 Xcode 打开至少一次用于 cocoapods 集成项目

xcodebuild is not compiling the project unless it is opened using Xcode atleast only once for cocoapods integrated project

我有一个项目 cocoa pods。

这是我用来构建项目的命令。

/usr/bin/xcodebuild -scheme Jenkins -workspace /Users/Shared/Jenkins/Documents/Jenkins/Jenkins2/Jenkins.xcworkspace -configuration Release clean build CONFIGURATION_BUILD_DIR=/Users/Shared/Jenkins/Documents/JenkinsTestNuu/app 'CODE_SIGN_IDENTITY=iPhone Distribution: XXXX yay (3G5FKTZJ2K)' PRODUCT_BUNDLE_IDENTIFIER=com.XXXX.two PROVISIONING_PROFILE=6e6506e9-8233-4886-9084-ce21e8f8bbae

以上脚本只有在使用 Xcode 打开项目至少一次之后才能正常工作,之后 Xcode 可以关闭没有问题。

如果项目还没有打开那么如果我运行脚本 车轮在下面旋转,永远没有任何进展,例如下图

如果它打开一次而不是下面的纺车,文本将显示在下面

=== CLEAN TARGET XWebView OF PROJECT Pods WITH CONFIGURATION Release ===

Check dependencies

Clean.Remove clean /Users/Shared/Jenkins/Documents/JenkinsTestNuu/app/XWebView.framework.dSYM builtin-rm -rf /Users/Shared/Jenkins/Documents/JenkinsTestNuu/app/XWebView.framework.dSYM

Clean.Remove clean /Users/Shared/Jenkins/Library/Developer/Xcode/DerivedData/appanme-bqjwbjcqisegldeaonpytprisnig/Build/Intermediates/Pods.build/Release-iphoneos/XWebView.build builtin-rm -rf /Users/Shared/Jenkins/Library/Developer/Xcode/DerivedData/appanme-bqjwbjcqisegldeaonpytprisnig/Build/Intermediates/Pods.build/Release-iphoneos/XWebView.build

Clean.Remove clean /Users/Shared/Jenkins/Documents/JenkinsTestNuu/app/XWebView.framework builtin-rm -rf /Users/Shared/Jenkins/Documents/JenkinsTestNuu/app/XWebView.framework

=== CLEAN TARGET Pods OF PROJECT Pods WITH CONFIGURATION Release ===

Check dependencies etc...

在任何非cocoapods项目中都没有观察到该问题。

那么是什么原因造成的,如何解决呢?

您需要运行 pod install 在构建项目之前,以便在 Jenkins 工作区内 Podfile 中指定的 CocoaPods fetches the Pods

为什么会这样?

快速 diffMerge 工具 分析 由 Xcode 打开的项目与 未打开的相同项目到目前为止 Xcode 打开

从这里我们可以看到,在 .xcodeproj

中,一旦 Xcode 打开,就会创建许多与方案相关的文件

so xcodebuild 通过使用与方案相关的元数据创建 .app,但由于没有针对其失败的方案进行构建

如何解决?

由于缺少无法构建的元数据,因此需要打开 Xcode 以便 Xcode 自动创建文件,因此会有一些方案建立反对。

但是当您打开 Xcode 时,此方案相关文件会在 xcuserdata 下创建,这是针对特定用户的。即每个用户都有自己的文件,用于保存打开的状态文件夹、最后打开的文件等...

把这个文件留在我们身边是不明智的。

勾选Manage Schemes

下的Shared复选框即可解决问题

这会将 schemes 从您的个人 xcuserdata 移出到 shared 文件夹 中,可以通过源代码管理提交,您可以安全地忽略 xcuserdata 并将共享文件夹保留在 源代码管理

现在我们可以在不打开 Xcode 的情况下构建代码,即使只打开一次。

品牌化UI,构建设置和功能

  • UI
    • 应用程序图标和其他图标
    • iTunes 图稿
  • 构建设置
    • 应用名称
    • 捆绑包标识符
    • 配置文件
    • 代码签名身份
  • 功能正常
    • 品牌特定 URL(登录、注销、资源获取等...)

使用终端

Branding.sh

   #Author: Durai Amuthan(h.duraiamuthan@gmail.com)
#This is to achieve multiple branding of an iOS app by configuring the variables below

#************ Configuring the brand starts ************

#Directory path where .xcworkspace or .xcodeproj exists
PathOfProjectDirectory=/Users/Shared/Jenkins/Documents/JenkinsTestNuu/New/

#Path where info.plist exists
PathOfInfoPlist=$PathOfProjectDirectory/XxYyZz

#Path to icons where new iTunesArtwork and application icon exixts
#Note: Make sure proper naming conventions of file has been followed
PathOfNewIcons=/Users/Shared/Jenkins/Documents/icons-two

#Path to asset resource where you have kept your application icon.
PathOfAppIconSet=$PathOfProjectDirectory/XxYyZz/Icon.xcassets/AppIcon.appiconset

#Path where do you want the .app file has to be kept
PathToApp=/Users/Shared/Jenkins/Documents/JenkinsTestNuu/app

#Path where do you want the .ipa file has to kept
PathToIpa=/Users/Shared/Jenkins/Documents/JenkinsTestNuu/ipa

#Cocoapods project or project that involves more than one modules are scheme based
isWorkspaceBased=true

#Path of the Project (.xcodeproj) - applicable for workspace(.xcworkspace) based project
PathofProjectFile=$PathOfProjectDirectory/XxYyZz.xcodeproj

#Path of the Workspace (.xcworkspace)
PathofWorkspaceFile=$PathOfProjectDirectory/XxYyZz.xcworkspace

#Name of the target - applicable only for non-workspace(.xcodeproj)  based projects
Target=XxYyZz

#Scheme of the iOS app
Scheme=XxYyZz

#To ascertain Cocoapods has been used or not
isCocoaPodsBased=true

#Configuration of the app (Debug -(Development) or Release(Adhoc or Distribution))
Config=Release

#For giving access to signing idetity found in KeyChain
LoginKeychainPath=/Users/Shared/Jenkins/Library/Keychains/login.keychain
LoginKeyChainPassword=xxyyzz

#Name of the code signing identity.You can find the name in Keychain or xcode build setting
CodeSigningIdentity='iPhone Distribution: Xx Yy Zz Limited (3Z5MHUYJ2L)'

#Path of the provisioning profile
PathToMobileProvision=/Users/Shared/Jenkins/Desktop/BrandingTest.mobileprovision

#UUID value found inside Provisioning profile has to be given
#Do not forget to install provisiong profile in the system
ProvisioningProfileIdentity=6e6506e9-8233-4886-9084-zf21e8f8bbae

#Bundle identifier of the app
BundleIdentifier=com.xxyy.zz

#AppVersion of the app
AppVersion=2.2.2

#App Name
Appname=Two

#************ Configuring the brand ends ************

#** Creatting the build based on configuration starts **

cd $PathOfInfoPlist
echo "****************** Setting App Name ******************"
/usr/libexec/PlistBuddy -c "Set :CFBundleName $Appname" info.plist
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName $Appname" info.plist
echo "app name has been set as $Appname"

cd $PathOfProjectDirectory
echo "****************** Setting AppVersion ******************"
/usr/bin/agvtool new-marketing-AppVersion $AppVersion
/usr/bin/agvtool new-AppVersion -all $AppVersion

echo "****************** Changing app icons & iTunes Artwork ******************"
cp -R $PathOfNewIcons/*.png $PathOfAppIconSet
echo "App icons has been changed at $PathOfNewIcons"
cp -R $PathOfNewIcons/iTunesArtwork@2x $PathOfProjectDirectory/XxYyZz
cp -R $PathOfNewIcons/iTunesArtwork $PathOfProjectDirectory/XxYyZz
echo "iTunesArtwork has been changed at $PathOfProjectDirectory"

#Unlock login keychain
security unlock-keychain -p $LoginKeyChainPassword $LoginKeychainPath
if $isCocoaPodsBased == 'true'
then
echo "****************** Installing Cocoapods **********************"
/usr/local/bin/pod install
echo "Cocoapods has been installed"
fi

echo "****************** Creating .app ******************"
if $isWorkspaceBased == 'true'
then
/usr/bin/xcodebuild -scheme $Scheme -workspace $PathofWorkspaceFile -configuration $Config clean build CONFIGURATION_BUILD_DIR=$PathToApp "CODE_SIGN_IDENTITY=$CodeSigningIdentity" "PRODUCT_BUNDLE_IDENTIFIER=$BundleIdentifier" "PROVISIONING_PROFILE=$ProvisioningProfileIdentity"
else
/usr/bin/xcodebuild -target $Target -project $PathofProjectFile -configuration $Config clean build CONFIGURATION_BUILD_DIR=$PathToApp "CODE_SIGN_IDENTITY=$CodeSigningIdentity" "PRODUCT_BUNDLE_IDENTIFIER=$BundleIdentifier" "PROVISIONING_PROFILE=$ProvisioningProfileIdentity"
fi
echo ".app has been generated at $PathToApp"

echo "****************** Creating .ipa *******************"
/usr/bin/xcrun -sdk iphoneos PackageApplication -v $PathToApp/XxYyZz.app -o $PathToIpa/$Appname.ipa --embed $PathToMobileProvision --sign "$CodeSigningIdentity"
echo "$Appname.ipa has been generated at $PathToIpa"

#** Creatting the build based on configuration ends **

该文件是自我描述的,您可以轻松理解。 只需在文件中配置变量的值并像下面这样调用它

sh Branding.sh

仅供参考:

如果除了App Icon和iTunesArtwork你还想改变一些其他图标 使用 cp 命令,例如

cp path/to/source path/to/destination

要了解更多信息,请执行 cp man

使用以上文件,您可以为 UI 和构建设置.

进行 品牌化

对于功能性品牌,您必须保留

  • 品牌特定 URL

  • 品牌相关的其他输入

在单独的 plist 文件中,以便在构建应用程序时也可以根据各自的品牌更改这些内容

在编码方面,您可以自定义您的应用程序以像这样从 plist 中读取值

函数定义:

func getPlistFile()->Dictionary<String,AnyObject>? {
        var dictPlistFile:Dictionary<String,AnyObject>?
        if let path = NSBundle.mainBundle().pathForResource("plistfile", ofType: "plist") {
            if let dictValue = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {
                dictPlistFile=dictValue
            }
        }
        return dictPlistFile
    }

函数调用:

var Value=getPlistFile()?["Key"]

您可以在构建应用程序时使用 PlistBuddy 根据品牌更改密钥的值

语法如下

/usr/libexec/PlistBuddy -c "Set :Key Value" plistfile.plist

使用詹金斯

我们可以在 jenkins

中有效地重用 shell 脚本

1.You 必须使用 Add Parameter.. . 就像下面的屏幕截图一样,我已经为一个变量做了这样你必须为所有其他变量做的

2.Choose 构建步骤中执行shell

3.Copy 根据配置创建构建开始根据配置创建构建结束[=212]之间的脚本=] 并粘贴到 执行 Shell

注:

  • 资源规则

    在通过非 xcode 接口构建和打包应用程序时,在某些版本中存在关于 Xcode 的 ResourceRules 的已知错误。

    因此必须 运行 一次才能停用 资源规则 路径 xcode 中的验证.资源规则路径是 deprecated feature 和 apple doesn't accept apps that comes with resource rules 但是如果我们构建一个应用程序而不使用 Xcode 表示未找到资源规则的验证错误将出现以反驳我们必须 运行 脚本一次。

xcode_fix_PackageApplicationResourceRules.sh

#!/bin/sh
# A script to patch xcrun PackageInstallation so that it doesn't use the deprecated --resource-rules
# See "Do not use the --resource-rules flag or ResourceRules.plist. They have been obsoleted and will be rejected."
#   under https://developer.apple.com/library/mac/technotes/tn2206/_index.html#//apple_ref/doc/uid/DTS40007919-CH1-TNTAG205
# Reported as Apple bug #19384243
# 
# should be run as a user who can modify the PackageApplication file

xcodedir=

function usage {
        # FIXME we cannot parse args properly because 2 are optional...
        echo "USAGE: [=16=] xcodedir"
        echo "  xcodedir: an install dir like /Application/Xcode6.1.1.app"
}

if [[ $# -ne 1 ]]; then
        echo "ERROR: invalid number of arguments"
        usage
        exit -1 
fi

pi="$xcodedir/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication"
piorig="$piOrig"

if [[ ! -f "$pi" ]]; then
    echo "$pi file not found. Invalid argument ?"
    usage
    exit -1
fi

grep resource-rules "$pi" 
if [[ $? -ne 0 ]]; then
    echo "PackageApplication doesn't use resource-rules. Skipping"
    exit 0
fi

if [[ -f "$piorig" ]]; then
    echo "Backup file $piorig already exist. Aborting"
    exit -1
fi

perl  -p -i'Orig' -e 'BEGIN{undef $/;} s/,resource-rules(.*sign}).*ResourceRules.plist"//smg' "$pi" 
echo $?
  • 解锁钥匙串

    每当您 运行 Branding.sh 在终端中时,它会提示用户名和密码作为访问 系统钥匙串

    每当你 运行 Jenkins 中的 Job 时,你将得到“不允许用户交互”错误

    因此,要解决此问题,您必须按照以下步骤操作

    • 打开钥匙串访问
    • 右击私钥
    • Select"Get Info"
    • Select "Access Control" 选项卡
    • 点击"Allow all applications to access this item"
    • 点击"Save Changes"
    • 输入密码
  • 配置文件

    如果您遇到“未找到匹配的配置文件”,请确保您已双击并通过 Xcode.

    安装了它

    安装后,您会在 ~/Library/MobileDevice/Provisioning 配置文件/[=25 中看到 UUID.mobileprovision =]

    此 UUID 是移动配置中的值,表示配置文件已安装。

希望对您有所帮助