本文翻译自: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" button
signingConfigs {
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 variables
storeFile 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 = storePw
android.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_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
....
Then you can run gradle assembleRelease
然后您可以运行gradle assembleRelease