views:

1356

answers:

2

For automated testing (using Hudson) I have a script that generates a bunch of emulators for many combinations of Android OS version, screen resolution, screen density and language.
This works fine, except for the language part.

I need to find a way to change the Android system locale automatically. Here's some approaches I can think of, in order of preference:

  • Extracting/editing/repacking a QEMU image directly before starting the emulator
  • Running some sort of system-locale-changing APK on the emulator after startup
  • Changing the locale settings on the emulator filesystem after startup
  • Changing the locale settings in some SQLite DB on the emulator after startup
  • Running a key sequence (via the emulator's telnet interface) that would open the settings app and change the locale
  • Manually starting the emulator for each platform version, changing the locale by hand in the settings, saving it and archiving the images for later deployment

Any ideas whether this can be done, either via the above methods or otherwise?

Do you know where locale settings are persisted to/read from by the system?


Solution:
Thanks to dtmilano's info about the relevant properties, and some further investigation on my part, I came up with a solution even better and simpler than all the ideas above!

I have updated his answer below with the details.

+6  A: 

Personally I think the simplest way is to start the emulator, probably a clean instance unless you are running integration tests that depends on other applications and then change locale using adb:

$ adb shell '
setprop persist.sys.language en;
setprop persist.sys.country GB;
stop;
sleep 5;
start'

or whatever locale you want to set. To verify that your change was successful just use

$ adb shell 'getprop persist.sys.language'

You may also want to run emulators on know ports, check my answer in this thread.


Note that you can also set system properties directly when starting the emulator:

emulator -avd my_avd -prop persist.sys.language=en -prop persist.sys.country=GB

This way, you can create a plain old emulator of any type then start it up immediately using the locale of your choice, without first having to make any modifications to the emulator images.

This locale will persist for future runs of the emulator, though of course you can always change it again at startup or during runtime.

dtmilano
This is really interesting to me so I went to try it out, I have tried it through ADB shell on a G1 and a HTC Hero, I can use getprop no problem and get information back. But I want to use setprop but when I do nothing seems to actually get changed?
Donal Rafferty
We were talking about emulators, not sure if this could work on a real device.
dtmilano
Cool.. I didn't know about those properties or `stop`/`start`. Works for me. Also worked on my HTC Hero, or at least `stop` and `start` happily rebooted things (when run as root). :)
Christopher
I also tried extracting an emulator system image, adding those '`persist`' properties to `/build.prop`, rebuilding, and then adding the image to the AVD. That worked, but seemingly with some inconsistent side effects of not displaying things at the right screen density. Rebooting helped, sometimes. I'll have to experiment tomorrow to see if I can get it working better this way, as I'd rather not have the first run of the emulator be a special case where I need to set these properties and reboot.
Christopher
I'm also already using the `emulator -ports` stuff you mentioned, but on a port range outside the normal Android range, so I use `adb connect` to let the adb server know where the emulators are running.
Christopher
Probably setting the properties you want to persist in '/data/local.prop' is enough for your needs saving you from building images.
dtmilano
Ah, could be. Anyway, preparing an image is easily automated (essentially a combo of unzip, cat, zip) so it's what I'd prefer over having to start the emulator first and then make changes via adb and reboot etc. I'll have to see whether the `/data` partition can be altered ahead of time. There is a `userdata.img` image for the emulator containing a couple of APKs, but adding `local.prop` didn't seem possible. I'll need have a closer look at the images and QEMU options later.
Christopher
Ah so a real device has to be rooted to get it to work? Can you try on a non rooted device just to make sure Christopher?
Donal Rafferty
I just tried it on my HTC Hero without becoming root and it does not work. Neither `setprop` nor `stop` will work unless you're root.
Christopher
Thanks for that Christopher, much appreciated
Donal Rafferty
Just one more thing Christopher can you point me to how you rooted your HTC Hero?
Donal Rafferty
This is the technique I used, I believe: http://theunlockr.com/2009/08/08/how-to-gain-root-access-on-your-htc-hero/
Christopher
dtmilano: Thanks a lot for the info about the properties. I came up with an even awesomer solution and added it to your post so I could accept your answer.
Christopher
Thank you for your comment. Setting properties on the command line seems much more flexible, mainly when you need a known environment to run the tests.
dtmilano
A: 

Hi Christopher,

Would you mind giving your script? It would be really useful in developping... Thanks

Sephy