How to create a release signed apk file using Gradle?


Once your android application is ready, You need to generate apk with signed .

In this tutorial talks about how to generate a release apk file using gradle file

create a release signed apk file using Gradle

How to generate release unsigned apk file using gradle?

During development, You need to test your application in real devices, you need to generate unsigned apk file.

Here are sequence of steps to generate unsigned apk release In build.gradle file, You can add signingConfigs attribute with below values.

signingConfigs{
    unsigned{
        storePassword = ""
        keyAlias = ""
        keyPassword = ""
    }
}

Next add buildTypes with release information

buildTypes{
    release{
        signingConfig signingConfigs.unsigned
    }
}

Next run below commands

gradle build
gradle assemble

It generates apk name project-release-unsigned.apk file under `appmodule/build/outputs/apk/

How to create android app signed apk released using gradle

For signing and release an apk file, We need following things

  • keyAlias : alias key
  • keyPassword : key to sign a password
  • storePassword : actual password
  • storeFile : keystore file
  • v1SigningEnabled : jar file signing
  • v2SigningEnabled: complete apk signing

This example configure static values in build.gradle Add all this under release property in signingConfigs of build.gradle file

android{
signingConfigs {
            release {
                keyAlias 'alias key'
                keyPassword 'key password'
                storePassword 'store Password'
                storeFile file("${directory}/releaseversion.keystore")
                v1SigningEnabled true
                v2SigningEnabled true

            }

          
        }
}

Next add the below signingConfig under buildTypes section

android{
buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}
}

Next, rebuild your project with gradle command

gradle clean build

It generates signed release apk file and ready to push to google play store.

How to read signing keystore from properties file and generate signed release apk in gradle

First, We have a properties file release.properties in config folder of your project

add the below properties to release.properties

storePassword=password
keyPassword=password-key
keyAlias=key-alias
storeFile=location of keystore file

The values are placeholders, So update with your real values

Add your application build gradle file to read this properties file

def releaseFile = rootProject.file("../../release.properties")
def properties = new Properties()
properties.load(new FileInputStream(releaseFile))

And configure signingConfigs with values read from properties file

android {

    signingConfigs {
            release {
                keyAlias properties['keyAlias']
                keyPassword properties['keyPassword']
                storeFile file(properties['storeFile'])
                storePassword properties['storePassword']
            }
        }
    buildTypes {
        release {
            signingConfig signingConfigs.release
            ...
        }
    }
}

Run gradle command to rebuild and generate api file

gradle clean build

Conclusion

You learned how to generate android app apk in android studio gradle build

  • signed release apk
  • unsigned release apk
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.