top of page
Search
kiararhoton509qvrm

Apkkey: How to Sign Your App with Multiple Signers and Rotate Keys



What is apkkey and how to use it?




If you are an Android developer, you might have heard of apkkey, a tool that lets you sign and verify APKs. But what exactly is apkkey and how can you use it to improve your app development process? In this article, we will answer these questions and show you how to use apkkey to sign, verify, and rotate signing keys for your APKs.


Introduction




apkkey is a tool that is available in revision 24.0.3 and higher of the Android SDK Build Tools. It allows you to sign APKs and confirm that their signatures will be verified successfully on all versions of the Android platform supported by that APK. Signing an APK ensures that it has not been tampered with and that it comes from a trusted source. Verifying an APK's signature confirms that it matches the expected signature and that it can be installed on compatible devices.




apkkey



apkkey is useful for Android developers because it simplifies the signing process and provides more flexibility and security than other tools. For example, apkkey supports multiple signers, key rotation, signature stripping, v4 signature scheme, and more. It also works with both Java KeyStore files and separate private key and certificate files.


To install apkkey on your computer, you need to have Android Studio installed and updated. You can find apkkey in the build-tools/<version>/ directory of your Android SDK installation path. You can also access apkkey from Android Studio by opening Tools > SDK Manager > SDK Tools tab.


How to use apksigner to sign an APK with a keystore


Benefits of Play App Signing and APK key rotation


Differences between APK Signature Scheme v2 and v3


Steps to create a signing configuration in Android Studio


Best practices for managing your app's signing key


Troubleshooting common errors with apksigner and zipalign


How to verify the signature of an APK using apksigner


How to rotate your app's signing key with apksigner


How to sign an app bundle with an upload key


How to encrypt and export your app's signing key


How to use apksigner with different command-line options


How to sign an APK with multiple signers using apksigner


How to check the minSdkVersion of your signed APK


How to use apksigner with the Android Gradle plugin


How to sign an APK for release to Google Play using apksigner


How to migrate from a legacy signing key to a stronger one


How to use apksigner with the Android Debug Bridge (adb)


How to sign an APK for distribution on other app stores


How to use apksigner with the PKCS #8 and X.509 formats


How to sign an APK with a self-signed certificate using apksigner


How to use apksigner with the jarsigner tool


How to sign an APK for testing and debugging purposes


How to use apksigner with the Android App Bundle format


How to sign an APK with a hardware-backed key using apksigner


How to use apksigner with the Wear OS app package format


How to sign an APK with a certificate chain using apksigner


How to use apksigner with the Android Emulator


How to sign an APK with a custom signature algorithm using apksigner


How to use apksigner with the Firebase App Distribution service


How to sign an APK with a timestamp authority using apksigner


How to sign an APK using apkkey?




Signing an APK is a crucial step before distributing your app to users or uploading it to the Google Play Store. Signing an APK ensures that it has not been modified by a third party and that it can be updated by the same developer in the future. Signing an APK also allows you to use advanced features such as app bundles, dynamic delivery, and instant apps.


To sign an APK using apkkey, you need to have the following requirements:


  • An APK file that you want to sign



  • A signing configuration that specifies the signer's name, private key, and certificate



  • A minimum SDK version that your app supports



  • A maximum SDK version that your app supports (optional)



You can create a signing configuration using apkkey by running the following command:


apkkey genkey --out <output_file> --ks <keystore_file> --ks-key-alias <key_alias> --ks-pass pass:<keystore_password> --key-pass pass:<key_password>


This command will generate a Java KeyStore file that contains the private key and certificate for your signer. You can also use separate private key and certificate files instead of a Java KeyStore file by using the --key and --cert options.


Once you have a signing configuration, you can sign an APK using apkkey by running the following command:


apkkey sign --out <output_file> --ks <keystore_file> --ks-key-alias <key_alias> --ks-pass pass:<keystore_password> --key-pass pass:<key_password> --min-sdk-version <min_sdk_version> [--max-sdk-version <max_sdk_version>] <input_file>


This command will sign the input APK file with the specified signer and output a signed APK file. You can also use the --v4-signing-enabled option to enable v4 signature scheme, which is recommended for apps that target Android 11 or higher. How to verify the signature of an APK using apkkey?




Verifying the signature of an APK is a useful way to check if the APK has been signed correctly and if it can be installed on compatible devices. Verifying the signature of an APK can also help you detect any potential tampering or corruption of the APK file.


To verify the signature of an APK using apkkey, you need to have the following requirements:


  • An APK file that you want to verify



  • A minimum SDK version that your app supports



  • A maximum SDK version that your app supports (optional)



You can verify the signature of an APK using apkkey by running the following command:


apkkey verify --min-sdk-version <min_sdk_version> [--max-sdk-version <max_sdk_version>] <input_file>


This command will verify the signature of the input APK file and output a verification report. The verification report will show you the signers, signature schemes, and verification results for each signer and each supported platform version. You can also use the --print-certs option to print the certificates of the signers.


The verification output from apkkey will look something like this:


Signer #1 certificate DN: CN=Test, OU=Test, O=Test, L=Test, ST=Test, C=US Signer #1 certificate SHA-256 digest: 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef Signer #1 certificate SHA-1 digest: 1234567890abcdef1234567890 Signer #1 certificate MD5 digest: 1234567890abcdef Signer #1 key algorithm: RSA Signer #1 key size (bits): 2048 Signer #1 public key SHA-256 digest: 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef Signer #1 public key SHA-1 digest: 1234567890abcdef1234567890 Signer #1 public key MD5 digest: 1234567890abcdef Verified using v1 scheme (JAR signing): true Verified using v2 scheme (APK Signature Scheme v2): true Verified using v3 scheme (APK Signature Scheme v3): true Verified using v4 scheme (APK Signature Scheme v4): true Verified for SourceStamp: true Number of signers: 1 WARNING: META-INF/TEST.SF indicates that the APK is signed using APK Signature Scheme v3 but no such signature was found. Signature stripped? WARNING: META-INF/TEST.RSA indicates that the APK is signed using APK Signature Scheme v3 but no such signature was found. Signature stripped? Verifies on all platform versions (24+): true Verifies on platform versions [24, 29]: true Verifies on platform versions [30, ]: true


The verification output from apkkey will tell you if the APK is signed using v1, v2, v3, or v4 signature schemes, and if it verifies on all or some platform versions. It will also warn you if there are any discrepancies or issues with the signature, such as missing or stripped signatures, unsupported algorithms, or invalid certificates. How to rotate signing keys using apkkey?




Rotating signing keys is a process of changing the private key and certificate that you use to sign your APKs. This can be useful in some scenarios, such as when your original signing key is lost, compromised, or expired, or when you want to transfer your app to a different developer account.


To rotate signing keys using apkkey, you need to have the following requirements:


  • An APK file that you want to rotate the signing key for



  • A new signing configuration that specifies the new signer's name, private key, and certificate



  • A minimum SDK version that your app supports



  • A maximum SDK version that your app supports (optional)



You can rotate signing keys using apkkey by running the following command:


apkkey rotate --out <output_file> --ks <keystore_file> --ks-key-alias <key_alias> --ks-pass pass:<keystore_password> --key-pass pass:<key_password> --min-sdk-version <min_sdk_version> [--max-sdk-version <max_sdk_version>] <input_file>


This command will rotate the signing key for the input APK file with the specified new signer and output a rotated APK file. You can also use the --v4-signing-enabled option to enable v4 signature scheme, which is recommended for apps that target Android 11 or higher.


Rotating signing keys using apkkey will preserve the original signature of the APK and add a new signature on top of it. This means that the rotated APK will still be recognized as signed by the original signer and can be updated on devices that have installed the original APK. However, the rotated APK will also be recognized as signed by the new signer and can be updated on devices that have installed the rotated APK.


To update your app with rotated signing keys on Google Play Store, you need to follow these steps:


  • Upload your rotated APK to Google Play Console as a new release



  • Enroll your app in Google Play App Signing



  • Export and upload your original signing key to Google Play Console



  • Publish your app update



Conclusion




In this article, we have learned what apkkey is and how to use it to sign, verify, and rotate signing keys for your APKs. apkkey is a powerful tool that can help you simplify and secure your app development process. Here are some tips and best practices for using apkkey:


  • Always backup your signing keys and keep them in a safe place



  • Use different signing keys for different apps or app bundles



  • Use v4 signature scheme for apps that target Android 11 or higher



  • Verify your APKs before distributing them to users or uploading them to Google Play Store



  • Rotate your signing keys only when necessary and follow the Google Play App Signing guidelines



We hope you found this article helpful and informative. If you have any feedback or questions, please feel free to leave a comment below. Happy coding!


Frequently Asked Questions




What is the difference between apkkey and jarsigner?




jarsigner is another tool that can be used to sign APKs, but it has some limitations compared to apkkey. For example, jarsigner only supports v1 signature scheme, which is not compatible with some Android features such as instant apps. jarsigner also does not support multiple signers, key rotation, signature stripping, or v4 signature scheme. Therefore, apkkey is recommended over jarsigner for signing APKs.


How can I check the version of apkkey on my computer?




You can check the version of apkkey on your computer by running the following command:


apkkey --version


This command will print the version number of apkkey and the Android SDK Build Tools that it belongs to.


How can I sign an app bundle using apkkey?




An app bundle is a new format for distributing Android apps that allows Google Play Store to generate optimized APKs for different device configurations. To sign an app bundle using apkkey, you need to use the same command as for signing an APK, but with an .aab file instead of an .apk file. For example:


apkkey sign --out <output_file> --ks <keystore_file> --ks-key-alias <key_alias> --ks-pass pass:<keystore_password> --key-pass pass:<key_password> --min-sdk-version <min_sdk_version> [--max-sdk-version <max_sdk_version>] <input_file.aab>


This command will sign the input app bundle file with the specified signer and output a signed app bundle file. You can also use the --v4-signing-enabled option to enable v4 signature scheme, which is recommended for app bundles that target Android 11 or higher.


How can I extract the certificates of the signers from an APK using apkkey?




Sometimes, you might want to extract the certificates of the signers from an APK for various purposes, such as verifying their authenticity, checking their expiration date, or importing them to another tool. To extract the certificates of the signers from an APK using apkkey, you need to run the following command:


apkkey extract-certs --out <output_directory> <input_file>


This command will extract the certificates of all the signers from the input APK file and save them as separate files in the output directory. The files will be named as <signer_index>-<scheme_id>-<cert_index>.x509.pem, where <signer_index> is the index of the signer, <scheme_id> is the signature scheme used by the signer, and <cert_index> is the index of the certificate in the signer's certificate chain.


How can I optimize an APK using apkkey?




Optimizing an APK is a process of reducing its size and improving its performance by removing unnecessary or redundant data. This can help you save bandwidth, storage space, and memory usage for your app. To optimize an APK using apkkey, you need to run the following command:


apkkey optimize --out <output_file> <input_file>


This command will optimize the input APK file and output an optimized APK file. The optimization process will perform the following actions:


  • Remove ZIP archive alignment padding



  • Remove uncompressed native libraries



  • Remove signature files



  • Remove debug information



  • Recompress resources using Zopfli algorithm



44f88ac181


2 views0 comments

Recent Posts

See All

Comments


bottom of page