92
votes

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 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Building-and-Tasks 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?

5
@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
sign with v1 (jar signature) or v2 (full apk signature) version from gradle file? solution here: stackoverflow.com/questions/57943259/…user1506104

5 Answers

97
votes

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.

64
votes

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
40
votes

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

2
votes

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

-3
votes

build menu > generate signed apk