本文翻译自:How to create a release signed apk file using Gradle?

I would like to have my Gradle build to create a release signed apk file using Gradle. 我想使用Gradle构建Gradle版本以创建发布签名的apk文件。

I'm not sure if the code is correct or if I'm missing a parameter when doing gradle build ? 我不确定代码是否正确或在gradle build时是否缺少参数?

This is some of the code in my gradle file: 这是我的gradle文件中的一些代码:

android {...signingConfigs {release {storeFile file("release.keystore")storePassword "******"keyAlias "******"keyPassword "******"}}
}

The gradle build finishes SUCCESSFUL, and in my build/apk folder I only see the ...-release-unsigned.apk and ...-debug-unaligned.apk files. gradle构建成功完成,在我的build/apk文件夹中,我仅看到...-release-unsigned.apk...-debug-unaligned.apk文件。

Any suggestions on how to solve this? 关于如何解决这个问题有什么建议吗?


#1楼

参考:https://stackoom.com/question/1Eu8g/如何使用Gradle创建发布签名的apk文件


#2楼

I managed to solve it adding this code, and building with gradle build : 我设法通过添加此代码来解决它,并使用gradle build

android {...signingConfigs {release {storeFile file("release.keystore")storePassword "******"keyAlias "******"keyPassword "******"}}buildTypes {release {signingConfig signingConfigs.release}}
}

This generates a signed release apk file. 这将生成一个签名的发行版apk文件。


#3楼

This is a reply to user672009 and addition to sdqali's post (his code will crash on building debug version by IDE's "Run" button): 这是对user672009的答复, 也是对sdqali帖子的补充(他的代码将在IDE的“运行”按钮生成调试版本时崩溃):

You can use the following code: 您可以使用以下代码:

final Console console = System.console();
if (console != null) {// Building from console signingConfigs {release {storeFile file(console.readLine("Enter keystore path: "))storePassword console.readLine("Enter keystore password: ")keyAlias console.readLine("Enter alias key: ")keyPassword console.readLine("Enter key password: ")}}} else {// Building from IDE's "Run" buttonsigningConfigs {release {}}}

#4楼

Note that @sdqali's script will (at least when using Gradle 1.6) ask for the password anytime you invoke any gradle task. 请注意,@ sdqali的脚本将(至少在使用Gradle 1.6时)在您调用任何 gradle任务时要求输入密码。 Since you only need it when doing gradle assembleRelease (or similar), you could use the following trick: 由于仅在进行gradle assembleRelease (或类似操作)时才需要它,因此可以使用以下技巧:

android {...signingConfigs {release {// We can leave these in environment variablesstoreFile file(System.getenv("KEYSTORE"))keyAlias System.getenv("KEY_ALIAS")// These two lines make gradle believe that the signingConfigs// section is complete. Without them, tasks like installRelease// will not be available!storePassword "notYourRealPassword"keyPassword "notYourRealPassword"}}...
}task askForPasswords << {// Must create String because System.readPassword() returns char[]// (and assigning that below fails silently)def storePw = new String(System.console().readPassword("Keystore password: "))def keyPw  = new String(System.console().readPassword("Key password: "))android.signingConfigs.release.storePassword = storePwandroid.signingConfigs.release.keyPassword = keyPw
}tasks.whenTaskAdded { theTask -> if (theTask.name.equals("packageRelease")) {theTask.dependsOn "askForPasswords"}
}

Note that I also had to add the following (under android) to make it work: 请注意,我还必须添加以下内容(在android下)以使其工作:

buildTypes {release {signingConfig signingConfigs.release}
}

#5楼

If you want to avoid hardcoding your keystore & password in build.gradle , you can use a properties file as explained here: HANDLING SIGNING CONFIGS WITH GRADLE 如果要避免在build.gradle中对您的密钥库和密码进行硬编码,则可以按照以下说明使用属性文件: 使用GRUNDLE处理签名配置

Basically: 基本上:

1) create a myproject.properties file at /home/[username]/.signing with such contents: 1)在/ home / [用户名] /。signing中创建一个myproject.properties文件,其内容如下:

keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********

2) create a gradle.properties file (perhaps at the root of your project directory) with the contents: 2)使用内容创建gradle.properties文件(也许在项目目录的根目录):

MyProject.properties=/home/[username]/.signing/myproject.properties

3) refer to it in your build.gradle like this: 3)像这样在build.gradle中引用它:

    if(project.hasProperty("MyProject.properties")&& new File(project.property("MyProject.properties")).exists()) {Properties props = new Properties()props.load(new FileInputStream(file(project.property("MyProject.properties"))))signingConfigs {release {storeFile file(props['keystore'])storePassword props['keystore.password']keyAlias props['keyAlias']keyPassword props['keyPassword']}}
}

#6楼

Easier way than previous answers: 比以前的答案更简单的方法:

Put this into ~/.gradle/gradle.properties 将其放入~/.gradle/gradle.properties

RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****

Modify your build.gradle like this: 像这样修改build.gradle

...
signingConfigs {release {storeFile file(RELEASE_STORE_FILE)storePassword RELEASE_STORE_PASSWORDkeyAlias RELEASE_KEY_ALIASkeyPassword RELEASE_KEY_PASSWORD}
}buildTypes {release {signingConfig signingConfigs.release}
}
....

Then you can run gradle assembleRelease 然后您可以运行gradle assembleRelease

如何使用Gradle创建发布签名的apk文件?相关推荐

  1. apk私钥_Android创建私钥并为APK文件签名

    Android创建私钥并为APK文件签名的相关命令 创建私钥命令: keytool -genkey -keystore .keystore -keyalg RSA -validity -alias 该 ...

  2. Android Studio(十二):打包多个发布渠道的apk文件

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  3. Gradle 3.0.0设置Apk文件输出命名

    为了方便识别apk文件,一般我们都希望通过androoid studio打包的文件,会带上app的名字,时间,之类的,使用多渠道打包的时候,还希望带上渠道名称 以前我都这样做 //修改生成的apk名字 ...

  4. uniapp项目创建打包生成安卓apk文件

    1.下载开发工具 HBuilder X 并安装 下载地址(安装过程不赘述):HBuilderX-高效极客技巧 2.创建uniapp项目 点击左上角文件=>新建=>项目,选择项目类型,填写项 ...

  5. 手把手教你生成正式签名的APK文件

    一,使用Android Studio生成 1. 2. 3.如果先前没有则先点Create new 4. 5. 6. 7.点击Finish 8. 二.使用Gradle生成: 1.在安卓闭包中加上 上图紫 ...

  6. 如何在Eclipse中构建APK文件?

    当我使用Eclipse开发项目时, APK文件会在模拟器上运行. 但我想将我的应用程序上传到真实设备. 有没有工具来构建一个APK文件? 这个过程是什么? 或者是否可以从模拟器中提取APK文件? #1 ...

  7. Android Studio使用签名打包发布APP(安卓生成apk文件)

    一.创建签名密钥库文件 1.在 Build 菜单中选择 Generate Signed Bundle / APK... 2.选择 APK,点击 Next ,进入下一步 3.点击 Create new. ...

  8. 如何给APK文件签名

    转载地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=126421 1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商 ...

  9. [转]Android学习系列(1)--为App签名(为apk签名)

    本文转自:http://www.cnblogs.com/qianxudetianxia/archive/2011/04/09/2010468.html 写博客是一种快乐,前提是你有所写,与人分享,是另 ...

最新文章

  1. Android Intent setAction的使用注意
  2. Linux给用户添加sudo权限
  3. Vue:触发视图更新的hack
  4. 2、Collections操作(自定义类)的各种实现
  5. C++:究竟还有没有未来?
  6. 4thweek.P_problemB .poj1505copy books.二分法
  7. oracle定时向mysql取数据_Oracle中通过Job实现定时同步两个数据表之间的数据
  8. mysql 函数事务_MySQL:函数和事务
  9. selenium+python谷歌驱动配置
  10. arduino步进电机程序库_arduino控制步进电机的库(带有驱动器)
  11. 关于ob函数的使用和应用场景
  12. 超详细三维建模教程【小白专用】
  13. vba宏是什么,如何操作
  14. 学习用PS美化软件界面
  15. html火焰字效果,如何用PS制作火焰字特效
  16. java计算机毕业设计腾讯网游辅助小助手源代码+数据库+系统+lw文档
  17. 2023全新纯净版本知识付费微信小程序源码_附搭建教程_亲测可用
  18. 编码的奥秘:手电筒剖析
  19. 路由器与服务器延迟过高,路由器延迟高什么原因(图文)
  20. VMware Linux虚拟机CPU占用过高

热门文章

  1. RTL5640 path配置
  2. 携程Apollo配置中心架构深度剖析
  3. Flutter GridView详解
  4. oracle 物理dg 逻辑dg,物理DG与逻辑DG的区别与逻辑DG同步异常处理方法
  5. 如果要快速的读写表格,Pandas 并不是最好的选择
  6. mysql压缩包安装的密码忘了怎么办_数据库mysql 8.0.18 压缩包安装及忘记密码重置所遇到的坑...
  7. 【软件测试】软件测试学习笔记(一)
  8. 搭建博客网站详细报告
  9. PNG格式图片(验证码..)不能显示的解决办法
  10. qq阅读java带签名_手机QQ阅读器Java触屏且签名版