Automated Process:
Use this tool (uses the new apksigner from Google):
https://github.com/patrickfav/uber-apk-signer
Disclaimer: Im a developer :)
Manual process:
Step 1: Generate Keystore (only once)
You need to create a keystore once and use it to sign unsigned apk. Use the keytool provided by the JDK found in %JAVA_HOME%/bin/
keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app
Step 2 or 4: Zipalign
zipalign which is a tool provided by the Android SDK found in for example %ANDROID_HOME%/sdk/build-tools/24.0.2/ is a mandatory optimization step if you want to download apk to the Play Store.
zipalign -p 4 my.apk my-aligned.apk
Note: when using the old jarsigner you need to run zipalign AFTER signing. When using the new apksigner method apksigner you do this before you sign up (confusing, I know). Calling zipalign before apksigner works fine , because apksigner preserves the alignment and compression of the APK (unlike jarsigner).
You can check the alignment with
zipalign -c 4 my-aligned.apk
Step 3: Sign and Confirm
Using built-in tools 24.0.2 and later
Use jarsigner , which, like keytool, comes with the JDK distribution found in %JAVA_HOME%/bin/ and use it like this:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my.keystore my-app.apk my_alias_name
and can be checked with
jarsigner -verify -verbose my_application.apk
Using built-in tools 24.0.3 and newer
Android 7.0 introduces the APK v2 subscription scheme, a new application subscription scheme that offers faster application installation time and greater protection against unauthorized changes in the APK files (see here and here for more information). Therefore, Google introduced its own apk subscriber called apksigner (duh!) The script file can be found in %ANDROID_HOME%/sdk/build-tools/24.0.3/ (.jar is located in the /lib subfolder). Use it like this:
apksigner sign --ks my.keystore my-app.apk --ks-key-alias alias_name
and can be checked with
apksigner verify my-app.apk
The official documentation can be found here.