AzureDevOps Android Pipeline zipalign - failed with exit code 1
Asked Answered
F

1

5

I'm not sure what happened, everything used to build just fine with our pipeline in Azure Devops.

But just recently, we have every build failing at zipaligning ... the APK zipaligns and builds just fine when using the Android Studio IDE, but it fails in Azure Devops pipeline.

Here is the start of the zipalign from Azure:

Starting: AndroidSigning
==============================================================================
Task         : Android signing
Description  : Sign and align Android APK files
Version      : 3.176.0
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/android-signing
==============================================================================
"C:\Program Files (x86)\Android\android-sdk\build-tools\29.0.2\zipalign.exe" -v 4 D:\a\1\s\app\build\outputs\apk\release\app-release-unsigned.apk.unaligned D:\a\1\s\app\build\outputs\apk\release\app-release-unsigned.apk
Verifying alignment of D:\a\1\s\app\build\outputs\apk\release\app-release-unsigned.apk (4)...

And here is the failure message at the end:

Verification FAILED
##[error]Error: The process 'C:\Program Files (x86)\Android\android-sdk\build-tools\29.0.2\zipalign.exe' failed with exit code 1

Lastly, here is the context of our pipeline YAML:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'assembleRelease'

- task: AndroidSigning@3
  inputs:
    apkFiles: '**/*.apk'
    apksign: true
    apksignerFile: C:\Program Files (x86)\Android\android-sdk\build-tools\29.0.2\apksigner.bat
    apksignerKeystoreFile: 'keystore.jks'
    apksignerKeystorePassword: '$(jarsignerKeystorePassword)'
    apksignerKeystoreAlias: 'alias'
    apksignerKeyPassword: '$(jarsignerKeyPassword)'
    apksignerArguments: --out $(Build.SourcesDirectory)/app/build/outputs/apk/release/app.apk
    zipalign: true
    zipalignFile: C:\Program Files (x86)\Android\android-sdk\build-tools\29.0.2\zipalign.exe

Any help would be greatly appreciated!

EDIT: Here is the debug log of the build failing to publish artifact, when set to AndroidSigning@2 (jarsigner instead of apksigner)

2020-11-02T15:49:43.9428841Z ##[debug]Evaluating condition for step: 'PublishBuildArtifacts'
2020-11-02T15:49:43.9430851Z ##[debug]Evaluating: SucceededNode()
2020-11-02T15:49:43.9431429Z ##[debug]Evaluating SucceededNode:
2020-11-02T15:49:43.9432781Z ##[debug]=> True
2020-11-02T15:49:43.9433320Z ##[debug]Result: True
2020-11-02T15:49:43.9433928Z ##[section]Starting: PublishBuildArtifacts
2020-11-02T15:49:43.9558720Z ==============================================================================
2020-11-02T15:49:43.9559237Z Task         : Publish build artifacts
2020-11-02T15:49:43.9559565Z Description  : Publish build artifacts to Azure Pipelines or a Windows file share
2020-11-02T15:49:43.9559869Z Version      : 1.158.3
2020-11-02T15:49:43.9560103Z Author       : Microsoft Corporation
2020-11-02T15:49:43.9560457Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/publish-build-artifacts
2020-11-02T15:49:43.9560873Z ==============================================================================
2020-11-02T15:49:43.9627178Z ##[debug]Using node path: C:\agents\2.175.2\externals\node\bin\node.exe
2020-11-02T15:49:44.1659280Z ##[debug]agent.TempDirectory=D:\a\_temp
2020-11-02T15:49:44.1690792Z ##[debug]loading inputs and endpoints
2020-11-02T15:49:44.1701762Z ##[debug]loading ENDPOINT_AUTH_PARAMETER_SYSTEMVSSCONNECTION_ACCESSTOKEN
2020-11-02T15:49:44.1714168Z ##[debug]loading ENDPOINT_AUTH_SCHEME_SYSTEMVSSCONNECTION
2020-11-02T15:49:44.1717582Z ##[debug]loading ENDPOINT_AUTH_SYSTEMVSSCONNECTION
2020-11-02T15:49:44.1719830Z ##[debug]loading INPUT_ARTIFACTNAME
2020-11-02T15:49:44.1721291Z ##[debug]loading INPUT_ARTIFACTTYPE
2020-11-02T15:49:44.1722675Z ##[debug]loading INPUT_PARALLEL
2020-11-02T15:49:44.1723961Z ##[debug]loading INPUT_PARALLELCOUNT
2020-11-02T15:49:44.1725345Z ##[debug]loading INPUT_PATHTOPUBLISH
2020-11-02T15:49:44.1729968Z ##[debug]loading SECRET_JARSIGNERKEYPASSWORD
2020-11-02T15:49:44.1732034Z ##[debug]loading SECRET_JARSIGNERKEYSTOREPASSWORD
2020-11-02T15:49:44.1733774Z ##[debug]loading SECRET_SYSTEM_ACCESSTOKEN
2020-11-02T15:49:44.1739927Z ##[debug]loaded 11
2020-11-02T15:49:44.1753983Z ##[debug]Agent.ProxyUrl=undefined
2020-11-02T15:49:44.1755481Z ##[debug]Agent.CAInfo=undefined
2020-11-02T15:49:44.1756003Z ##[debug]Agent.ClientCert=undefined
2020-11-02T15:49:44.1756464Z ##[debug]Agent.SkipCertValidation=undefined
2020-11-02T15:49:44.1769886Z ##[debug]check path : D:\a\_tasks\PublishBuildArtifacts_2ff763a7-ce83-4e1f-bc89-0ae63477cebe\1.158.3\task.json
2020-11-02T15:49:44.1771713Z ##[debug]adding resource file: D:\a\_tasks\PublishBuildArtifacts_2ff763a7-ce83-4e1f-bc89-0ae63477cebe\1.158.3\task.json
2020-11-02T15:49:44.1772325Z ##[debug]system.culture=en-US
2020-11-02T15:49:44.1792494Z ##[debug]PathtoPublish=D:\a\1\a\app\build\outputs\apk\release\
2020-11-02T15:49:44.1793208Z ##[debug]check path : D:\a\1\a\app\build\outputs\apk\release\
2020-11-02T15:49:44.1821823Z ##[debug]task result: Failed
2020-11-02T15:49:44.1857082Z ##[error]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a\app\build\outputs\apk\release\
2020-11-02T15:49:44.1867823Z ##[debug]Processed: ##vso[task.issue type=error;]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a\app\build\outputs\apk\release\
2020-11-02T15:49:44.1869433Z ##[debug]Processed: ##vso[task.complete result=Failed;]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a\app\build\outputs\apk\release\
2020-11-02T15:49:44.1890809Z ##[section]Finishing: PublishBuildArtifacts
Fecund answered 28/10, 2020 at 16:24 Comment(5)
Hi @buradd, Could you try the task AndroidSigning@2 and then kindly share the result here? In addition, please also share the full log here. ThanksUnzip
Hi @VitoLiu-MSFT - when I change AndroidSigning@3 to AndroidSigning@2, it fails during pre-job with the following error: ##[error]Error: Input required: keystoreFileFecund
@VitoLiu-MSFT, I see that AndroidSigning@2 uses jarsigner instead of apksigner.. so I made the adjustments, and it passes the signing task but then fails on PublishBuildArtifacts - ##[error]Publishing build artifacts failed with an error: Not found PathtoPublish: D:\a\1\a\app\build\outputs\apk\release\Fecund
Hi @buradd, Sorry to reply you so late, could you please set the variable system.debug to true and then share the full detail logs here? We need check the log and help you local the issue. Thanks.Unzip
@VitoLiu-MSFT no problem - I've edited my post with the log from the AndroidSigning@2 debug error at PublishArtifact. The signing goes well, but then it can't find the output folder? When using the AndroidSigning@3, it doesn't get passed the AndroidSigning job, a lot of the checks come back as (BAD - 2), (BAD - 3), etc.. mostly /res/ files, everything passes just fine with (OK) when using AndroidSigning@2. Keep in mind we have used this pipeline for a long time already, and had many successful builds - but now it wont even build old branches that we know have built fine before with pipelineFecund
F
10

So after going back and forth with some on the github azure pipelines task issue thread, I found that by changing my Android gradle plugin from 4.1.0+ (it was at 4.1.1) to 4.0.0 in my project level gradle.settings, the azure pipeline build would complete successfully.

Gradle itself stayed at version 6.5, but the Android gradle plugin was set to 4.0.0 to make it build successfully.

Then, another pointed this thread out on the Google issue tracker: https://issuetracker.google.com/issues/162117652

It appears that the zipalign is no longer necessary as of the latest Android gradle plugin 4.1.0+. I guess Android gradle plugin 4.1.0+ already zipaligns the apk, so any subsequent call to zipalign will show a failure as it's already zipaligned.

So the ultimate solution here, was to keep the latest version of Android gradle plugin (4.1.1) and change zipalign: true to zipalign: false in my Azure pipeline AndroidSigning@3 task.

You can check to confirm if your APK is actually zipaligned, even with the zipalign set to false in the pipeline, by running the following:

zipalign -c -v 4 /path/to/app-release.apk.

rather than attempt to modify the file, this command will simply verify the zipaling in the apk and show if the verification was successful (apk is zipaligned successfully).

Fecund answered 23/11, 2020 at 17:26 Comment(1)
Thank you for this answer! It helped me a lot.Epicarp

© 2022 - 2024 — McMap. All rights reserved.