Android Gradle构建文件,带有时间戳,运行于昨天版本

Android Gradle构建文件,带有时间戳,运行于昨天版本,android,android-studio,android-gradle-plugin,build.gradle,Android,Android Studio,Android Gradle Plugin,Build.gradle,下面是我的Gradle构建文件。 问题它运行昨天的APK而不是今天的。 根本原因。我动态地将日期放在apks名称中——用于调试构建 当我运行应用程序时,它会看到旧的APK,并看到它符合Gradle的期望,因为Gradle没有刷新并注意到日期更改 我需要强迫gradle刷新每次跑步记录 buildTypes { debug { debuggable true minifyEnabled false proguardFiles 'proguard

下面是我的Gradle构建文件。 问题它运行昨天的APK而不是今天的。 根本原因。我动态地将日期放在apks名称中——用于调试构建

当我运行应用程序时,它会看到旧的APK,并看到它符合Gradle的期望,因为Gradle没有刷新并注意到日期更改

我需要强迫gradle刷新每次跑步记录

buildTypes {
   debug {
        debuggable true
        minifyEnabled false
        proguardFiles 'proguard-rules.pro'
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def formattedDate = new Date().format('yyyyMMdd')
                def newName = output.outputFile.name
                newName = newName.replace("app-", "myappname-") //"MyAppName" -> I set my app variables in the root project
                newName = newName.replace("-release", "-" + versionName + "-" + formattedDate + "-r")
                newName = newName.replace("-debug", "-" + versionName + "-" + formattedDate + "-d")
                output.outputFile = new File(output.outputFile.parent, newName)
            }
        }
    }
 }
命令行选项 即使其他一些选择可能有效,您是否尝试过

强制重新编译脚本,绕过缓存

??另一种选择是,但这可能是过分的

代码选项:UpdateWhen 看一看。设置
Updatewhen{false}
可能会起作用。请尝试以下操作:

    applicationVariants.all { variant ->
        variant.outputs.upToDateWhen {false}
        variant.setOnlyIf { true }
        variant.outputs.each { output ->
            def formattedDate = new Date().format('yyyyMMdd')
            def newName = output.outputFile.name
            newName = newName.replace("app-", "myappname-") //"MyAppName" -> I set my app variables in the root project
            newName = newName.replace("-release", "-" + versionName + "-" + formattedDate + "-r")
            newName = newName.replace("-debug", "-" + versionName + "-" + formattedDate + "-d")
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }

例如,如果没有flavors,您可以为每个Flavor和构建类型(installDebug、intallRelease)创建这样的任务,并运行它而不是默认的运行配置。 但是,然后您应该手动附加到debug,并且,可能还有一些其他问题。 也许有一些能力为每种风格/构建类型自动生成这些任务

此处的脚本:


我不确定这是否有用,但看起来很有用。看起来也很有用。很有趣的剃须刀。这并不能解决问题,但在发射前。。也许在发布前我可以添加一个不同的条目。可能有。我以前没有遇到过这个问题,所以我不能完全确定你的问题可能是什么。我想你只能在每次运行应用程序时刷新它。我看不出什么解决方案:(期望的结果是什么?这一个非常接近。--重新编译脚本让它创建新的APK。但是它仍然尝试启动旧版本。至少它创建了新版本。最新版本--在android环境下编译时遇到问题。你有完整的代码示例吗?Groovy看起来是必需的ed,但是Gradle不会接受给你赏金,尽管解决方案还不完整。
    applicationVariants.all { variant ->
        variant.outputs.upToDateWhen {false}
        variant.setOnlyIf { true }
        variant.outputs.each { output ->
            def formattedDate = new Date().format('yyyyMMdd')
            def newName = output.outputFile.name
            newName = newName.replace("app-", "myappname-") //"MyAppName" -> I set my app variables in the root project
            newName = newName.replace("-release", "-" + versionName + "-" + formattedDate + "-r")
            newName = newName.replace("-debug", "-" + versionName + "-" + formattedDate + "-d")
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }
task appStart(type: Exec, dependsOn: 'install$Flavor$Build') {
    // linux
    commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MainActivity'

    // windows
    // commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MainActivity'
}