46

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?

7 Answers 7

50

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

ZipAlign Docs

1
  • i am getting the windows out just have you said but it is not generating the aligned app for me to upload in the play store. Please help Feb 20, 2021 at 20:15
17

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.

11

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
0
5

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.

3

for win10 its in c:\Users, such as: C:\Users\\Android\sdk\build-tools\

0

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:

  1. uncheck Use Share Runtime, Generate one package, enable multi-dex.
  2. check use incremental ... aapt2
  3. package format: apk
  4. dex compiler d8
  5. code shrinker r8

Right click Android project, select Archive...

  1. this creates an apk and opens the archive manager.
  2. In archive manager, click Distribute ..., select Google play store
  3. Brings up Signing Identity, click the green + button 3.1) fill out alias name (I think this becomes the name of your keystore file) 3.2) password 3.3) Full name (I used the name of the app) 3.4) organization

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:

  1. Settings, API Access, Create OAuth client (will forward you to google cloud platform, APIs & Services)
  2. Create Credentials
  3. Application Type: Android
  4. Package name: right click android project in Visual Studio, select Android Manifest, package name is displayed
  5. SHA-1 certificate fingerprint

To view fingerprint:

  1. open a command prompt, go to your keystore folder above.
  2. find your jdk folder. It depends on the release you have installed. Mine shown below:
  3. run the keytool: c:\progra~1\Java\jdk1.8.0_112\bin\keytool.exe -list -keystore .<your keystore above>.keystore

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

  1. give the account description (I used app name)

  2. give client id from oauth 2 work in google console, above

  3. 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)

  4. 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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.