Android Studio: how to generate signed APK using Gradle?

AndroidGradleAndroid StudioApkAndroid Gradle-Plugin

Android Problem Overview


I've searched on Google and SO but cannot find my answer.

This is the first time I'm working with the Gradle system and I am now at the point of generating a signed APK to upload to Google Play (project is imported from eclipse).

Now, I've read the part here that you should add signingConfigs to your build.gradle.

I've added these lines and now I saw that you need to run ./gradlew assembleRelease but running this in my CMD returns

> 'gradle' is not recognized as an internal or external command, operable program or batch file.

I've also tried to right click on the build.gradle and run it, saying it was sucessful but once I look in the build/apk folder only a file called app-debug-unaligned.apk.

So, how do I generate the signed APK using the Gradle system?

Android Solutions


Solution 1 - Android

There are three ways to generate your build as per the buildType. (In your case, it's release but it can be named anything you want.)

  1. Go to Gradle Task in right panel of Android Studio and search for assembleRelease or assemble(#your_defined_buildtype) under Module Tasks

  2. Go to Build Variant in Left Panel and select the build from drop down

  3. Go to project root directory in File Explore and open cmd/terminal and run:

    Linux: ./gradlew assembleRelease or assemble(#your_defined_buildtype)

    Windows: gradlew assembleRelease or assemble(#your_defined_buildtype)

If you want to do a release build (only), you can use Build > Generate Signed apk. For other build types, only the above three options are available.

You can find the generated APK in your module/build directory having the build type name in it.

Solution 2 - Android

It is possible to take any existing Android Studio gradle project and build/sign it from the command line without editing any files. This makes it very nice for storing your project in version control while keeping your keys and passwords separate:

./gradlew assembleRelease -Pandroid.injected.signing.store.file=$KEYFILE -Pandroid.injected.signing.store.password=$STORE_PASSWORD -Pandroid.injected.signing.key.alias=$KEY_ALIAS -Pandroid.injected.signing.key.password=$KEY_PASSWORD

Solution 3 - Android

You can use this code

android {
   ...
    signingConfigs {
        release {
            storeFile file("../your_key_store_file.jks")
            storePassword "some_password"
            keyAlias "alias_name"
            keyPassword "key_password"
        }
    }

    buildTypes {

        release {
            signingConfig signingConfigs.release
        }
    }
   ...
}

then from your terminal run

./gradlew assembleRelease

you will get the apk at

your-android-app/build/outputs/apk/your-android-app-release.apk

Solution 4 - Android

I think this can help you https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/ then just select the Release from the Build Variants

Solution 5 - Android

This is for Kotlin DSL (build.gradle.kts).
You could either define your properties in local.properties file in the project root directory or define them as environment variables (which is especially useful for CIs like GitHub Actions).

// See https://stackoverflow.com/q/60474010
fun getLocalProperty(key: String) = gradleLocalProperties(rootDir).getProperty(key)

fun String?.toFile() = file(this!!)

// Could also use System.getenv("VARIABLE_NAME") to get each variable individually
val environment: Map<String, String> = System.getenv()

android {
    signingConfigs {
        create("MyAppSigningConfig") {
            keyAlias = getLocalProperty("signing.keyAlias") ?: environment["SIGNING_KEY_ALIAS"] ?: error("Error!")
            storeFile = (getLocalProperty("signing.storeFile") ?: environment["SIGNING_STORE_FILE"] ?: error("Error!")).toFile()
            keyPassword = getLocalProperty("signing.keyPassword") ?: environment["SIGNING_KEY_PASSWORD"] ?: error("Error!")
            storePassword = getLocalProperty("signing.storePassword") ?: environment["SIGNING_STORE_PASSWORD"] ?: error("Error!")
            enableV1Signing = true
            enableV2Signing = true
        }
    }

    buildTypes {
        getByName("release") { // OR simply release { in newer versions of Android Gradle Plugin (AGP)
            signingConfig = signingConfigs["MyAppSigningConfig"]
            // ...
        }
    }
}

myProject/local.properties file:

signing.keyAlias=foo
signing.keyPassword=bar
# also called keystore
signing.storePassword=abcdefgh
signing.storeFile=C\:\\my-files\\my-keystore.jks

NOTE: Do NOT add your local.properties file to your version control system (like Git), as it exposes your secret information like passwords etc. to the public (if it's a public repository).

Generate your APK with either of the 3 ways that this answer mentioned.

Solution 6 - Android

If you live in certain countries, be sure to use a VPN.

step1: run this command in the command-line:

keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

it will ask you for some information such as password, name,... and enter them.

step2: create a file name key.properties in your android folder. write these lines in the created file

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key
storeFile=<location of the key store file, such as ~/key.jks>

keep the key.properties file private, always keep a backup of the key.properties file and never publish publicly.

step3: Replace the following lines in app-level Gradle

def keystoreProperties = new Properties()
    def keystorePropertiesFile = rootProject.file('key.properties')
    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    }
android {
signingConfigs {
  release {
    keyAlias keystoreProperties['keyAlias']
    keyPassword keystoreProperties['keyPassword']
    storeFile file(keystoreProperties['storeFile'])
    storePassword keystoreProperties['storePassword']
  }
}
buildTypes {
  release {
    signingConfig signingConfigs.release
  }
}
}

step4:

keytool -list -v -keystore ~/key.jks -alias key -storepass <password> -keypass <password>

step5: I recommend building APK, using android studio. Build > Generate Signed Bundle/APK...

Solution 7 - Android

build menu > generate signed apk

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGooeyView Question on Stackoverflow
Solution 1 - AndroidPiyush AgarwalView Answer on Stackoverflow
Solution 2 - AndroidWayne PiekarskiView Answer on Stackoverflow
Solution 3 - AndroiddroidchefView Answer on Stackoverflow
Solution 4 - AndroiddouglaszunigaView Answer on Stackoverflow
Solution 5 - AndroidMahozadView Answer on Stackoverflow
Solution 6 - AndroidHusenView Answer on Stackoverflow
Solution 7 - AndroidtrickedoutdavidView Answer on Stackoverflow