96

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?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Gooey
  • 4,740
  • 10
  • 42
  • 76
  • 1
    See http://stackoverflow.com/questions/20876069/using-android-studio-how-do-i-get-a-signed-non-debug-and-zip-aligned-apk/20876737#20876737 – Scott Barta Jan 04 '14 at 15:49
  • @ScottBarta Partially did the trick, once I switched to release mode and ran the gradle I still got an error, but using he build > generate signed apk via the menu it eventually worked out! Thanks I upvoted you at the provided link. – Gooey Jan 04 '14 at 16:26
  • sign with v1 (jar signature) or v2 (full apk signature) version from gradle file? solution here: https://stackoverflow.com/questions/57943259/how-to-use-v1-jar-signature-or-v2-full-apk-signature-from-build-gradle-file/57946742#57946742 – user1506104 Sep 15 '19 at 18:12
  • 1
    Does this answer your question? [How to build a 'release' APK in Android Studio?](https://stackoverflow.com/questions/19619753/how-to-build-a-release-apk-in-android-studio) – Mahozad Feb 18 '22 at 07:37

7 Answers7

105

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.

George Hilliard
  • 15,402
  • 9
  • 58
  • 96
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • 4
    This will only build a signed apk if you have the build configuration in your build.gradle. If you store this in source code management tools or share it with other peoples you should think about separating the credentials of your signing key from your source code. – Janusz Aug 07 '14 at 07:59
  • Build > Generate Signed apk ... its as simple as that – Atul Aug 14 '16 at 17:38
71

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
Wayne Piekarski
  • 3,203
  • 18
  • 19
  • 1
    Since we need to pass the source code to companion, this is the best way to keep our keystore stuff secret. Thanks. – Yeung Dec 14 '15 at 03:26
  • 3
    anyway getting to know these **android.injected.signing.*** properties existence ? – Tixeon Jun 16 '17 at 08:01
  • I got this information from Xav from the Android Studio team, and I don't know if there are any other variables like this. – Wayne Piekarski Jun 16 '17 at 20:23
  • I am getting java.lang.RuntimeException: com.android.ide.common.signing.KeytoolException: Failed to read key XXXXXX from store "C:\Users\somedirectory\Desktop\someKey.jks": No key with alias 'XXXXXX' found in keystore C:\Users\somedirectory\Desktop\someKey.jks. Why is that? – Archie G. Quiñones Jun 10 '19 at 06:52
48

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

droidchef
  • 2,237
  • 1
  • 18
  • 34
2

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

douglaszuniga
  • 331
  • 2
  • 4
0

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.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
0

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...

Husen
  • 185
  • 1
  • 10
-3

build menu > generate signed apk

trickedoutdavid
  • 868
  • 2
  • 8
  • 12
  • If you run that, it tells you that's not the way to do it. Besides if I upload that .apk file it raises an error saying that error detections or something is on. – Gooey Jan 04 '14 at 14:46
  • 11
    it says that debugging is on... in android studio, on the left, there should be a tab that says 'build variants'... click on it and switch everything from 'debug' to 'release'... after that, generate signed apk again... – trickedoutdavid Jan 04 '14 at 16:27
  • 2
    This no longer works! For example the APK it builds has BuildConfig.DEBUG set to true. This has caused me no end of trouble, you must now use the gradlew assembleRelease method. – Ben Clayton Apr 17 '14 at 14:32
  • @BenClayton You are absolutely right! I wasted hours because of this! Another very annoying "bug" in Android Studio - No end in sight with troubles... – sjkm Apr 29 '16 at 10:03
  • I wonder why this simple step cannot be most suitable way to generate signed apk as it clearly mentions. This step generated app and works perfect for me, without any trouble. But lots of other answers allover on SO confused me thinking what I did might not correct way or what. – Atul Aug 14 '16 at 17:35