In my previous posts covering the Pipeline Templates I’ve discussed building a Xamarin.Forms apps for iOS, Android and Windows (UWP) and subsequently deploying them to AppCenter. In this post we’re going to look at doing the same with a Uno application. Given that Uno is built on top of the core Xamarin functionality, the process for both build and deploy should be fairly similar. Rather than just stepping through using the template, I’m going to cover creating a new Uno project and setting up the corresponding multi-stage pipeline to build and deploy to AppCenter.
Also, I’ve pushed v0.3.0 of the pipeline templates, which I’ll summarise at the end of this post.
Creating a Uno Application
Let’s get into it and start by creating a Uno application. Before proceeding, make sure you grab the latest Uno extension for Visual Studio that includes the project templates. From the Create a new project dialog, enter “uno” into the search box and then select the Cross-Platform App (Uno Platform) template.
Next, enter a name and location for your new application
When you hit the Create button, Visual Studio will generate a new solution with five projects, representing the head projects for iOS, Android, Windows (UWP) and Web (WASM), along with a shared project.
After creating the application, you should go through each platform and check that it builds and runs. The WASM project will take a while to build and run the first time as it needs to download some components first – don’t be alarmed it nothing appears to happen for a long time.
One thing I do like to tidy up for my applications is the build configurations. This is often overlooked by developers and then wonder why they have to wait around for say the Android project to build, when they have the UWP project selected as their start up project. Here are the Release configurations I have set for the different platforms
Building Uno Applications
We’ll go through each Uno application separately and I’ll endeavor to highlight typical pain points that developers face. If you’re using the pipeline_templates and you run into issues, message me on Twitter and I’ll help you out. Where there are common frustrations, I’ll work to improve the templates to make them easier to use.
Let’s start by creating a new pipeline in Azure DevOps. I’m not going to step through the pipeline wizard but for what we want to do, just select the appropriate source code respository and an empty YAML file. When you get to the YAML editor, enter the following.
trigger: none resources: repositories: - repository: builttoroam_templates type: github name: builttoroam/pipeline_templates ref: refs/tags/v0.3.0 endpoint: github_connection variables: - group: 'Uno Build Variables'
There are three parts to this inital YAML. Firstly, we’re disabling the CI trigger whilst we’re configuring the build. As we’ll be committing changes to both the build pipeline and source code, it’s as well to only have builds triggered when you’re ready, otherwise you’ll be continually cancelling builds.
Next, the resources section is where we pull in the pipeline templates. In this case we’re targeting the v0.3.0 release to ensure the templates don’t change and break our builds in the future.
Lastly, we’re pulling in the Uno Build Variables variable group. You can pick whatever name you want for this group but it needs to match the name of the variable group defined under the Library tab on Azure DevOps.
Android – Build
Starting with Android we’re going to add stages to the YAML file (note you only need to include the stages element once and then add the individual stages by referencing the corresponding template). Here’s the Android build
stages: - template: azure/mobile/[email protected]_templates parameters: stage_name: 'Build_Android' build_android: $(android_enabled) solution_filename: 'src/Apps/DotNet/Inspector.Uno.sln' solution_build_configuration: 'Release' build_number: '$(Build.BuildId)' full_version_number: '$(version_prefix).$(Build.BuildId)' artifact_name: 'inspector-build' artifact_folder: 'Android_output' application_package: 'Inspector-Uno-Android.aab' secure_file_keystore_filename: '$(android_keystore_filename)' keystore_alias: '$(android_keystore_alias)' keystore_password: '$(android_keystore_password)'
There are a few things you need to have setup for this build to work:
- solution_filename – make sure the path to your solution file is correct, relevant to the root of your code repository.
- $(version_prefix) – make sure the version_prefix variable exists in your variable group and has the format X.Y (eg 1.0)
- $(android_keystore_filename) – make sure the android_keystore_filename variable exists in your variable group and that it’s value matches the Secure file name of the keystore file uploaded to the Secure files under the Library in Azure DevOps.
- $(android_keystore_alias) – make sure the android_keystore_alias variable exists in your variable group and that it is the the alias that you specified when creating the keystore. This variable should be marked as private by clicking the lock beside it in the portal – this hides it both in the variable group editor in the portal and in the log files.
- $(android_keystore_password) – make sure the android_keystore_password variable exists in your variable group and that it is the password you specified when creating the keystore. Again this should be marked as private.
Frustratingly, the Android build didn’t just work for a couple of reasons:
- Firstly, there was an issue where one of the images has a file name that isn’t supported by the version of Visual Studio and/or Android tooling on the windows-latest build agent, resulting in the following error:
2020-02-08T06:04:55.7036355Z ##[error]src\Apps\DotNet\Uno\InspectorUno\InspectorUno\InspectorUno.Shared\Assets\Square44x44Logo.targetsize-24_altform-unplated.png(0,0): Error APT0003: Invalid file name: It must contain only [^a-zA-Z0-9_.]+.
I simply excluded the file Square44x44Logo.targetsize-24_altform-unplated.png from the shared project
- Secondly, the build agent include the Android NDK but doesn’t specify the appropriate environment variables (eg ANDROID_NDK_HOME), resulting in the following error:
2020-02-07T12:56:19.5428557Z ##[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2843,3): Error XA5101: Could not locate the Android NDK. Please make sure the Android NDK is installed in the Android SDK Manager, or if using a custom NDK path, please ensure the $(AndroidNdkDirectory) MSBuild property is set to the custom path.
The solution here was for me to update the build-xamarin-android.yml to include these environment variables. You shouldn’t run into this error if you’re using v0.3.0 of the pipeline templates.
Android – Deploy
Here’s the YAML for deploying the Android application to AppCenter
- template: azure/mobile/[email protected]_templates parameters: stage_name: 'Deploy_Android' depends_on: 'Build_Android' environment_name: 'Inspector-Alpha' artifact_name: 'inspector-build' artifact_folder: 'Android_output' application_package: 'Inspector-Uno-Android.aab' appcenter_service_connection: 'AppCenterInspectorCI' appcenter_organisation: 'thenickrandolph' appcenter_applicationid: 'Inspector-Uno-Alpha' appcenter_release_notes: 'Release from deploy pipeline' secure_file_keystore_filename: '$(android_keystore_filename)' keystore_alias: '$(android_keystore_alias)' keystore_password: '$(android_keystore_password)'
Again there are a few things you need to check here but most importantly for this to work you need to setup an application in AppCenter. I created a new application
After creating the application, look at the url that appears in the browser. The piece highlighted in green is the value you need to set for the appcenter_organisation and the yellow is for the appcenter_applicationid.
You also need to make sure you setup a Service Connection between Azure DevOps and AppCenter (in this case it was given the name ‘AppCenterInspectorCI’ and assigned to the appcenter_service_connection property)
The other properties you need to check are:
- depends_on – make sure this matches the stage_name property set on the android build stage
- artifact_name, artifact_folder and application_package – make sure these match the corresponding value on the android build stage
- secure_file_keystore_filename, keystore_alias and keystore_password – these should be set to the same as on the android build stage and are required so that the release process can extract and sign a fat apk from the aab file.
iOS – Build
Before we get into building your iOS application, let’s make sure you have both a signing certificate and a provisioning profile ready to go. If you have both of these, you can jump over the next two sections. These instructions can be stepped through on Windows, without the need for a Mac!
- Install OpenSSL (Download from https://slproweb.com/products/Win32OpenSSL.html)
- Open command prompt
- Add OpenSSL to path (alternatively you can permanently add it to your Path environment variable if you want to have it accessible across all command prompts)
set PATH=%PATH%;C:\Program Files\OpenSSL-Win64\bin\
- Generate the signing request
openssl genrsa -out ios_distribution.key 2048
openssl req -new -key ios_distribution.key -out CertificateSigningRequest.certSigningRequest
- Sign into the Apple Developer portal and go to Certificates (https://developer.apple.com/account/resources/certificates)
- Click the + button to start the process of adding a new certificate
- Select Apple Distribution from the list of certificate types and then Continue
- Use the Choose File link to select the CertificateSigningRequest.certSigningRequest file you created in the previous steps, and then Continue
- Important – Make sure you click the Download button to download your file. It’ll download as a file called distribution.cer unless you choose to rename it.
- Move the distribution.cer file to the same folder that you’ve been working in
- Complete the certificate creation from the command prompt (making sure you change PASSWORD to be a password of your choice)
openssl x509 -in distribution.cer -inform DER -out ios_distribution.pem -outform PEM
openssl pkcs12 -export -inkey ios_distribution.key -in ios_distribution.pem -out ios_distribution.p12 -passout pass:PASSWORD
With these steps completed you have a certificate file, ios_distribution.p12, which you can upload as a Secure File to Azure DevOps.
You’ll also need the signing identity, which we can get directly from the ios_distribution.pem that was generated along the way.
openssl x509 -text -noout -in ios_distribution.pem
Before you can create a provisioning profile you need to setup an identity for your application in the Apple Developer portal. Go to the Identities tab and click on the + button to register a new application.
Select App IDs and then Continue
Enter both a Description and a Bundle ID. You may also need to select an App ID Prefix if you belong to a team. Next click Continue, followed by the Register button to complete the process of registering your application’s identity.
Note: Currently you will need to update the Info.plist file of your iOS application to set the CFBundleIdentifier to match the Bundle ID specified when creating the App ID.
To create the provisioning profile, click on the Profiles tab in the Apple Developer portal. Click the + button to start the process of creating a new provisioning profile.
Select the Ad Hoc option under Distribution and then Continue.
Select the App ID that you previously created, then Continue
Select the certificate that needs to be used to sign the application, then Continue
Select the devices that will be able to install the application, then Continue
Give the provisioning profile a name and then click Generate.
Download the provisioning profile and add it as a Secure File in the Library in Azure DevOps. You’ll also need the provisioning profile id, which you can extract from the provisioning profile using any text editor. Open the provisioning profile and look for the UUID key; the string value immediately after it is the profile id.
Ok, we’re now ready for the build stage
- template: azure/mobile/[email protected]_templates parameters: stage_name: 'Build_iOS' build_ios: true solution_filename: 'src/Apps/DotNet/Inspector.Uno.sln' solution_build_configuration: 'Release' ios_cert_password: '$(ios_signing_certificate_password)' ios_cert_securefiles_filename: '$(ios_signing_certificate_securefiles_filename)' ios_provisioning_profile_securefiles_filename: '$(ios_provisioning_profile_securefiles_filename)' build_number: '$(Build.BuildId)' full_version_number: '$(version_prefix).$(Build.BuildId)' ios_signing_identity: '$(ios_signing_identity)' ios_provisioning_profile_id: '$(ios_provisioning_profile_id)' artifact_name: 'inspector-build' artifact_folder: 'iOS_output' application_package: 'Inspector-Uno-iOS.ipa'
iOS – Deploy
The deploy stage is relatively simple
- template: azure/mobile/[email protected]_templates parameters: stage_name: 'Deploy_iOS' depends_on: 'Build_iOS' environment_name: 'Inspector-Alpha' artifact_name: 'inspector-build' artifact_folder: 'iOS_output' application_package: 'Inspector-Uno-iOS.ipa' appcenter_service_connection: 'AppCenterInspectorCI' appcenter_organisation: 'thenickrandolph' appcenter_applicationid: 'Inspector-Uno-iOS-Alpha' appcenter_release_notes: 'Release from deploy pipeline'
Windows – Build
The build stage for Windows (UWP) is
- template: azure/mobile/[email protected]_templates parameters: stage_name: 'Build_Windows' build_windows: true solution_filename: 'src/Apps/DotNet/Inspector.Uno.sln' solution_build_configuration: 'Release' uwpBuildPlatform: 'x86' build_number: '$(Build.BuildId)' full_version_number: '$(version_prefix).$(Build.BuildId)' windows_cert_securefiles_filename: '$(windows_signing_certificate_securefiles_filename)' windows_cert_password: '$(windows_signing_certificate_password)' artifact_name: 'inspector-build' artifact_folder: 'Windows_output' application_package: 'Inspector-Uno-Windows.msixbundle' windows_appxupload_name: 'Inspector-Uno-Windows.msixupload'
In order to build your Windows application you’re going to need a signing certificate. Microsoft has good documentation for creating the certificate which I would encourage you to follow. The important thing is to upload the certificate to the Secure files section under the Library tab on Azure DevOps and the corresponding password as a private variable. Damien Aicheh has a good tutorial on uploading certificates to Azure DevOps – don’t worry about the section on using the certificates as the Windows build template already does this for you.
Note: You also don’t need to worry about updating your package.appxmanifest so that the Publisher matches the Subject on the signing certificate. The Windows build template takes care of this too.
There are a couple of other parameters you’ll want to check off
- uwpBuildPlatform – in this YAML this is only set to x86 for efficiency. If you’re planning to deploy your application to a range of devices, you should set this to x86|x64|ARM (this is the default value on the template so you can omit this property if you want to build for all three platforms)
- $(windows_signing_certificate_securefiles_filename) – make sure the windows_signing_certificate_securefiles_filename variable exists in your variable group and that it’s value matches the Secure file name of the certificate (.pfx) file uploaded to the Secure files under the Library in Azure DevOps.
- $(windows_signing_certificate_password) – make sure the windows_signing_certificate_password variable exists in your variable group and that it is password you specified when creating the certificate. This should be marked as private.
Note that the application package that we’re specifying here is an msixbundle. The Windows build pipeline has been upgraded to support both appx and msix bundles.
Windows – Deploy
The deploy stage for Windows (UWP) is
- template: azure/mobile/[email protected]_templates parameters: stage_name: 'Deploy_Windows' depends_on: 'Build_Windows' environment_name: 'Inspector-Alpha' artifact_name: 'inspector-build' artifact_folder: 'Windows_output' application_package: 'Inspector-Uno-Windows.msixbundle' appcenter_service_connection: 'AppCenterInspectorCI' appcenter_organisation: 'thenickrandolph' appcenter_applicationid: 'Inspector-Uno-UWP-Alpha' appcenter_release_notes: 'Release from deploy pipeline'
Pipeline Templates v0.3.0
As promised, here’s a quick summary of the changes in v0.3.0. Technically this should have been a major version increment as there were some breaking changes but since we’re not at v1 yet, I’ve just pushed a minor update.
- build-xamarin-android.yml – change android_bundle_name parameter to application_package (for consistency with deploy-appcenter.yml)
- build-xamarin-ios.yml – change ios_ipa_name parameter to application_package (for consistency with deploy-appcenter.yml)
- build-xamarin-windows.yml – change windows_bundle_name parameter to application_package (for consistency with deploy-appcenter.yml)
- build-xamarin-android.yml – added environment variables (ANDROID_NDK_HOME, ANDROID_NDK_PATH, AndroidNdkDirectory) so that the build process can locate the NDK. This is required for Android builds that enable AOT compilation.
- build-xamarin-android.yml: android_manifest_filename property no longer required – template will search for AndroidManifest.xml
- build-xamarin-windows.yml: windows_package_manifest_filename property no longer required – template will search for *.appxmanifest
- build-xamarin-ios.yml: ios_plist_filename property no longer required – template will search for Info.plist
- build-xamarin-windows.yml: application_package supports both appxbundle and msixbundle file types. You need to make sure the filename matches the type of bundle your application is setup to create.