Xamarin-android: Deploying to Android fails with zipalign error

Created on 6 Apr 2018  Â·  55Comments  Â·  Source: xamarin/xamarin-android

_From @johnshardman on April 4, 2018 10:25_

Bug report best practices: https://github.com/xamarin/Xamarin.Forms/wiki/Submitting-Issues

Description

After upgrading Visual Studio to 15.6, deployment of app to Android (physical devices and emulators) failed with a zipalign error.

Steps to Reproduce

  1. Check the "Generate one package (.apk) per selected ABI" option in the Android project properties.
  2. Hit F5

Expected Behavior

App should build, deploy and run in debug mode.

Actual Behavior

Deployment error reported:

AndroidZipAlign Task
Alignment: 4
Source: G:\tfs\MyApp\Top\MyApp.Android\obj\Debug\android\bin\com.MyApp.MyApp-arm64-v8a.apk
DestinationDirectory: bin\Debug\
C:\ProgramFiles\Android\AndroidSDK\build-tools\25.0.2\zipalign.exe 4 "G:\tfs\MyApp\Top\MyApp.Android\obj\Debug\android\bin\com.MyApp.MyApp-arm64-v8a.apk" "bin\Debug\com.MyApp.MyApp-arm64-v8a-Signed.apk"
Output file 'bin\Debug\com.MyApp.MyApp-arm64-v8a-Signed.apk' exists
"zipalign.exe" exited with code 1.
Done executing task "AndroidZipAlign" -- FAILED

Basic Information

VS 15.6
XF 2.4.0.38779

I'm not the only one seeing this. @batmaci has also reported it at https://forums.xamarin.com/discussion/comment/325764

_Copied from original issue: xamarin/Xamarin.Forms#2243_

Most helpful comment

@curtisshipley you could add a custom target to your csproj which runs before the _Sign target that deletes the files in question.

<Target Name="_Workaround" BeforeTargets="_Sign"
         Inputs="$(MSBuildAllProjects);$(ApkFileIntermediate);$(_AndroidBuildPropertiesCache)"
    Outputs="$(ApkFileSigned)">
    <ItemGroup>
        <ApkAbiFilesSigned Include="$(ApkFileSigned)" />
        <ApkAbiFilesSigned Condition="'$(AndroidCreatePackagePerAbi)' == 'true' " Include="$(OutDir)$(_AndroidPackage)*-Signed.apk" />
    </ItemGroup>
    <Delete Files="%(ApkAbiFilesSigned.FullPath)" />
</Target>

That might fixup the issue in the mean time.

All 55 comments

_From @curtisshipley on April 4, 2018 19:46_

I'm running into it, and it's a show-stopper for me because I need to split APKs for UI Testing.

Other people are running into it here:
https://developercommunity.visualstudio.com/content/problem/210073/msb6006-zipalign-exited-with-code-1.html

_From @EmilAlipiev on April 6, 2018 12:31_

It is a such a big disappointment that they didnt fix this with latest Vs update. It is producing more and more errors every new update. VS2017 should give us option to easily downgrade. I dont want to spend hours to do that. this is really annoying how VS and Xamarin teams work. such a basic issue and they are overlooking and they are saying "more info needed". which info do you need?

I'm able to reproduce it on 15.7 Preview 2 as well. We're talking to the X.Android team and moving the issue for further troubleshooting from them.

Looks like an issue wrt https://github.com/xamarin/xamarin-android/commit/f6c58d60c867619c93c2085399b20e17f4d772b6 .

Maybe this call to <Delete> task https://github.com/xamarin/xamarin-android/commit/f6c58d60c867619c93c2085399b20e17f4d772b6#diff-db39bf07b89b50eaab8ab2001546310cR2447 should be actually this https://github.com/xamarin/xamarin-android/commit/f6c58d60c867619c93c2085399b20e17f4d772b6#diff-db39bf07b89b50eaab8ab2001546310cR2470 (along with <ItemGroup> definition immediately above), but just a guess.

@davidortinau I am unable to repo this on our master branch on Mac. I will try on windows d15-7. Failing that can you provide diagnostic output from your repo (and even the repo project itself) to make sure we are using the correct setup.

@davidortinau There are a number of variables in this it seems (as I am unable to repo at all or master or d15-7).
Are they using apksigner ? Is it turned off? what are the values for AndroidSupportedAbis.
We could really use some diagnostic build output that would clear allot of those questions up.

While I am tempted to implement @atsushieno's suggestion, until I can repo we'd just be guessing.

Here is a project that is broken with this issue. (https://github.com/RobertFrancis/testHelloworld2)

If it builds and deploys to a device correctly, make a code change eg

string debug = "";

Build and re-deploy and you should be able to reproduce the zip align error. This project also creates corrupt abi's when trying to package them.

AndroidSupportedAbis Settings are as follows;

  • Generate one package (.apk) per selected ABI (Checked)
  • Advanced > Supported ABI's - armeabi (Checked), armeabli-v7a (Checked), x86 (Checked), arm64-v8a (Unchecked), x86_64 (Unchecked)

@RobertFrancis thanks for the info, however I already tried that exact scenario and it worked here.
Do you have the diagnostic build output from that process? if so can you pop it up on gist.guthub.com so we can take a look at the output?

Removed

@RobertFrancis sorry thats not diagnostic output, so it doesn't contain the additional info we need. See [1] for how to turn it on in the IDE's.
Note the output will be HUGE so go to gist.github.com and past the output there. It makes it easier to read..

[1] https://docs.microsoft.com/en-us/xamarin/android/troubleshooting/troubleshooting#diagnostic-msbuild-output

@RobertFrancis that is diagnostic output, but only from Build, not the Install target (which is how the IDE deploys to device).

I guess try this from the command line

msbuild <project> /t:Install /v:diag > build.log

@dellis1972 I've pinged the OP and others to try and provide you with answers.

Sorry for my stupidity. But could you clarify a few things...

I guess try this from the command line

msbuild [project] /t:Install /v:diag > build.log

  • Command line, meaning Terminal on Mac?
  • Am I running this command at root ?
  • Project, being the full path to the testHelloWorld2 sln file?
  • Then I'm guessing the build.log will be dumped in this dir?

@RobertFrancis:

  • Command line, meaning Terminal on Mac?

Yes.

  • Am I running this command at root ?

You run it from the directory containing the .csproj of your Application project.

cd path/to/your/Solution/AppProject
msbuild AppProject.csproj /t:Install /v:diag > build.log
  • Project, being the full path to the testHelloWorld2 sln file?

Project is the path to the AppProject.csproj file, whatever project is responsible for creating your .apk file.

  • Then I’m guessing the build.log will be dumped in this dir?

Yes.

Once finished, you can run open . to open a Finder window from the current directory, at which point you can drag-and-drop build.log onto GitHub to upload it.

Here is the gist:

https://gist.github.com/curtisshipley/1473bdf868eff77fc37c79fa02a0dc59

FWIW, I noticed that if I blow away the obj and bin directories, then rebuild, it succeeds. Doing a clean or rebuild all, doesn't work.

@curtisshipley

I see this in the logs

Task "AndroidSignPackage" skipped, due to false condition; ( '$(AndroidUseApkSigner)' != 'true' ) was evaluated as ( 'True' != 'true' ).

which is very weird. are you running a case sensitive file system?

never mind. But at least we know apksigner is not being used

These project files are around 4 years old. They are used on both Mac OS and Windows. This build gist was done on a mac.

@jonpryor Thanks Jon, that was really helpful.

My gist is linked below.
https://gist.github.com/RobertFrancis/b5ce92b2591c80fd0d6f3d4bb2b6e493

Sorry for the slow response, I've been away.
Has there been any progress with this?

@RobertFrancis I think I found a bug in our targets where the Abi specific apks are not being deleted, which would then probably cause this. I'll be taking a look this week

I think I have nailed down the problem here. Hopefully will have a PR up soon

@dellis1972 In the mean time, is there a tweak we can make to the project file for this? Is it simply a matter of making sure the APKs are deleted?

@curtisshipley you could add a custom target to your csproj which runs before the _Sign target that deletes the files in question.

<Target Name="_Workaround" BeforeTargets="_Sign"
         Inputs="$(MSBuildAllProjects);$(ApkFileIntermediate);$(_AndroidBuildPropertiesCache)"
    Outputs="$(ApkFileSigned)">
    <ItemGroup>
        <ApkAbiFilesSigned Include="$(ApkFileSigned)" />
        <ApkAbiFilesSigned Condition="'$(AndroidCreatePackagePerAbi)' == 'true' " Include="$(OutDir)$(_AndroidPackage)*-Signed.apk" />
    </ItemGroup>
    <Delete Files="%(ApkAbiFilesSigned.FullPath)" />
</Target>

That might fixup the issue in the mean time.

@dellis1972 Thanks!!

Has this made it into any builds yet?
I can't see it in any of the release history notes...

We are still currently having to use an old version of VS for mac on a single laptop in order to deploy/create a non corrupt apk.

it is still not fixed. i am using latest update of VS 2017 pro version and I am getting this error very often.

Exact same problem here with see below and "Generate one package per selected ABI" enabled. However, Dellis1972 workaround fixed it for now.

Last output of build:

3>Deleting file "....\bin\Android\AnyCPURelease\com.upp.pingpong.apk".
3>Copying file from "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong.apk" to "....\bin\Android\AnyCPURelease\com.upp.pingpong.apk".
3>Copying file from "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong-armeabi.apk" to "....\bin\Android\AnyCPURelease\com.upp.pingpong-armeabi.apk".
3>Copying file from "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong-armeabi-v7a.apk" to "....\bin\Android\AnyCPURelease\com.upp.pingpong-armeabi-v7a.apk".
3>Copying file from "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong-x86.apk" to "....\bin\Android\AnyCPURelease\com.upp.pingpong-x86.apk".
3>Copying file from "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong-arm64-v8a.apk" to "....\bin\Android\AnyCPURelease\com.upp.pingpong-arm64-v8a.apk".
3>Touching "objRelease\android_debug_keystore.flag".
3>C:\Program Files\Java\jdk1.8.0_121\bin\keytool.exe -list -alias androiddebugkey -storepass android -keypass ... -keystore "..."
3>Deleting file "....\bin\Android\AnyCPURelease\com.upp.pingpong-Signed.apk".
3>...\Android\Sdk\build-tools\27.0.3\zipalign.exe 4 "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong.apk" "....\bin\Android\AnyCPURelease\com.upp.pingpong-Signed.apk"
3>...\Android\Sdk\build-tools\27.0.3\zipalign.exe 4 "C:\reps\Upp.PingPong\Source\Upp.PingPong.Android\objRelease\android\bin\com.upp.pingpong-arm64-v8a.apk" "....\bin\Android\AnyCPURelease\com.upp.pingpong-arm64-v8a-Signed.apk"
3>Output file '....\bin\Android\AnyCPURelease\com.upp.pingpong-arm64-v8a-Signed.apk' exists
3>"zipalign.exe" exited with code 1.
3>Done building project "Upp.PingPong.Android.csproj" -- FAILED.
3>Build FAILED.

It seems only the generic APK is deleted. That is why signing that one works and the following (arm64...) fails.

Versions:

Microsoft Visual Studio Professional 2017
Version 15.6.7
VisualStudio.15.Release/15.6.7+27428.2043
Microsoft .NET Framework
Version 4.7.02556

Installed Version: Professional
...
Xamarin 4.9.0.753 (f0f46392f)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 4.10.96 (a702a9ceb)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 8.2.0.16 (HEAD/a78295902)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK 11.9.1.24 (f62de47)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

why is that closed? this error exist in latest VS2017 current and pre versions. just hit 2 times release and 2nd time returns this error without cleaning all those folders and rebuilding

I also get this error, also pretty anoyed about something crucial like this not working correctly and not fixed yet.
Every new update of VS/Xamarin is a nightmare for our projects since everytime something critical starts failing.

I have this error. Why is that closed? I can't use new version VS because of this error. It worked before just fix it. Xamarin Team - I want to hear your comments.

The issues is closed because the fix was merged into master.
Unfortunately it was merged too late to get into the 15.7 build which just went to stable, as a result it will be in the next preview release 15.8.

Because of this I posted a work around [1] for those of you wanting to use apksigner.
For those not wanting to use apksigner you can set the property AndroidUseApkSigner to false in your csproj

<AndroudUseApkSigner>False</AndroudUseApkSigner>

This will stop the new code path being used and the old jarsigner will be used in its place. This old code path should not have any issues.

[1] https://github.com/xamarin/xamarin-android/issues/1519#issuecomment-382025458

@EmilAlipiev I was one of the first people to report this particular bug. I am completely satisfied with @dellis1972 's investigation and two work-arounds. That solved the problem for me. I am no longer blocked.

I suggest that if you have specific problems, you either file a new issue, or comment on an existing issue that wasn't resolved instead of muddying the thread with non-specific gripes, because it wastes my time and the time of everyone else on this thread who gets email notifications.

@dellis1972 - Thank you, it it working.

@dellis1972 I just upgrade to vs 15.8 preview, but it still fails.

Hi,

I applied the workaround, which now allows me to deploy to a device from Visual Studio for Mac.

However this has not fixed the issue when trying to install an apk that has been generated by "Archive for Publishing".

When trying to install this apk the install fails. It would appear that the apk is corrupt.

install_screen

Looking at the device log/Logcat errors show this.

error

Let me know if there is any other information that you need.
Note : This exact process works in Visual Studios for Mac 7.3
Note 2 : Rolled Xamarin.Android back to 8.1.5 now everything is working.

I just installed 15.7.3 which came out today, same problem again, can't it be fixed in a 15.7.x service release ?
Deleting bin/obj did the trick for me

For me the issue was when I had checked the option "Generate one package per selected ABI". The moment I unchecked zip align issues gone.. VS 2017 community edition

Hello everybody, it does not work again(vs 15.7). Apk is builded and even installed. But the application crashes when it starts. Will it ever work ("Generate one package per selected ABI")? Three months have already passed. Xamarin Team - your comment.
p.s. I did three times downgrade to Visual Studio 15.5.

is there anyone here? it does not work for three months!!!

I am re-opening this issue based on user feedback. Is someone able to post a diagnostic build log which contains the error? I will try to repo again on 15.8.

If the application is crashing on startup that is probably a different issue. Please open a new issue and post the add logcat of the application startup.

I just tried to repo on Xamarin.Android 8.3.99.12 (the current alpha on MacOS) and it is working for me. The apk's are correctly signed when using

msbuild /t:Install /p:Configuration=Release /v:diag

and the application starts on the device without any problems. Can anyone post any diagnostic output of the issue they are facing?

@RobertFrancis the archiving in VSforMac is handled by the IDE at the moment. I believe there is currently a bug in the IDE around using apksigner . You might want to try <AndroudUseApkSigner>False</AndroudUseApkSigner> in your project until they fix that issue.

Thank you for your message. I will prepare a repository with a demonstration of this issue. I use VS 15.7 for Windows.

This is my repository with a demonstration of the issue
This is simple standard project with one page. I created archive for publishing. If I use "Aot compile" option I get error - "Apk failed to install Error:could not parse error string". If I disable this option than apk is work.
For use my test project update option AndroidSigningKeyStore in ApkSignerIssue.Android.csproj - insert your path. File test.keystore is in folder test.
I use Android Api 26. Now I try Api 25. If AOT not working it is very bed. Without AOT Xamarin Forms +Android can not be used for commercial purposes.

I try Api 25 - it is not work. I try use VS 15.5.7 and 15.8.0(preview) - with "AOT compile" it is not work.
Without this option, XF+Android can not be used - the first load of the app will take 8-10 seconds. Team - your comments.

@FetFrumos it sounds like you have a different issue from the ZipAlign problem listed on this issue.

Do you have the build log for the "Apk failed to install Error:could not parse error string" issue?

You might need to provide both the diagnostic build log and the output from adb logcat so we can look at the output the device is producing. We might need to open an new issue for the Aot problem so we can track that properly.

@dellis1972 ,

Please see this post for more information.
https://developercommunity.visualstudio.com/content/problem/253160/archive-for-publishing-generating-corrupt-apks.html

Why does rolling back my Xamarin.Android version stop the issue, if the issue is related to the VS for Mac IDE?

Since rolling back everything has been fine and I am somewhat hesitant to update anything at this point.

=== Visual Studio Community 2017 for Mac ===

Version 7.5 (build 1254)
Runtime:
    Mono 5.10.1.47 (2017-12/8eb8f7d5e74) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 4.4.0.36 (master / 0c7c49a6)

    Package version: 510010047

=== Xamarin.Android ===

Version: 8.1.5.0 (Visual Studio Community)
    Supported Android versions:
        2.3   (API level 10)
        4.0.3 (API level 15)
        4.4   (API level 19)
        5.0   (API level 21)
        5.1   (API level 22)
        6.0   (API level 23)
        7.0   (API level 24)
        7.1   (API level 25)
        8.0   (API level 26)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 27.0.1
SDK Build Tools Version: 27.0.3

Java SDK: /usr
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Build Information ===

Release ID: 705001254
Git revision: 498923ea36d2c7fe440c4e4b8cfb75bd50bbd748
Build date: 2018-05-05 10:35:24-04
Xamarin addins: 219f1c4943b4693b837b4173dd10ea982a47c852
Build lane: monodevelop-lion-d15-7

=== Operating System ===

Mac OS X 10.13.4

Thanks !

@RobertFrancis the reason there is problem is because the XA build system switch over to use apksigner by default. The IDE calls on of the packaging targets (PackageForAndroid or SignAndroidPackage) and with apksigner enabled this produces slightly different results to what the IDE is expecting for its 'Archiving' functionality. This results in a weird apk. Disabling the AndroudUseApkSigner property in the latest versions should fix the issue with the IDE Archiving functionality.

We do have a project in the pipeline to move all the Archiving logic from the IDE's into the XA msbuild system, so that its a) consistent and b) more resilient to changes in the build system.

Hello everybody,
Downgrade to 15.5.7 working for me now.
I do not know why this did not work right away, maybe I rebooted the computer. but now it works.
That's good - I was able to release an update my app.
And this is very sad - this is my fourth downgrade in three months(starting from 15.6).
Archiving the package does not work normally three months, three months, Karl!
I understand everything very difficult, but three months, with a bunch of reviews about these errors.
I do not know how you organized the product testing, but it's very sad. I lost a lot of time.

When will this work normally and stable? Xamarin Team - your comments.

I installed VS 15.7.4
With option:

<AndroidUseApkSigner>False</AndroidUseApkSigner>

This is work for me. I hope the problem is solved. Thank you.

This is so annoying problem still exist and every time we need to delete bin and obj folders.

If this is still a problem, can someone post on gist.github.com the
diagnostic build output showing the error and post the link here.
The scenario mentioned in the bug was fixed and is in the 15.8 release
(which is in the Alpha channel at the moment).

If you are using 15.7.x you need to use the work around
https://github.com/xamarin/xamarin-android/issues/1519#issuecomment-398999607

If there is another scenario where this is occurring we need more
information to replicate the issue.

If the error you are seeing is not producing a zip align error, when a new
issue should be opened with as much information as possible.

On 5 August 2018 at 12:54, Emil Alipiev notifications@github.com wrote:

This is so annoying problem still exist and every time we need to delete
bin and obj folders.

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/xamarin/xamarin-android/issues/1519#issuecomment-410515194,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAxeeQj_4xSd8yaPDUb5JfFEtAnoplW7ks5uNt0RgaJpZM4TKCuw
.

may be this help someone,in my case was that the firewall was blocking zipaling.exe to make changes

@EmilAlipiev

Have you figured out what it is in the bin and obj directories that causes this hang-up? I am also noticing your solution as acceptable, and am wondering what can be done on Xamarin's end to remedy this? It seems to happen on the second or subsequent builds when dealing in large projects.

I understand giving a VS-integrated GUI option to delete bin and obj is sort of cheeky, but not being able to fix it for years while developers write custom shell and batch scripts to do _Xamarin_'s housekeeping locally? That is also cheeky.

Hoping to see a solution to this one soon

Was this page helpful?
0 / 5 - 0 ratings