How to create a release signed apk file using Gradle?

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

This tutorial talks about how to generate a release apk file using the Gradle file.

How to generate release unsigned apk file using Gradle?

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

Here is a sequence of steps to generate an unsigned apk release In the build.gradle file, You can add the signingConfigs attribute below values.

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

Next, add buildTypes with release information.

buildTypes{
    release{
        signingConfig signingConfigs.unsigned
    }
}

Next, run the below commands.

Gradle build
gradle assemble

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

How to create an android app signed apk released using Gradle

For signing and releasing an apk file, We need the following things.

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

This example configures static values in the 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 the Gradle command.

gradle clean build

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

How to read signing Keystore from a properties file and generate signed release apk in Gradle

First, We have a properties file release. properties in the 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 them 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 a properties file.

android {

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

Run the Gradle command to rebuild and generate the API file.

gradle clean build

Conclusion

You learned how to generate an android app apk in android studio Gradle build

  • signed release apk
  • unsigned release apk