Automatic Signed the android release APK by using keystore from build.gradle of android studio

As an android developer, we faced many challenges to build and deploy the app to the market. Here I am going to discuss one of the challenges is signed the APK from the newly created Keystore or exiting Keystore from the android studio.

As we know the android studio having two different build types named debug and release. Generally, when we run the application in debug mode than by default the android studio signed the APK from debugging Keystore. We can find the debug Keystore in the corresponding home directory of the system which is used to sign the APK by the android studio. The default name for the Keystore is that has a Keystore password .

In debug mode, we do not face any problem because it handles by an android studio internally. Generally, We faced the challenge in release mode which we are going to discuss in this article to sign the release APK from exiting keyStore by using the android studio. Let’s discuss all the steps for signing the APK in release mode.

KeyStore File(exp name.jks)

In the very first step, we need to generate the Keystore to sign the APK from the android studio. If you have already generated the Keystore then we can use the exiting Keystore else we can create the new one from the android studio.

Go to the top sidebar of the android studio, and click on build and select to Generate the Signed the APK, it will open a pop-up window to ask for sign the APK from the exiting Keystore or create the new key store. Here is a screenshot for creating the new Keystore.

 

Here we need to fill all the needed information with alias key and password if it is already created which needs to remember while generating the signed APK. But here I am going to use this key and password inside the Keystore.properties file to avoid remembering the stuff. After completing all the information it will generate the .jks file inside the chosen directory.

Create a Keystore.properties file inside your project :

storePassword=android
keyPassword=android
keyAlias=androidalias
storeFile=keystore/test_sign.jks

 

Please paste the created .jks file inside the Keystore folder. Here I had chosen the alias key is “androidalias” and password is “android”. It’s up to you to decide the key alias and password.  I need to create the Keystore folder inside the app of your project and paste the created Keystore (.jks) file.

Configure the build config to automatic sign the APK:

From the official document:

  1. In the Project window, right-click on your app and click Open Module Settings.
  2. On the Project Structure window, under Modules in the left panel, click the module you would like to sign.
  3. Click the Signing tab, then click Add .
  4. Select your Keystore file, enter a name for this signing configuration (as you may create more than one), and enter the required information.

Once you entered the required information, it will generate the code inside the build.gradle file.

android {
    signingConfigs {
        config {
            keyAlias "here aliase"
            keyPassword "here aliase"
            storeFile "here file"
            storePassword "here password"
        }
    }
.....
}

 

Note:

It is not best practice to put the passwords as hardcoded constants in the build file. A better approach would be to put them in a separate

 

file that is not part of the Version Control System (

VCS

).

Remove signing information from your build files:

We can write all the secret information like alias and password into the Keystore.properties file as I shared above of this article and give access to this file to build.gradle to read information.

 

// 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.
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    signingConfigs {
        config {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        debug {
          /// set for debug
        }
        release {
             minifyEnabled true
             signingConfig signingConfigs.config
        }
    }
}

 

Now we need to just select the release from build variant and build the APK. It will take a moment to generate the release APK to the corresponding directory.

That’s all.

Wrapping 

Now we have a good understanding of signing the APK in release mode.

If you are wondering to learn Android then Please learn from Android category and wondering to learn Kotlin then Kotlin Category will help you. If you want to learn all the python article, then learn from the python category.

Happy Coding 🙂

5 1 vote
Article Rating

Recent Posts

Hide your production API key or any sensitive data in Android

Hi everyone, In this article, we are going to learn how to hide the production… Read More

2 years ago

How to handle the localisation or multi language support in android with examples?

Hello everyone, Today in this article, we are going to learn about localisation to support… Read More

2 years ago

How to convert any callback to Coroutines and use them in Kotlin Android?

Hello everyone, In this article, we are going to learn something to handle the callback… Read More

2 years ago

Request Permission Launcher with Kotlin in Android

In this article, we are learning about the run time permissions for request permission launchers.… Read More

2 years ago

Implement the SMS User Consent API and SMS Retriever API in Android

Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More

3 years ago

Jetpack Compose Coroutine flow with LiveData/ViewModel in Android

Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More

3 years ago