Android Studio: how to generate a signed apk using Gradle?

I searched on Google and SO but cannot find the answer.

This is the first time I'm working with a gradle system, and now I'm going to create a signed APK for upload to a Google game (the project is imported from eclipse).

Now I read the part here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Building-and-Tasks , which you must add signingConfigs to your build.gradle

I added these lines, and now I saw that you need to run ./gradlew assembleRelease , but doing this in my cmd "gradle" return is not recognized as an internal or external command, operating program, or batch file. I also tried right-clicking on build.gradle and running it, stating that it was successful, but as soon as I look in the build / apk folder there is only a file named app-debug-unaligned.apk

So, how do I generate a signed apk using the gradle system?

+73
android android-studio android-gradle gradle apk
Jan 04 '14 at 13:13
source share
5 answers

There are three ways to create your assembly according to buildType . (In your case, it is released, but it can be called anything.)

  • Go to the Gradle Task in the right panel of Android Studio and search for assembleRelease or assemble(#your_defined_buildtype) in the "Module Tasks" section

  • Click the "Build Variants" tab in the left pane and select the drop-down list.

  • Go to the 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 build the (only) release, you can use Build > Generate Signed apk . For other types of assembly, only the three above parameters are available.

You can find the generated APK in the module/build directory, which has the name of the assembly type.

+85
Jan 04 '14 at
source share

You can execute any existing Android Studio gradle project and build / sign it from the command line without editing any files. This makes it very pleasant to keep your project in version control, while saving your keys and passwords:

 ./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 
+44
Aug 11
source share

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

and then from your terminal

 gradle assembleRelease 

you will get apk in

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

+18
Sep 05 '14 at 19:58
source share

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

+2
Mar 13 '14 at 17:22
source share

build menu> generate signed apk

0
Jan 04 '14 at 14:41
source share



All Articles