Android Studio:如何使用 Gradle 生成签名的 apk?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20921456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:41:45  来源:igfitidea点击:

Android Studio: how to generate signed apk using Gradle?

androidgradleandroid-studioapkandroid-gradle-plugin

提问by Gooey

I've searched on Google and SO but cannot find my answer.

我在 Google 和 SO 上搜索过,但找不到我的答案。

This is the first time I'm working with the gradle system and I am now at the point of generating a signed APK to upload to google play (Project is imported from eclipse).

这是我第一次使用 gradle 系统,我现在正在生成一个签名的 APK 以上传到 google play(项目是从 eclipse 导入的)。

Now I've read the part here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Building-and-Tasksthat you should add signingConfigsto your build.gradle

现在我已经阅读了这里的部分http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Building-and-Tasks你应该添加signingConfigs到你的build.gradle

I've added these lines and now I saw that you need to run ./gradlew assembleReleasebut running this in my cmd returns 'gradle' is not recognized as an internal or external command, operable program or batch file. I've also tried to right click on the build.gradle and run it, saying it was sucessful but once I look in the build/apk folder only a file called app-debug-unaligned.apk

我添加了这些行,现在我看到您需要运行,./gradlew assembleRelease但在我的 cmd 中运行它返回 'gradle' 不被识别为内部或外部命令、可运行的程序或批处理文件。我还尝试右键单击 build.gradle 并运行它,说它成功了,但是一旦我查看 build/apk 文件夹中只有一个名为app-debug-unaligned.apk

So how do I generate the signed apk using the Gradle system?

那么如何使用 Gradle 系统生成签名的 apk 呢?

回答by pyus13

There are three ways to generate your build as per the buildType. (In your case, it's release but it can be named anything you want.)

根据buildType. (在您的情况下,它是发布,但可以命名为您想要的任何名称。)

  1. Go to Gradle Task in right panel of Android Studio and search for assembleReleaseor assemble(#your_defined_buildtype)under Module Tasks

  2. Go to Build Variant in Left Panel and select the build from drop down

  3. Go to project root directory in File Explore and open cmd/terminal and run:

    Linux: ./gradlew assembleRelease or assemble(#your_defined_buildtype)

    Windows: gradlew assembleRelease or assemble(#your_defined_buildtype)

  1. 转到 Android Studio 右侧面板中的 Gradle Task 并在 Module Tasks 下搜索assembleReleaseassemble(#your_defined_buildtype)

  2. 转到左侧面板中的构建变体并从下拉列表中选择构建

  3. 转到文件资源管理器中的项目根目录并打开 cmd/terminal 并运行:

    Linux: ./gradlew assembleRelease or assemble(#your_defined_buildtype)

    视窗: gradlew assembleRelease or assemble(#your_defined_buildtype)

If you want to do a release build (only), you can use Build> Generate Signed apk. For other build types, only the above three options are available.

如果你想做一个发布版本(仅限),你可以使用Build> Generate Signed apk。对于其他构建类型,只有上述三个选项可用。

You can find the generated APK in your module/builddirectory having the build type name in it.

您可以在module/build包含构建类型名称的目录中找到生成的 APK 。

回答by Wayne Piekarski

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:

可以使用任何现有的 Android Studio 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

回答by droidchef

You can use this code

您可以使用此代码

android {
   ...
    signingConfigs {
        release {
            storeFile file("../your_key_store_file.jks")
            storePassword "some_password"
            keyAlias "alias_name"
            keyPassword "key_password"
        }
    }

    buildTypes {

        release {
            signingConfig signingConfigs.release
        }
    }
   ...
}

then from your terminal run

然后从你的终端运行

./gradlew assembleRelease

you will get the apk at

你会在

your-android-app/build/outputs/apk/your-android-app-release.apk

your-android-app/build/outputs/apk/your-android-app-release.apk

回答by douglaszuniga

I think this can help you https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/then just select the Releasefrom the Build Variants

我认为这可以帮助您https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/然后只需ReleaseBuild Variants

回答by trickedoutdavid

build menu > generate signed apk

构建菜单 > 生成签名的 apk