Creating a Keystore and Release APK on Linux: A Detailed Guide with Lost Keystore Help

DevapraveenDevapraveen
3 min read

As a mobile app developer, you might occasionally encounter unorganized environments leading to the loss of your keystore for a production app. This can prevent you from deploying updated versions of your app on the Play Store. Deleting the existing mobile application is not an option, as it would result in losing your current subscribers. I've faced this issue in previous companies and, with the help of Google support, managed to resolve it. I created this guide to assist other React Native or Android app developers who might face similar challenges. I hope this post will be a valuable resource for you.

Prerequisites

First, you need to install the JDK on your Linux system. Use the following command:

sudo apt-get install default-jdk

To verify the JDK installation path on your machine, use these commands:

which java
readlink -f $(which java)

You should see a path like /usr/lib/jvm/java-11-openjdk-amd64/bin. Navigate to this directory to create the keystore file.

Generating a Keystore

To generate a new keystore file, use the following command:

sudo keytool -genkeypair -alias my-key-alias -keyalg RSA -keysize 2048 -validity 9125 -keystore my-release-key.keystore

Replace my-release-key.keystore with the desired name for your keystore file and my-key-alias with your key alias.

You can find the alias and keystore name in the gradle.properties file under your Android directory in your React Native app.

To view the keystore details, use this command:

keytool -list -v -keystore my-release-key.keystore

Move the generated keystore to your android/app/ directory in your React Native app.

Building the Release APK

First, clean the Gradle build to remove any previously generated resources:

cd android
./gradlew clean

Delete the build directories within your Android project to ensure that any cached or intermediate build files causing conflicts are removed. Run the following commands from your project's root directory:

rm -rf android/app/build
rm -rf android/build

To generate the release AAB build, use the following command:

cd android
./gradlew bundleRelease

To generate the release APK build, use this command:

cd android
./gradlew assembleRelease

Regenerating a Lost Keystore

If you have lost your keystore, follow these instructions to generate and register a new upload key:

  1. Generate a new key. It must be different from any previous keys, be a 2048-bit RSA key, and have a 25-year validity. Use the following command line to generate a new key:

     keytool -genkeypair -alias upload -keyalg RSA -keysize 2048 -validity 9125 -keystore keystore.jks
    
  2. Export the certificate for that key to PEM format:

keytool -export -rfc -alias upload -file upload_certificate.pem -keystore keystore.jks
  1. Once you have generated the PEM file, follow these steps:
  • Go to Setup > App integrity > App Signing.

  • Request an Upload key reset.

  • Provide a reason for the key reset request.

  • Enter the PEM file.

  • Click Request.

3
Subscribe to my newsletter

Read articles from Devapraveen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Devapraveen
Devapraveen

Passionate full-stack developer from india. Sharing knowledge and experiences through my blog to help fellow developers tackle challenges and navigate the evolving world of software development.