Android:添加日期/时间以gradle输出apk文件名

Pau*_*est 9 android gradle android-gradle-plugin

如何在Gradle Android输出文件名中添加日期/时间戳?

应该像project_v0.5.0_201503110212_public.apk

已经看过了

Fre*_*red 15

我假设你想要它以你指定的格式,所以这是一个可能的解决方案.

在gradle文件中,您可以定义一个新函数来获取您想要的日期时间字符串:

import java.text.DateFormat
import java.text.SimpleDateFormat

def getDateTime() {
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");

    return df.format(new Date());
}
Run Code Online (Sandbox Code Playgroud)

然后对于所有变体,您只需运行此:

android {
    //...
  buildTypes {
    //...
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def file = output.outputFile
            output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + getDateTime() + ".apk"))
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这并不像您发布的那样真正输出apk名称,但我想这足以帮助您.

  • 使用此方法生成具有适当时间戳的APK,但不知何故Android Studio查找错误的名称,例如输出文件名为"my-app-debug-20160309_1649.apk",但AS查找"my-app-debug-" 20160309_1648.apk" - 一分钟后关闭.知道发生了什么事吗? (3认同)

Dyn*_*ind 6

这段代码对我有用。

    applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def project = "Your App Name"
        def SEP = "_"
        def flavor = variant.productFlavors[0].name
        def buildType = variant.variantData.variantConfiguration.buildType.name
        def version = variant.versionName
        def date = new Date();
        def formattedDate = date.format('ddMMyy_HHmm')

        def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"

        output.outputFile = new File(output.outputFile.parent, newApkName)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我得到“无法为 com.android 类型的 ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=0.1}} 设置只读属性 'outputFile' 的值。 build.gradle.internal.api.ApkVariantOutputImpl。” 同步gradle时出错 (3认同)

归档时间:

查看次数:

5521 次

最近记录:

7 年,4 月 前