1

In strings.xml

<resources>
<string>name="app_name">App_Name</string>
<string>name="app_port">https://example.com</string>
<string>name="app_key">App_Key</string>
</resources>

In build.gradle

productFlavors {
    production {
        applicationId = "com.example.production"
    }
    staging {
        applicationId = "com.example.staging"
        //code to manipulate strings.xml value because for production and staging app_name, app_port and key are different
    }
}

I need a code code to manipulate strings.xml from productFlavours of build.gradle file instead of creating separate folder for staging and production.

Archana
  • 597
  • 4
  • 18
  • Do they have different values or are keys themselves different? – mixel Oct 05 '15 at 11:32
  • I think following link might help you [enter link description here][1] [1]: http://stackoverflow.com/a/17201265/1182022 – Adnan Amjad Oct 05 '15 at 11:38
  • @mixel What ever i specified with in strings.xml file are different for staging. – Archana Oct 05 '15 at 11:42
  • @Archana Creating separate folder for product flavor is right solution. I do not understand why it's not suitable for you. Could you provide question with more details of your specific case? – mixel Oct 05 '15 at 11:46
  • @mixel I am trying to change strings.xml values without creating separate folders for staging and production. – Archana Oct 05 '15 at 11:56
  • @AdnanAmjad On running staging it should select this port and app name. – Archana Oct 05 '15 at 11:58

1 Answers1

6

I found an answer. Using resValue as follows with in gradle

productFlavors {
production {
    applicationId = "com.example.production"
    resValue 'string', 'APP_NAME', 'app_name'
}
staging {
    applicationId = "com.example.staging"
    resValue 'string', 'APP_NAME', 'app_name_stage'
}

It will create generated.xml file with in build directory. To get the values in code, use generated resource like all others as follows,

getResources().getString(R.string.APP_NAME)
Archana
  • 597
  • 4
  • 18