3

Using Android Studio how do I get a signed, non-debug and zip aligned APK?

So far I can get a signed one but it gets rejected because it has debugging in it.

I can get a non debug release apk but it gets rejected because it's not zip aligned.

I can zip align it but then I can't upload it because that one is not signed.

Edit: I should mention that I'm on windows. Most everything I've looked at is linux based and difficult to separate linux paths from config paths.

Edit2: Things are on hold at the moment. I updated Android Studio and that killed everything because it comes with gradle 1.9 dependancies but doesn't install gradle 1.9 properly. So I thought I'd download the full installer with gradle 1.9 but the download link gives me the version I started with. I know. I should have known better than to update but given the issues I thought it might actually contain a fix.

Edit3: Problem solved. I have a full answer typed up ready to post but SO won't let me post it until tomorrow.

Jonik
  • 80,077
  • 70
  • 264
  • 372
m12lrpv
  • 997
  • 2
  • 11
  • 18
  • Hi, maybe you can check this link out: http://stackoverflow.com/questions/17604448/gradle-zipalign-task-not-working Hope that can help you! – k3v1n4ud3 Jan 02 '14 at 04:38
  • zipalign isn't the core problem but that link tells me how to automate it. Thanks for that. Now the remaining problem is getting an apk that doesn't have debugging in it and was also signed so that I could run zipalign on it. – m12lrpv Jan 02 '14 at 04:58
  • You shouldn't need to install Gradle 1.9. Most projects use the Gradle wrapper which downloads the right version of Gradle automatically based on the distribution URL you set in the wrapper properties. – Scott Barta Jan 02 '14 at 05:10
  • @Scott Android studio downloaded the Gradle 1.9 files but didn't configure their use properly. I wiped the entire install and reinstalled from scratch. – m12lrpv Jan 02 '14 at 06:05
  • 1
    Does this answer your question? [Android Studio: how to generate signed APK using Gradle?](https://stackoverflow.com/questions/20921456/android-studio-how-to-generate-signed-apk-using-gradle) – Mahozad Feb 18 '22 at 11:25
  • I answered it myself 8 years ago. – m12lrpv Feb 22 '22 at 00:29

4 Answers4

16

All builds are signed, even debug ones (which are signed with a debug key). It's just a matter of setting it up to sign your release builds with the correct key. You can set up a signing config via the Project Structure dialog, or you can edit the build.gradle file by hand, following the instructions in the Gradle Plugin User Guide

Once your build file is set up, you can either generate the release APK from the command line with the command

./gradlew assembleRelease

on Linux or Mac, or on Windows:

gradlew.bat assembleRelease

or in the GUI, you can generate the release build by choosing it from the Build Variants view:

IDE window showing Build Variants view

building the APK, and signing it using the wizard.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • I'm working through that user guide now "When the shipped deployment doesn't work, start from scratch"... From what i've figured out the only builds generated by default are debug builds signed with the debug key which is entirely pointless because they cannot be used for anything but debugging on your own device. Using the Build->Generate signed APK helps you to set up a different signing but it generates an (possibly) signed APK full of debugging information which is also useless. This means the core remaining issue is setting up a build configuration without debugging. – m12lrpv Jan 02 '14 at 06:11
  • You're signing the debug build, not the release build. I updated my answer. – Scott Barta Jan 02 '14 at 17:35
  • The problem was that there was no release build option to select. That configuration was missing. SO finally allows me to post my answer and I included my entire build.gradle so that others newcomers can see a full configuration instead of just a few fragments with no context that is in most threads. – m12lrpv Jan 02 '14 at 21:15
5

I have solved the problem Part 1 : k3v1n4ud3's link did help a lot to coalesce the information. Thank you for that. Here is my entire build.gradle located under the project folder:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.6.+'
        }
    }
    apply plugin: 'android'

    repositories {
        mavenCentral()
    }

    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.0"

        signingConfigs {
            debug {
                storeFile file("debug.keystore")
            }

            release {
                storeFile file("D:\\AndroidStudioProjects\\KeyStore\\Keystore_password1.jks")
                storePassword "password"
                keyAlias "MyAppName"
                keyPassword "password"
            }
        }

        productFlavors {
            free {
                packageName "com.mypackage.myappname"
            }

            paid {
                packageName "com.mypackage.myappname"
            }
        }

        buildTypes {
            debug {
                signingConfig signingConfigs.release
            }

            release {
                signingConfig signingConfigs.release
                debuggable false
                zipAlign true
            }

            /*
            alpha {
                packageNameSuffix ".alpha"
            }
            beta {
                packageNameSuffix ".beta"
            }*/
        }


        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 19
        }
    }

    android.applicationVariants.all { variant ->
        if (variant.buildType.name == "release") {
            switch (variant.name) {
                case "FreeRelease":
                    variant.mergeResources.doFirst {
                        android.sourceSets.debug.setRoot("src/free")
                    }
                    break;
                case "PaidDebug":
                    variant.mergeResources.doFirst {
                        android.sourceSets.debug.setRoot("src/paid")
                    }
                    break;
            }
        }
        else if (variant.buildType.name == "debug") {
            switch (variant.name) {
                case "FreeDebug":
                    variant.mergeResources.doFirst {
                        android.sourceSets.debug.setRoot("src/debug/free")
                    }
                    break;
                case "PaidDebug":
                    variant.mergeResources.doFirst {
                        android.sourceSets.debug.setRoot("src/debug/paid")
                    }
                    break;
            }
        }
    }


    dependencies {
        compile 'com.android.support:appcompat-v7:+'
    }

Part 2: I used the keystore created when I initially used the Build->Generate Signed APK... wizard. Pay attention to the keyalias used. After half a day of banging my head against the wall i had forgotten what I'd typed :-)

Part 3: This thread helped me set up the source folders and understand the flavors. Folder naming convention for gradle build variants

Part 4: With just one AndroidManifest.xml I couldn't use the suffixes on the package names. With suffixes it was rejected when uploading to the device. That becomes a problem when pretty much every example of build.gradle includes suffixes.

Part 5: Use View->Tool Windows->BuildVariants to bring up the build variants. The second column is actually a drop down. Select what you want to build here otherwise it's just going to keep building the debug version. (Why on earth it's not under the build menu or the run/debug configurations is a mystery???)

Part 6: The future... I have to try and work out the flavors and how to set them up as I would eventually like to deploy a free and a paid version off the same code base. I will start signing the debug versions with my own key as well.

Community
  • 1
  • 1
m12lrpv
  • 997
  • 2
  • 11
  • 18
4

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 and not in your build.gradle file:

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

If you are using different gradle build version rather than in which you developed your keystore file, at that time it may affect.

I also faced this problem in my project i do following changes:

set classpath

from classpath 'com.android.tools.build:gradle:2.2.0-alpha3'

to

classpath 'com.android.tools.build:gradle:2.1.2'