Below I have my build file for Gradle. Issue. It runs yesterday's APK instead of today's. Root cause. I dynamically put the date in the apks name -- for debug builds.
When I run the app it sees the old APK and sees it matches what Gradle is expecting, as Gradle has not refreshed and noticed the date change.
I need to force gradle to refresh every run.
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)
}
}
}
}
Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.
Gradle uses normalized inputs when doing up-to-date checks and when determining if a cached result can be re-used instead of executing the task.
Even though some other options might work, have you tried the
--recompile-scripts
Forces scripts to be recompiled, bypassing caching.
command-line option? Another alternative would be --rerun-tasks
, but that might be overkill.
upToDateWhen
Have a look at Resetting the UP-TO-DATE property of gradle tasks?. Setting upToDateWhen {false}
might do the trick. Try the following instead:
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With