我想有我的Gradle构建创建一个发布签名apk文件使用Gradle。
我不确定代码是否正确,或者我在做gradle构建时是否缺少一个参数?
这是build.gradle/build.gradle中的一些代码。节文件:
android {
...
signingConfigs {
release {
storeFile(file("release.keystore"))
storePassword("******")
keyAlias("******")
keyPassword("******")
}
}
}
Gradle构建成功完成,在我的build/apk文件夹中,我只看到…-release-unsigned.apk和…-debug-unaligned.apk文件。
对如何解决这个问题有什么建议吗?
我有几个问题,我把下面的行放在错误的地方:
signingConfigs {
release {
// We can leave these in environment variables
storeFile file("d:\\Fejlesztés\\******.keystore")
keyAlias "mykey"
// These two lines make gradle believe that the signingConfigs
// section is complete. Without them, tasks like installRelease
// will not be available!
storePassword "*****"
keyPassword "******"
}
}
确保你把signingConfigs部分放在了android部分:
android
{
....
signingConfigs {
release {
...
}
}
}
而不是
android
{
....
}
signingConfigs {
release {
...
}
}
很容易犯这个错误。
您可以从命令行请求密码:
...
signingConfigs {
if (gradle.startParameter.taskNames.any {it.contains('Release') }) {
release {
storeFile file("your.keystore")
storePassword new String(System.console().readPassword("\n\$ Enter keystore password: "))
keyAlias "key-alias"
keyPassword new String(System.console().readPassword("\n\$ Enter keys password: "))
}
} else {
//Here be dragons: unreachable else-branch forces Gradle to create
//install...Release tasks.
release {
keyAlias 'dummy'
keyPassword 'dummy'
storeFile file('dummy')
storePassword 'dummy'
}
}
}
...
buildTypes {
release {
...
signingConfig signingConfigs.release
}
...
}
...
if-then-else块防止在构建版本时请求密码。虽然else分支是不可达的,它欺骗Gradle创建一个安装…发布的任务。
基本信息。正如https://stackoverflow.com/a/19130098/3664487所指出的,“Gradle脚本可以使用System.console()提示用户输入。readLine方法。”不幸的是,Gradle总是会要求密码,即使你在构建一个调试版本(参见如何使用Gradle创建一个发布签名apk文件?)幸运的是,这是可以克服的,如上所述。
如果您想避免在构建中硬编码您的密钥库和密码。gradle,你可以使用属性文件解释这里:处理签名配置与gradle
基本上:
1)创建一个myproject。属性文件在/home/[username]/。签名内容如下:
keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********
2)创建gradle。属性文件(可能在你的项目目录的根目录)的内容:
MyProject.properties=/home/[username]/.signing/myproject.properties
3)在你的构建中参考它。这样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']
}
}
}