2

I have the following in my android app

#MyAndroidApp/assets/app.properties

server=localhost
....
....

when building the app, I would like to change the value of 'server' from localhost to whatever is defined in gradle.properties.

The 'java' plugin allows me to do this

processResources {
   def serverHostName = project.hasProperty("serverhost") ? project.property("serverhost") : "localhost"
   filter ReplaceTokens, tokens: [
    "serverhost": serverHostName
   ]
}

Similarly, I thought I could override processDebugResources and processReleaseResources in Android but unfortunately its not possible. Is there any other alternative way?

I checked out 'productFlavors' but it requires to define whole new project structure, all I want is to change a single property here.

Krishnaraj
  • 2,360
  • 1
  • 32
  • 55
  • Why dont' you store those properties as Android resources? In that case you could use `resValue` to provide different values for build type: http://stackoverflow.com/a/17201265/321354 – rciovati Aug 03 '14 at 11:02
  • It seems like a possible solution but unfortunately breaks in eclipse. The generated files go into the build folder while eclipse looks for in bin. I suppose I can make eclipse output dir to 'build' but read somewhere that its a bad idea. – Krishnaraj Aug 03 '14 at 16:18

1 Answers1

3

Since I'm new to the gradle build system probably it is not the best solution, but it worked for me:

defaultConfig {
    ...
    applicationVariants.all { variant ->
         def serverHostName = project.hasProperty("serverhost") ? project.property("serverhost") : "localhost"
         variant.mergeAssets.doLast {
            copy {
                from("${projectDir}/src/main/assets") {
                    include "*.properties"
                }
                into("${buildDir}/assets/${variant.dirName}")

                filter(ReplaceTokens, tokens: [
                        "serverhost": serverHostName)
                ])
            }
        }
    }
}
txuslee
  • 416
  • 3
  • 5
  • Neat!. Just in case, the following might be useful for others: https://github.com/shakalaca/learning_gradle_android/blob/082fa1575de1fa75ef09c90cb0cfdd60407e184d/07_tricks/app/build.gradle – Krishnaraj Aug 24 '14 at 16:46