2

In my app, I have a lot of different product flavors, around 10 actually. The way they are done is something like : eng_testDebug, eng_prodDebug, eng_testRelease, eng_prodRelease e.t.c for other languages.

So now I'v got a task to add "- Test" to the app name for build with test in the build name.

Right now I found a solution to parse string.xml from main sourceSet and build my app name using manifestPlaceHolders in a gradle file. But it works only for main flavor. Here is a code for getting default strings.xml app_name I am using:

def getAppName() {
    def stringsFile = android.sourceSets.main.res.sourceFiles.find { it.name.equals 'strings.xml' }
    String s = new XmlParser().parse(stringsFile).string.find { it.@name.equals 'app_name' }.text();
    return s.replaceAll("\"", "");
}

And here I modify it to add "Test":

def getEditedAppName() {
    if (getCurrentFlavor().contains("test")) {
        return getAppName() + "(Test)";
    } else {
        return getAppName();
    }
}

getCurrentFlavor() returns me a flavor name used in :assemble task.

Basically, the question is how can I get other sourceSet files depending on my current building flavor so I can parse flavor's app_name?

Unfortunately defying an app_name string for each flavor in a gradle file is not an valid option for me.

Yury Dombaev
  • 61
  • 1
  • 7

2 Answers2

0

For each flavor you can create a folder named like the flavor in you src folder. There you should also see your main folder. In those flavor folders you can create the res folder and override or add additional resource files. Here you can create the strings.xml and change the app name as well as drawables and so on.

http://ptrprograms.blogspot.de/2014/08/build-variations-using-gradle-and.html

Edit based on comment:

I got these two flavors:

productFlavors {
    appFlavorA{
        applicationId "de.test.appaA"
    }
    appFlavorB{
        applicationId "de.test.appaB"
    }
}

Now I got the following structure for strings.xml

ProjctRoot/app/src/main/res/values/strings.xml
ProjctRoot/app/src/appFlavorA/res/values/strings.xml
ProjctRoot/app/src/appFlavorB/res/values/strings.xml

This will result in the usage of the corresponding strings.xml for the flavorflavor

glethien
  • 2,440
  • 2
  • 26
  • 36
  • Thanks for the answer, but I already have folders for each of my flavors and app_name overidden in each of them. But now I cant find a way to parse these overidden files from gradle to get each flavor's app_name. – Yury Dombaev Jan 19 '17 at 13:02
  • Normally this is done automatically if you configured gradle corectly – glethien Jan 19 '17 at 13:03
  • Updated my answer – glethien Jan 19 '17 at 13:07
  • I edited my question again too. Right now I know the path to each of my flavor's strings.xml, but is it possible to get the right strings.xml from gradle code like in my getAppName(); method? – Yury Dombaev Jan 19 '17 at 13:10
  • Why do you want to do that? From my point of view this is a bad practice to do so. You have the flavors and the strings to deal with localization orientation and so on. Gradle does not know about this. FOr adding a suffix you can use something like this: versionNameSuffix '-TEST' in your flavor. Reading and manipuilating a string during compile time feels bad for me – glethien Jan 19 '17 at 13:13
  • 1
    Unfortunately 'virsionNameSuffix' edits the output file name, but in my case the app name has to be edited. And yes, I know its a bad practice but I need to resolve this task without editing app_name for each flavor string.xml – Yury Dombaev Jan 19 '17 at 13:19
0

If I understand the question correctly, you want to have different app names based on your product flavors.

In your gradle file:

android {
     productFlavors {
          stagingone{
          }
          stagingtwo{
          }
     }
}

Assuming that you have this sample entry, just place a strings.xml file that contains different app_name for each of the flavors.

src/stagingone/res/values/strings.xml src/stagingtwo/res/values/strings.xml

I don't think you can manually do that (change app names at runtime), as highlighted in this Stackoverflow thread.

Android : Change App Label Programatically

But, there is another way. Do it the same, but for app launcher icons for the flavors. Good luck.

Community
  • 1
  • 1
Kevin Tan
  • 1,038
  • 8
  • 19