gradle - Force locale for Android flavor with resConfig -


i trying use resconfig , resconfigs android build system.

  • android studio version 1.2.2
  • gradle build version 1.2.3
  • osx 10.10.3

i having problem these 2 options project started new blank project android studio. attached build.gradle added resconfigs "en", "fr" under

android {     defaultconfig {         ...         resconfigs "en", "fr"          ...    } } 

and defined 2 basic flavors

productflavors {         fr {             resconfig "fr"         }          en {             resconfig "en"         } } 

i created 2 strings.xml files , translated hello_world default label

/src/main/res/values/strings.xml (default)
/src/main/res/values-en/strings.xml
/src/main/res/values-fr/strings.xml

with this, expect see 3 values folders in myapplication/app/builds/intermediates/res/en/debug since defined in resconfigs use "en" , "fr" , filter else

/values/
/values-en/
/values-fr/

although, languages values folder still in there resconfigs not filtering apparently.

i expect see label hello_world values-en when running endebug flavor variant since specified flavor en use resconfig "en" although when run test app see label /values-fr/strings.xml instead of /values-en/strings.xml since language on tablet configured french canada

am misunderstanding purpose behind resconfig? if so, how supposed force flavor run in language , not dependant on os locale setting? solution must compatible flavordimensions since use them in real app i'm trying configure.

note: filed bug since think there problem resconfig behavior https://code.google.com/p/android/issues/detail?id=180694

thanks

well, after many hours, here's solution problem ending same problem.

turns out resconfig not though. proper , way found force locale specific flavor this:

first, make sure activities extends common activity responsible force locale. (i had create 2 since have activities extending fragmentactivity , extending activity. don't doing in activities constructor since have call applyoverrideconfiguration before call done on getresources, need call before activity's onstart).

public class localemainactivity extends activity {      public localemainactivity() {         activityutils.forcelocale(this);     } }  public class localemainfragmentactivity extends fragmentactivity {      public localemainfragmentactivity() {         activityutils.forcelocale(this);     } }  //method activityutils public static void forcelocale(contextthemewrapper contextthemewrapper) {     configuration configuration = new configuration();     locale defaultlocale = new locale(buildconfig.locale);     configuration.locale = defaultlocale;     contextthemewrapper.applyoverrideconfiguration(configuration);       } 

next, need define locale in build.gradle flavors

productflavors {      myfrenchflavor {      buildconfigfield "string", "locale", "\"fr\""     }     myenglishflavor {       buildconfigfield "string", "locale", "\"en\""     } } 

i tested solution on api 19+. works when change language while in app too.



alternate solution (won't work on android m+ see android bug report) solution wide spreaded on stackoverflow faced wall while implementing on android m keep in mind.

in oncreate() of androidapplication can call updateconfiguration locale set.

locale mylocale = new locale(buildconfig.locale); resources res = getresources(); configuration conf = res.getconfiguration(); conf.locale = mylocale; res.updateconfiguration(conf, res.getdisplaymetrics()); 

you need reset if defined onconfigurationchanged()

@override     public void onconfigurationchanged(configuration newconfig) {         super.onconfigurationchanged(newconfig);                     configuration config = new configuration(newconfig);         config.locale = new locale(buildconfig.locale);         getbasecontext().getresources().updateconfiguration(config, getbasecontext().getresources().getdisplaymetrics());     } 

edit: aware applyoverrideconfiguration has side effect explained here chromium webview not seems work android applyoverrideconfiguration. chromium team working on it!

thanks.


Comments

Popular posts from this blog

c# - Store DBContext Log in other EF table -

c# - SetBinding not registering for PropertyChanged event -

Nuget pack csproj using nuspec -