Getting Signed Release apk’s from the command line
If anyone of you has deployed an application on the play store, you may have most probably used Android Studio’s built in Generate signed apk option.
The generate apk option in android studio
Recently while making the Open Event Apk generator, I had to make release apk’s, so that they could be used by an event organiser to publish their app, plus apk’s had to be signed because if they were not signed, it would be impossible to upload due to checks by Google.
Error shown on the developers console
So since I was building the app using the terminal and I didn’t have the luxury of signing the app using Android studio and I had to look for alternatives. Luckily I found two of them :
Using the Signing configs offered by gradle
Using the Oracle sun jarsigner
First of all the signing configs in gradle is a great way to do this. Most Open source apps use this as a way to put their code out for everyone to view and sucessfully hide any private keys and password.
You just need to add few lines of code in your app level build.gradle file and create a file called keystore.properties
In your keystore.properties, we just need to store the sensitive info and this file will be accessible only to people who are part of the project.
Next we go to the build.gradle and add these lines to read the keystore.properties file and it’s variables
// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
def keystorePropertiesFile = rootProject.file("keystore.properties")
// Initialize a new Properties() object called keystoreProperties.
def keystoreProperties = new Properties()
// Load your keystore.properties file into the keystoreProperties object.
Next we can add the signingConfigs task and reference the values we got above over there
So As you see this is as simple as this but according to my requirements this seemed a bit tedious since a person setting up the apk generator had to make a keystore file, then find the build.gradle and change the path of the keystore file according to the server directories. So this does the trick but this can be so tedious for someone with no technical experience, so I researched on other solutions and then I got it : Jarsigner and Zipalign
First of all,the jarsigner and zipalign are 2 great tools and the best part about them is that both of them work perfectly with a just one line commands. For signing the app :
jarsigner -keystore <keystore_file> -storepass <storepassword> <apknameTosigned> <alias>
and then zipaligning :
zipalign -v 4 <unaligned-apk-location> <path-to-generated-aligned-apk>
So this is it, we finally used these 2 commands to sign and zipalign an apk and it works perfectly fine. Please test and share comments of the demo live @ http://220.127.116.11. Ciao !