Re-signing an Android APK file

This tutorial is going to look at how you can manually re-sign an Android APK for an application on your device. In another post we’ll look at using Robotium to run unit tests on Android applications for which you don’t have the original source code. There is a tool that does this for you, but if you experience problems with this, either check out this nugget, or read on…

Assuming you have the Android SDK installed, you will need an app (or see comment below) that can backup other apps on your device (like AndroZip) and an Archive application on your PC that allows you to delete items within a zip archive (such as 7-zip [PC] or BetterZip [Mac]). For this tutorial I am going to re-sign my demo app ‘Custom Buttons’ which you can grab from my downloads page.

  1. To start, backup your target app to SD card using AndroZip and transfer to your PC
  2. Change the file extension from .apk to .zip
  3. Open the archive with your chosen utility, delete the ‘META-INF’ folder and exit your utility programme, saving the changes if prompted. Don’t extract the zip, remove the folder then re-zip – this won’t work!
  4. Change the extension back to .apk
  5. Re-sign the .apk file with you debug certificate in terminal. Here I have cd‘d to the directory containing my target apk file
    jarsigner -verbose -keystore ~/.android/debug.keystore targetapp.apk androiddebugkey
  6. When prompted, the password to enter is android
  7. Verify that the app has been signed properly:
    jarsigner -verify targetapp.apk
  8. Next, cd into your Android SDK tools folder and run zipalign, specifying the location of the input file and output file.
    $ ./zipalign -v 4 ~/Desktop/targetapp.apk ~/Desktop/targetapp-aligned.apk
  9. Verify that the apk has been correctly aligned
    $ ./zipalign -c -v 4 ~/Desktop/targetapp-aligned.apk
  10. Now you can uninstall the original application from your Android device and install this re-signed application to your device. There are many ways to do this, but from the command-line, cd into your platform-tools directory and, provided you only have the one device attached, enter:
    $ ./adb install ~/Desktop/targetapp-aligned.apk