Android gradle构建配置-重命名生成的APK
每当我构建一个APK时,我需要用一个特殊的名称保存每个APK。Android gradle构建配置-重命名生成的APK,android,gradle,Android,Gradle,每当我构建一个APK时,我需要用一个特殊的名称保存每个APK。 这是所需的名称格式:“buildType appName\u version\u Date\u Time.apk” 示例:“调试myAppName\u v1.0\u 20161009\u 0854.apk” 示例:“发布myAppName\u v1.0\u 20161009\u 0854.apk” 为此,我添加了这个gradle脚本: buildTypes { release { minifyEnabled
这是所需的名称格式:“buildType appName\u version\u Date\u Time.apk”
示例:“调试myAppName\u v1.0\u 20161009\u 0854.apk”
示例:“发布myAppName\u v1.0\u 20161009\u 0854.apk” 为此,我添加了这个gradle脚本:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def formattedDate = new Date().format('yyyyMMdd_HHmm')
println(output.outputFile)
def newName = variant.name + " myAppName_v" + defaultConfig.versionName + "_" + formattedDate + ".apk"
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
这个脚本可以工作,但是android studio找不到重命名的APK。我也尝试过清理/重建项目,但它再次出现
Android stuio错误:APK文件路径\to\project\app\build\outputs\APK\debug myAppName\u v1.0\u20161009\uu0854。磁盘上不存在APK。
安装APK时出错 当我检查“build\outputs\apk”目录时,我看到一个apk已经生成,它的名称是“myAppName\u v1.0”[20161009\ustrong>0856].apk”
<>这里有什么不对? ,而不是在APK文件名中包含当前时间(以分钟为单位,不少于),考虑以下方法:
- 获取最新版本控制提交的时间戳(保持稳定)
- 不要更改apk文件名,而是将其从无时间戳的名称(Android Studio很容易找到)复制到有时间戳的apk。
applicationVariants.all { variant ->
variant.outputs.each { output ->
def copyApkTask = tasks.create(name: "copy" + variant.name + "Apk") {
def newName = ... // can include the timestamp with minutes
println(newName)
// Using java.nio.Files.copy
def targetPath = new File(output.outputFile.parent, newName).toPath()
Files.copy(output.outputFile.toPath(), targetPath)
// Using gradle's Copy logic (clunky for single-file copy and rename)
copy {
from output.outputFile
into output.outputFile.parent
rename { String fileName ->
newName
}
}
}
copyApkTask.mustRunAfter variant.assemble
}
}
而不是在APK文件名中包含当前时间(以分钟为单位,不少于),考虑以下方法:
- 获取最新版本控制提交的时间戳(保持稳定)
- 不要更改apk文件名,而是将其从无时间戳的名称(Android Studio很容易找到)复制到有时间戳的apk。
applicationVariants.all { variant ->
variant.outputs.each { output ->
def copyApkTask = tasks.create(name: "copy" + variant.name + "Apk") {
def newName = ... // can include the timestamp with minutes
println(newName)
// Using java.nio.Files.copy
def targetPath = new File(output.outputFile.parent, newName).toPath()
Files.copy(output.outputFile.toPath(), targetPath)
// Using gradle's Copy logic (clunky for single-file copy and rename)
copy {
from output.outputFile
into output.outputFile.parent
rename { String fileName ->
newName
}
}
}
copyApkTask.mustRunAfter variant.assemble
}
}
你是在告诉gradle在你的apk名称中包含这一分钟 然后它在
n
分钟内用您想要的名称构建它,当它想在n+1
分钟内引用它时,它找不到该文件。所以它遇到了这个错误
最好在apk名称中使用提交哈希或日期。(不包括小单位,如分钟、秒甚至小时)您正在告诉gradle将分钟包含在您的apk名称中 然后它在
n
分钟内用您想要的名称构建它,当它想在n+1
分钟内引用它时,它找不到该文件。所以它遇到了这个错误
最好在apk名称中使用提交哈希或日期。(不包括分钟、秒甚至小时等小单位)您在apk文件名中包括当前分钟。它在一分钟内生成,但android studio试图在另一分钟内访问它?是的。这就是问题所在。您正在apk文件名中包含当前分钟数。它在一分钟内生成,但android studio试图在另一分钟内访问它?是的。这就是问题所在。