2

I want to set my appName per flavor in Android Studio. I use com.android.tools.build:gradle-experimental:0.4.0 plugin so I can`t use standard gradle/groovy syntax.

I have found a solution here: how to change android app name in the build gradle file

However with all my experimentations I just can`t figure out the right syntax for resValue field.

I have tried something like this:

buildConfigFields.resValue = {
            create() {
                type = "String"
                name = "appName"
                value = "Game_Name"
            }
        }

But no luck.

Community
  • 1
  • 1
Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31

3 Answers3

1

I think the best way is to override the string in the strings file in the flavor folder.

So create the following files:

src/flavor1/res/values/strings.xml
src/flavor2/res/values/strings.xml

Then add in these files the value of the app name

 <string name="app_name">XXXX</string>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

I've got it working with "gradle-experimental:0.8.0-rc2'"

model {
    android {
        defaultConfig {
        ...
            resValues.with {
                create() {
                    type = "string"
                    name = "myVar"
                    value = "myValue"
                }
            }
        }
    }
}

After that I was able to access this variable in my code using:

R.string.myVar

Hope this helps.

Viktar Patotski
  • 584
  • 6
  • 20
  • This works perfectly in gradle-experimental. How did you figure this out? Thanks. – IgorGanapolsky Sep 19 '16 at 17:30
  • I already had solution for "buildConfigFields.with" in my build.gradle found I believe here: http://stackoverflow.com/questions/31683438/gradle-experimental0-1-0-buildconfigfield . So I just tried to do similar and it worked. – Viktar Patotski Sep 20 '16 at 17:33
0

You can try this in your app/build.gradle

android {
 .....
 .....
 productFlavors {
  flavor1 {
   resValue "string", "app_name", "App-Name Flavor1"
  }
  flavor2 {
   resValue "string", "app_name", "App-Name Flavor2"
  }
 }
}

Note: Remove string app_name from strings.xml

Edit :

This above approach doesn't work with given 0.4.0 plugin.

As suggested by @Gabriele Mariotti, you can try overriding the value of app_name string in that flavour. Here's how you do it.

Add app_name string in respective strings.xml file

src
-- main/res/values/strings.xml (default value)
-- flavour1/res/values/strings.xml
-- flavour2/res/values/strings.xml
Amrendra Kumar
  • 326
  • 2
  • 5
  • It doesn`t work with experimental 0.4.0 plugin. I have "Error:No signature of method: org.gradle.model.ModelMap.resValue() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [String, app_name, App-Name Flavor1]" error on gradle sync. I have tried String with capital S and small S as you wrote. – Adrian Ivasku Feb 07 '16 at 19:26
  • I have tried resValue{ create() { type = "String" name = "appName" value = "Game_Name" } } And I have Error:Attempt to mutate closed view of model of type 'java.lang.Object' given to rule 'model.android.productFlavors' error – Adrian Ivasku Feb 07 '16 at 19:32
  • I understand now. I have already done this and it works. Since everything in the flavor folder takes precedence over the main folder. However I am looking for a way to set this String value from a txt file. So I am looking for a way in gradle to override this since I know how to parse a txt file and use the values of that. – Adrian Ivasku Feb 07 '16 at 19:40
  • I get an error: ` Could not find method resValue() for arguments [String, ENVIRONMENT, debug]` – IgorGanapolsky Jul 27 '16 at 14:05