Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle build file with Time Stamp running yesterdays version

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)
            }
        }
    }
 }
like image 568
StarWind0 Avatar asked Mar 09 '16 07:03

StarWind0


People also ask

How do I force Gradle to sync?

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.

How does Gradle determine up to date?

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.


1 Answers

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)
        }
    }
like image 157
serv-inc Avatar answered Oct 14 '22 18:10

serv-inc