Search code examples
androidgradleazure-devopszipalignandroid-signing

Android Azure Devops Gradle build failing on signing apk w zipalign, exit code 1


I have an android app and I am trying to set up the CICD in Azure Devops for our team. However I cannot get the "Build" stage to complete. It fails in the "Android Signing" sub-task with error message:

[error]Error: The process '/Users/vsts/Library/Android/sdk/build-tools/19.1.0/zipalign' failed with exit code 1

It works when I deselect the optional "zipalign" feature on the signing task. But as I've read you are not supposed to deploy applications without using zipalign first.

The app is built in Android Studio (Kotlin). All similar issues I have found online have been related to xamarin, which I am not using at this point.

I am deploying on a arm64 device if that is relevant. Building and signing in Android Studio is no problem (Although I am not sure if it uses the zipalign by default there as it is not clear from using the Generate Signed APK wizard. I would assume it will do it for you without asking?). Anyways I'm of course trying to automate the build-deploy in DevOps on pull-requests to the master branch, which is how I usually do it with other projects.

Here is my azure-pipelines.yaml

# Android
# Build your Android project with Gradle.
# Add steps that test, sign, and distribute the APK, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/android

trigger:
- master

pool:
  vmImage: 'macos-latest'

steps:
- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'assembleDebug'

- task: AndroidSigning@3
  inputs:
    apkFiles: '**/*.apk'
    apksignerKeystoreFile: 'key.jks'
    apksignerKeystorePassword: 'XXXX'
    apksignerKeystoreAlias: 'key0'
    apksignerKeyPassword: 'XXXX'

- task: CopyFiles@2
  inputs:
    contents: '**/*.apk'
    targetFolder: '$(build.artifactStagingDirectory)'
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

The tail-end of the log below. I am getting a bunch of (BAD - 1-2-3) messages for various resources. Not sure if this is related to the error itself.

enter image description here


Solution

  • Please check the discussion here. https://github.com/microsoft/azure-pipelines-tasks/issues/13863. It seems that in the latest Gradle release the apk is built using a new library (zipflinger) which automatically alignes the apk, and zipalign fails when trying to align the apk that is already aligned (zipalign verification returns success on the initial apk)... This sounds really strange, but apparently zipalign is not needed anymore.

    So the solution is to disable zipalign in your zure-piplelines.yml

     - task: AndroidSigning@3
      inputs:
        apkFiles: '**/*.apk'
        ...
        zipalign: false