How to Sign and Align your Android App (.apk)

  • 72 favourites

Stats

25,481 visits, 36,271 views

Translations

This tutorial hasn't been translated.

Tools

Well, you've created your game in Construct 2, and have compiled it through CocoonJS (or your own methods). But when you try to publish it, it says you need to sign and align it. What does that mean? Well, it means we need to attach your very own keystore to the application, and align it all

What You'll Need

*JDK 6 (Download: 64-bit/32-bit ; If neither of those work for you, take your pick here. You could probably use a more recent version, such as JDK 6u45, as long as it's not JDK 7. Google Play Store doesn't play well with JDK 7, and the easiest thing to do right now is keep true with JDK 6.)

UPDATE: You'd think the problems with signing in JDK 7 would be fixed in JDK 8. That IS NOT the case. Please use JDK 6 for this tutorial.

*ADT Bundle (You can download this here. Extract the .zip if necessary and place it somewhere accessible. I recommend making a new directory in your C:/ drive such as C:/android.

*Access to Command Line (Standard Level Access Will Do)

Define Your Environment Variables

Here's a short video on how to define your enviornment variables once everything is installed. Do this only if it hasn't been done already.

Highlights:

*Open 2 Windows Explorer Windows

*Navigate to C:/Program Files/Java/"JDK Directory"/bin

*Navigate to C:/"ADT DIRECTORY"/sdk/tools

*Right-Click on Computer, then Properties

*Click Advanced System Settings

*Click Environment Variables

*Upper pane should have TEMP selected

*Lower pane should have Path selected

*Click Edit

*Move Cursor to Far End

*Insert Semicolon

*Insert path to ADT

*Insert Semicolon

*Insert path to JDK

*Click OK

*Click OK/Close All Remaining Windows

*No Restart Necessary

Creating A Keystore

1. Click Start (Click Run if In Windows Xp)

2. Type "cmd" (Exclude the Quotes)

3. Click OK or click "cmd.exe"

4. A black window should open

5. Now let's put together a command to execute. I recommend typing this somewhere other than the command line at first. We'll paste it in later. Let's take a look at the genkey command:

    keytool -genkey -v -keystore PATH/TO/YOUR_RELEASE_KEY.keystore -alias YOUR_ALIAS_NAME -keyalg RSA -keysize 2048 -validity 10000

This command will create a keystore. The keystore is a little file that contains your name, organization and shows the app is yours and that its validity is verified. You're going to need to substitute your own values wherever you see the capital letters.

"PATH/TO/YOUR_RELEASE_KEY.keystore": Type the path in which you want to store the file.

"YOUR_ALIAS_NAME": Give the keystore a name you'll remember.

Your code should look a little like this:

     keytool -genkey -v -keystore c:/users/bob/desktop/appkey.keystore -alias MyKey -keyalg RSA -keysize 2048 -validity 10000


Now paste this code into the command line if you haven't done so already, and press Enter The prompt will ask you some questions. Type the appropriate answer, and press Enter. The prompt will automatically move to the next question. (Find your country code here!) Once you've answered all the questions, the prompt will ask you to confirm. If everything is correct, type the letter y and press Enter. If no errors appear, and the keystore appears in the directory of your choice, then you've successfully created a keystore!

Signing Your Keystore

Now that we have a keystore, let's sign it to your app. Start by making sure your .apk file is in the same directory as your .keystore file. This makes the process much easier. Let's take a look at the jarsigner command:

    jarsigner -verbose -keystore PATH/TO/YOUR_RELEASE_KEY.keystore -storepass YOUR_STORE_PASS -keypass YOUR_KEY_PASS PATH/TO/YOUR_UNSIGNED_PROJECT.apk YOUR_ALIAS_NAME


This command will sign your keystore to your app. Once again, you'll need to specify your own values where you see the capital letters.

PATH/TO/YOUR_RELEASE_KEY.keystore: The path to your keystore.

YOUR_STORE_PASS: The password you selected when you created a keystore.

YOUR_KEY_PASS: This should be the same as YOUR_STORE_PASS unless you've changed it.

PATH/TO/YOUR_UNSIGNED_PROJECT.apk: The path to your unsigned app.

YOUR_ALIAS_NAME: The alias you selected when you created a keystone.

Your code should look a little like this:

    jarsigner -verbose -keystore c:/users/bob/desktop/appkey.keystore -storepass pa$$w0rd -keypass pa$$w0rd c:/users/bob/desktop/app_unsigned.apk MyKey



If no errors are returned and your screen looks something similar like this, then you've successfully signed your android app!

Aligning Your App

We're almost done! Now we just need to align your app, and it'll be ready for publication. We're going to use the zipalign command to do this. Let's take a look at a sample:

    zipalign -v 4 PATH/TO/YOUR_SIGNED_PROJECT.apk PATH/TO/YOUR_SIGNED_AND_ALIGNED_PROJECT.apk


Now let's fill in our own values for the capital letters.

PATH/TO/YOUR_SIGNED_PROJECT.apk: This is the path to your signed project.

PATH/TO/YOUR_SIGNED_AND_ALIGNED_PROJECT.apk: This is the path to your signed and aligned project.

Your code should look something like this:

    zipalign -v 4 c:/ad/shilo_release_unsigned.apk c:/ad/shilo_release_ready.apk



The command prompt should return a success message.

If no other errors appear, then Congratulations! Your app is ready for production! Remember to keep your keystore safe! Google Play will not accept application updates with scattered and inconsistent keystores. Keep it safe, especially if you're not good with coding. If you're going to install this app without publishing, make sure unknown sources are allowed. You can find this option under Settings→Applications→Unknown sources.

Troubleshooting

'jarsigner'/'keytool' is not recognized as an internal or external command, operable program or batch file: Looks like JDK isn't installed, or is installed but is not defined in your environment variables. See the video above for help with variables.

'zipalign' is not recognized as an internal or external command, operable program or batch file: Looks like the ADT Bundle isn't installed, or is installed but is not defined in your environment variables. See the video above for help with variables.

When I try to install my published app, it says "Error this application could not be installed (904)" or "Application Not Signed Correctly": JDK 7 doesn't play well with Google Play. The easiest thing would be to uninstall JDK 7, and install JDK 6. Once that's installed, be sure to redefine your environment variables.

Conclusion

If you've enjoyed this tutorial, please be sure to comment, share and favorite!

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!