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

会有一股神秘感。 提交于 2019-11-28 22:08:35

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

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:

building the APK, and signing it using the wizard.

m12lrpv

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.

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'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!