1

I would like to be able to quickly visually verify that a new build has been installed on my emulator. One way I'm considering, is to have the @string/app_name value be updated upon each new build with a unique hash, or maybe just a random four char value. Then when I see the application icon on the phone, it will have a different value than the last build and I'll know it was updated.

Is there a way to programmatically update @string values at build time? Maybe some Gradle functionality I'm not aware of?

Aubin
  • 14,617
  • 9
  • 61
  • 84
ether_joe
  • 1,068
  • 1
  • 13
  • 32

2 Answers2

2

Use resValue to add a resource value from the gradle file. See Is it possible to declare a variable in Gradle usable in Java? for example

badoualy
  • 386
  • 2
  • 10
1

I did something like you need:

This code replaces value in <string name="logging"></string>

\script\set_strings_xml_app_version.sh:

sed -E -i "" "s/<string name=\"logging\" translatable=\"false\">[a-zA-Z0-9]+<\/string>/<string name=\"logging\" translatable=\"false\">$1<\/string>/g" "$2"

\app\build.gradle :

android {
//...
}

dependencies {
//...
}

task setStringsXMLAppVersion << {
        def cmd = projectDir.absolutePath + "/../scripts/set_strings_xml_app_version.sh 0x" + Integer.toHexString(android.defaultConfig.versionCode).toUpperCase() + " " + projectDir.absolutePath + "/../app/src/main/res/values/strings.xml"
        def sout = new StringBuffer()
        def serr = new StringBuffer()
        def proc = cmd.execute()
        proc.consumeProcessOutput(sout, serr)
        proc.waitForOrKill(1000)
        if (serr) {
            println "Error while replacing versionCode in strings.xml: $serr"
        }
    }

    preBuild.dependsOn setStringsXMLAppVersion
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52