9

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)
            }
        }
    }
 }
serv-inc
  • 35,772
  • 9
  • 166
  • 188
StarWind0
  • 1,554
  • 2
  • 17
  • 46
  • 1
    I'm not sure if this will help, but [this](http://stackoverflow.com/questions/25831082/android-studio-is-installing-old-apk-on-device) looks helpful. [This](http://stackoverflow.com/questions/29006388/android-add-date-time-to-gradle-output-apk-filename) also looks helpful. – Razor Jul 15 '16 at 21:42
  • Interesting Razor. It does not solve the issue, but that before launch.. maybe there is a different entry I can add to the before launch. – StarWind0 Jul 17 '16 at 00:09
  • 1
    There probably is. I haven't come across this before so I'm not entirely sure on what your problem could be. – Razor Jul 17 '16 at 02:13
  • I think you're stuck with just refreshing your app each time you run it. I don't see much of a solution :( – Razor Jul 17 '16 at 02:18
  • What would be the desired outcome? – jwriteclub Jul 22 '16 at 10:14
  • What happens if you put in the minutes and seconds as well? – serv-inc Jul 22 '16 at 11:47

2 Answers2

3

Command-line options

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.

Code option: 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)
        }
    }
Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • This one is very close. --recompile-scripts get it to create the new APK. However it still tries to Launch the old version. At the least it creates the new version. The upToDateWhen -- Having issues getting that to compile in an android environment. Do you have a full code sample? It looks like Groovy is required, but Gradle would not accept it – StarWind0 Jul 22 '16 at 20:18
  • Giving you the bounty, though the solution is not complete. – StarWind0 Jul 22 '16 at 20:47
0

You can create such task, for example, for each flavour and build type (installDebug, intallRelease) if no flavors, and run it instead of default Run configuration. But then you should manually attach to debug, and, maybe, you'll have some other issues. Maybe there is some ability to generate these tasks automatically for each flavor/build type.

Script from here: https://stackoverflow.com/a/21992166/4069913

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'
}
Community
  • 1
  • 1
Orest Savchak
  • 4,529
  • 1
  • 18
  • 27