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

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

问题描述:

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

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

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

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).

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

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

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

I've added these lines and now I saw that you need to run ./gradlew assembleRelease but 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

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

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

根据 buildType,可以通过三种方式生成构建.(在您的情况下,它是发布版,但可以命名为您想要的任何名称.)

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.)

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

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

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

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

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

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

Windows:gradlew assembleRelease 或 assemble(#your_defined_buildtype)

如果您想进行发布构建(仅限),您可以使用 Build > Generate Signed apk.对于其他构建类型,仅上述三个选项可用.

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.

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

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