我想有我的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文件。
对如何解决这个问题有什么建议吗?
如果你像我一样通过命令行构建apk,那么你可以提供签名配置作为参数。
将此添加到build.gradle中
def getStore = { ->
def result = project.hasProperty('storeFile') ? storeFile : "null"
return result
}
def getStorePassword = { ->
def result = project.hasProperty('storePassword') ? storePassword : ""
return result
}
def getKeyAlias = { ->
def result = project.hasProperty('keyAlias') ? keyAlias : ""
return result
}
def getKeyPassword = { ->
def result = project.hasProperty('keyPassword') ? keyPassword : ""
return result
}
让你的signingconfig像这样
signingConfigs {
release {
storeFile file(getStore())
storePassword getStorePassword()
keyAlias getKeyAlias()
keyPassword getKeyPassword()
}
}
然后像这样执行gradlew
./gradlew assembleRelease -PstoreFile="keystore.jks" -PstorePassword="password" -PkeyAlias="alias" -PkeyPassword="password"
对于Groovy (build.gradle)
您不应该将签名凭据直接放入构建中。gradle文件。相反,凭证应该来自不受版本控制的文件。
放置一个文件签名。特定于模块构建的属性。Gradle被找到了。别忘了把它添加到你的。gitignore文件!
signing.properties
storeFilePath=/home/willi/example.keystore
storePassword=secret
keyPassword=secret
keyAlias=myReleaseSigningKey
build.gradle
android {
// ...
signingConfigs{
release {
def props = new Properties()
def fileInputStream = new FileInputStream(file('../signing.properties'))
props.load(fileInputStream)
fileInputStream.close()
storeFile = file(props['storeFilePath'])
storePassword = props['storePassword']
keyAlias = props['keyAlias']
keyPassword = props['keyPassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
// ...
}
}
}
我有几个问题,我把下面的行放在错误的地方:
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 {
...
}
}
很容易犯这个错误。