I have signed and packaged the Xamarin application
and so have created the apk file using Visual Studio.
How can I zipalign this file in Windows?
Make sure you have the Android SDK build-Tools installed then check where your SDK is installed mine was at C:\Program Files(x86)\Android\android-sdk\build-tools\23.0.1\zipalign
You will find zipAlign in the android build tools then use it like so:
C:\Program Files(x86)\Android\android-sdk\build-tools\23.0.1\zipalign -v 4 infile.apk outfile.apk
Usage
To align infile.apk and save it as outfile.apk:
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
To confirm the alignment of existing.apk:
zipalign -c -v <alignment> existing.apk
The alignment is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.
Flags:
-f : overwrite existing outfile.zip
-v : verbose output
-c : confirm the alignment of the given file
output from windows (extra parameters not in the Docs :O) :
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project
Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip
zipalign -c [-v] <align> infile.zip
<align>: alignment in bytes, e.g. '4' provides 32-bit alignment
-c: check alignment only (does not modify file)
-f: overwrite existing outfile.zip
-p: page align stored shared object files
-v: verbose output
-z: recompress using Zopfli
I spent a good few minutes searching for the Android command line build tools. They were nowhere to be found inside either of the Program Files
directories. Apparently, on my system the tools were in the following location:
C:\Users\{username}\AppData\Local\Android\Sdk
I found the location inside Android Studio by going to File > Project Structure > SDK Location.
Edit: Obviously, "my system" is Windows - specifically Windows 10.
For windows 10 set PATH variable to
C:\Users\<NAME>\AppData\Local\Android\Sdk\build-tools\<version>
And then run the command from the location where application is present
zipalign -v 4 <YOUR_APP_NAME> app-release-final.apk
On my end, the path it's quite different, i installed android studio, which created the following path:
C:\Users\<Username>\AppData\Local\Android\Sdk\build-tools\<VERSION>\zipalign.exe
Just direct to that path and run the command, make sure to use full path of where your file is located...
Hope that helps.
This is not a direct answer to the question on how to manually zipalign. But after struggling for a few days to get things working, I hope this may help someone avoid losing some hair. In the end, I found that my apk was not properly signed. It seems (speculation) that Xamarin does the zip aligning after the signing. So when I had the zipalign error, I used zipalign, only to find the next error to be that it wasn't signed correctly. Following are the steps I took to get an apk I could use to create a release in the play store. Clearly I am not proficient in the whole signing thing, so apologies if my steps seem like an awkward journey that could be done in a much simpler manner.
My environment is: Windows 10, Visual Studio 2019, Xamarin Forms 5 app.
First, creating an apk for publishing: In Visual Studio Solution Explorer, right click your Android project, Android Options:
Right click Android project, select Archive...
Find the keystore. Mine at:
C:\Users\<user>\AppData\Local\Xamarin\Mono for Android\Keystore\<keystore alias name above?>\<or this might be the alias>.keystore
Now stop, you need to go setup oauth with google.
create oauth in google developer console:
To view fingerprint:
This displays the fingerprint. Plug it into the Oath 2 screen in developer console. Now in OAuth 2.0 Client Ids you have a client id!
Back to Visual Studio, select the Signing Identity you create above (the keystore), then click Continue. This brings up the Google Play Accounts screen. Click on green + sign
give the account description (I used app name)
give client id from oauth 2 work in google console, above
there doesn't seem to be a client secret anymore, put any junk (warning: this is the most uncertain step - seems I read something about google ditching the client secret, but I can't find that info. However, I never entered anything useful here, and things seem to be working)
click Register. Now weird things happen: a web browser shows up and you log into your google developer account, it says all is good and you can close the window. Back in Visual Studio you may have an error, and for me, nothing was actually uploaded to google. BUT if the gods are smiling down at you, you may have a signed APK in:
C:\Users<user>\AppData\Local\Xamarin\Mono for Android\Archives<date><app name>.Android <date & time>.apkarchive\signed-apks
You've won! You've jumped through all the hoops and smashed through all the barriers that they put up to stop you! Woo-hoo! On the other hand, if you have no signed apk, then ... back to googling for other solutions, sorry :(
With signed apk in hand (which is also zip aligned at this point - you do NOT run the zipalign tool), now you can go back to the google pay console, and create a release (e.g internal testing). Drop this signed apk in, and you should be good to go.
If you use Android SDK installed with Unity:
C:\Program Files\Unity\Hub\Editor<UnityVersion
>\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\build-tools<SDKVersion
>\zipalign.exe