简体   繁体   English

使用Android Studio如何获得已签名,非调试和zip对齐的APK?

[英]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? 使用Android Studio如何获得已签名,非调试和zip对齐的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. 我可以得到一个非调试版本的apk但它被拒绝,因为它不是拉链对齐。

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. 编辑:我应该提到我在Windows上。 Most everything I've looked at is linux based and difficult to separate linux paths from config paths. 我所看到的大部分内容都是基于Linux的,很难将linux路径与配置路径分开。

Edit2: Things are on hold at the moment. 编辑2:目前情况暂停。 I updated Android Studio and that killed everything because it comes with gradle 1.9 dependancies but doesn't install gradle 1.9 properly. 我更新了Android Studio并杀死了所有内容,因为它带有gradle 1.9 dependancies,但没有正确安装gradle 1.9。 So I thought I'd download the full installer with gradle 1.9 but the download link gives me the version I started with. 所以我想我会用gradle 1.9下载完整的安装程序,但是下载链接给了我开始的版本。 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. 编辑3:问题解决了。 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 您可以通过“项目结构”对话框设置签名配置,也可以按照“ Gradle插件用户指南”中的说明手动编辑build.gradle文件。

Once your build file is set up, you can either generate the release APK from the command line with the command 设置构建文件后,您可以使用命令从命令行生成发布APK

./gradlew assembleRelease

on Linux or Mac, or on Windows: 在Linux或Mac或Windows上:

gradlew.bat assembleRelease

or in the GUI, you can generate the release build by choosing it from the Build Variants view: 或者在GUI中,您可以通过从Build Variants视图中选择它来生成发布版本:

IDE窗口显示Build Variants视图

building the APK, and signing it using the wizard. 构建APK,并使用向导对其进行签名。

I have solved the problem Part 1 : k3v1n4ud3's link did help a lot to coalesce the information. 我已经解决了问题第1部分:k3v1n4ud3的链接确实有助于合并信息。 Thank you for that. 谢谢你。 Here is my entire build.gradle located under the project folder: 这是我在项目文件夹下的整个build.gradle:

    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. 第2部分:我使用了最初使用Build-> Generate Signed APK ...向导时创建的密钥库。 Pay attention to the keyalias used. 注意使用的keyalias。 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. 第3部分:这个线程帮助我设置源文件夹并理解风格。 Folder naming convention for gradle build variants gradle构建变体的文件夹命名约定

Part 4: With just one AndroidManifest.xml I couldn't use the suffixes on the package names. 第4部分:只有一个AndroidManifest.xml我无法在包名上使用后缀。 With suffixes it was rejected when uploading to the device. 使用后缀时,上传到设备时会被拒绝。 That becomes a problem when pretty much every example of build.gradle includes suffixes. 当build.gradle的每个例子都包含后缀时,这就成了一个问题。

Part 5: Use View->Tool Windows->BuildVariants to bring up the build variants. 第5部分:使用View-> Tool Windows-> BuildVariants来调出构建变体。 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. 第6部分:未来......我必须尝试解决这些问题以及如何设置它们,因为我最终希望在相同的代码库中部署免费和付费版本。 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. 可以使用任何现有的Android Studio gradle项目,并从命令行构建/签名,而无需编辑任何文件。 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: 这使得将项目存储在版本控制中同时保持密钥和密码分离而不是build.gradle文件非常好:

./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. 如果您使用的是不同的gradle构建版本而不是您开发密钥库文件,那么它可能会影响。

I also faced this problem in my project i do following changes: 我也在我的项目中遇到了这个问题,我做了以下更改:

set classpath 设置classpath

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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