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

后端 未结 4 1681
猫巷女王i
猫巷女王i 2020-12-15 01:50

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.

相关标签:
4条回答
  • 2020-12-15 02:30

    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.

    0 讨论(0)
  • 2020-12-15 02:41

    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.

    0 讨论(0)
  • 2020-12-15 02:42

    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'

    0 讨论(0)
  • 2020-12-15 02:43

    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
    
    0 讨论(0)
提交回复
热议问题