简体   繁体   中英

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

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.

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窗口显示Build Variants视图

building the APK, and signing it using the wizard.

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.

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM