Signing Android App With Gradle Using CLI

Recently I stumbled upon a problem of creating a signed Android app via command line, after spending some time on this, I have decided to post following tutorial.
This problem exist in 2 situations. For example when you want to share your application publicly, but do not want to share any details about your signing key or you would like to automate building process using some kind of CI server.
  1. Create a new key store using Android studio (or use existing one), this document shows how to create a new keystore:http://developer.android.com/tools/publishing/app-signing.html#studio
  2. After your key is created you need to setup gradle properties like this. I have edited following file:/home/w/.gradle/gradle.properties
KEYSTORE=/home/w/android/android-studio.jks
KEYSTORE_PASSWORD=my_keystore_password
KEY_ALIAS=android-studio
KEY_PASSWORD=my_key_password
  1. This file is later used in build.gradle file like this:
apply plugin: 'com.android.application'


android {
    compileSdkVersion 20
    buildToolsVersion "20"

    defaultConfig {
        applicationId "com.wlangiewicz.test"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    signingConfigs {
        release {
            storeFile     file(KEYSTORE)
            storePassword KEYSTORE_PASSWORD
            keyAlias      KEY_ALIAS
            keyPassword   KEY_PASSWORD
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            runProguard false
        }
        debug {
            versionNameSuffix "-SNAPSHOT"
        }
    }
}
Please note the signingConfigs option, these are read by gradle from the previously created file.
  1. To build singed application just run:
./gradlew assembleRelease
From the root folder of your repository.
  1. This will create signed apk file in app/build/outputs/apk. File will be called app-release.apk