{{announcement.body}}
{{announcement.title}}

Build a Xamarin Android Mobile Application Using Azure DevOps (CI)

DZone 's Guide to

Build a Xamarin Android Mobile Application Using Azure DevOps (CI)

Build an Android app with Continuous Integration.

Free Resource

Use the azure-pipelines.yml  file at the root of the repository. Get this file, and build the Xamarin Android application using CI (Continuous Integration) Build.

Follow all instructions in  Create a build pipeline,  https://dzone.com/articles/introduction-to-azure-pipelines to create a build pipeline for Xamarin Android application.

Xamrin.Android

Xamarin.Android provides an open source binding of the Android SDK for use with .NET managed languages such as C#.

Steps

  • Azure pipelines have an option to build and deploy using  Microsoft-hosted agent. Each time, when running a build or release pipeline, get a fresh virtual machine (VM) for the build.
  • Build Xamarin Applications without needing to set up any infrastructure.
  • If Microsoft-hosted agents will not work, use a self-hosted agent that will act as a system host.
  • Need to required MS build, Xamarin, Android, JDK.
pool:
  name: Hosted VS2017
  demands:
  - MSBuild
  - Xamarin.Android
  - JDK


  • Set up the variables to build, and get those values in a required task.
variables:
  outputDirectory: '$(build.binariesDirectory)/$(buildConfiguration)'
  buildConfiguration: 'Release'


  • Install the NuGet with the help of NuGet tool installer.
steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.5.1'
  inputs:
    versionSpec: 4.5.1


  • Use this task in a build or release pipeline to install and update NuGet package dependencies, or package and publish NuGet packages. Make sure that the solution file is  (.sln)  set to the correct directory.
task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: xxx.Mobile/xxx.Mobile.Common/xxx.Mobile.Common.sln
    restoreDirectory: '$(system.defaultworkingdirectory)'


  • Whitesource Bolt is a free developer tool for finding and fixing open source code vulnerabilities.
  • Make sure that the current working directory is set for Whitesource Bolt.
task: whitesource.ws-bolt.bolt.wss. WhiteSource Bolt@19
  displayName: 'WhiteSource Bolt'
  inputs:
    cwd: xxxx.Mobile


Build an Android App With Xamarin

  • Relative path from root repo of Xamarin. Android projects to build, mention.  **/*.csproj  for all csproj files in all subfolders.
  • The project must have a package for Android targets. If Create App package is selected, a package for Android passes the target during the build to generate.
  • The Ms build version should be the latest; optionally supply the architecture of MS Build to run.
  • Pick the JDK version to be used during the build by selecting a JDK Version; the build will be recognized during builds or manually enter a JDK path.
task: XamarinAndroid@1
  displayName: 'Build Xamarin.Android project xxx.Mobile/xxx.Mobile.Admin.Client/Droid/xxx.Mobile.Admin.Client.Droid.csproj'
  inputs:
    projectFile: xxx.Mobile/xxx.Mobile.Admin.Client/Droid/xxx.Mobile.Admin.Client.Droid.csproj
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'
    msbuildVersionOption: latest
    jdkVersionOption: 1.8
  condition: always()


  • The main use of this task in a build or release pipelines to sign and align Android APK files.
  • Relative path from the repo to the APK files as a sign, wildcards to specify multiple files.
  • Use a Keytool command to generate KeyStore certificates.
  • keytool stores the keys and certificates in a Keystore
  • Create a Keystore using the Keytool.

        1. -alias   is an option to mention an Alias Name key entry.

        2.  -keysize  specifies the size of each key to be generated.

        3.  -keyalg  specifies the algorithm to be used to generate the key pair.

        4. -validity   specifies the validity of the KeyStore that create.

        5.  -genkey  The command to generate a key pair (public and private key)

keytool -genkey -v -keystore dep.keystore -storepass testediso -alias test*** -keypass **** -keyalg RSA -keysize 2048 -validity 10000


keytool

As the KeyStore name is mentioned, the  dept. keystore file created at the directory where you run the keytool command.dept.keystore


  • Sign the APK with a provided Keystore file.
  • Unsigned APKs can only run in an emulator.APKs and must be signed to run on a device.
  • Keystore password provided in the Keystore file use variables with its lock enabled on the variables tab to encrypt the value.Lock-enabled variable
  • The zipalign option reduces the amount of the RAM consumed by an app.
- task: AndroidSigning@3
  displayName: 'Signing and aligning APK file(s) $(outputDirectory)/*.apk'
  inputs:
    apkFiles: '$(outputDirectory)/*.apk'
    apksignerKeystoreFile: '******'
    apksignerKeystorePassword: *****
    apksignerKeystoreAlias: '****'
    apksignerKeyPassword: *****


  • Use this task to archive file compressions formats such as .rar, .zip and.tar.gz.
task: ArchiveFiles@2
  displayName: 'Archive $(outputDirectory)'
  inputs:
    rootFolderOrFile: '$(outputDirectory)'
    includeRootFolder: false


  • Use this task in the build pipeline to publish the build artifacts to azure pipelines, file share.it will store it in the Azure DevOps server.
task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathtoPublish: '$(outputDirectory)'


  • After the build succeeds or fails, get the logs during the build solution.Agent jobPublish Artifact: drop
  • Install the .apk file on the mobile device.Install .apk file


  • An azure-pipelines.yaml  file is shown here. Change the following azure-pipelines.yaml   file according to the requirements. 
pool:
  name: Hosted VS2017
  demands:
  - MSBuild
  - Xamarin.Android
  - JDK
variables:
  outputDirectory: '$(build.binariesDirectory)/$(buildConfiguration)'
  buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 4.5.1'
  inputs:
    versionSpec: 4.5.1
task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: xxx.Mobile/xxx.Mobile.Common/xxx.Mobile.Common.sln
    restoreDirectory: '$(system.defaultworkingdirectory)'
task: whitesource.ws-bolt.bolt.wss. WhiteSource Bolt@19
  displayName: 'WhiteSource Bolt'
  inputs:
    cwd: xxxx.Mobile
task: XamarinAndroid@1
  displayName: 'Build Xamarin.Android project xxx.Mobile/xxx.Mobile.Admin.Client/Droid/xxx.Mobile.Admin.Client.Droid.csproj'
  inputs:
    projectFile: xxx.Mobile/xxx.Mobile.Admin.Client/Droid/xxx.Mobile.Admin.Client.Droid.csproj
    outputDirectory: '$(outputDirectory)'
    configuration: '$(buildConfiguration)'
    msbuildVersionOption: latest
    jdkVersionOption: 1.8
  condition: always()
task: AndroidSigning@3
  displayName: 'Signing and aligning APK file(s) $(outputDirectory)/*.apk'
  inputs:
    apkFiles: '$(outputDirectory)/*.apk'
    apksignerKeystoreFile: '******'
    apksignerKeystorePassword: *****
    apksignerKeystoreAlias: '****'
    apksignerKeyPassword: *****
task: ArchiveFiles@2
  displayName: 'Archive $(outputDirectory)'
  inputs:
    rootFolderOrFile: '$(outputDirectory)'
    includeRootFolder: false
task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    PathtoPublish: '$(outputDirectory)'
Topics:
xamarin ,dotnet

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}