Changing the Android emulator locale automatically

2019-01-16 06:02发布

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.

2条回答
叼着烟拽天下
2楼-- · 2019-01-16 06:49

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-16 06:53

Accepted answer doesn't work anymore. persist.sys.language and persist.sys.country are gone from emulator properties.

My solution is to use preinstalled on android emulator "Custom locale" application. Simply send intent with extra language parameter to it as below:

adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN

More information here - prepare android emulator for UI test automation.

UPDATE: based on comment from Jonas Alves the following command works on API 28+:

adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE "en_US" com.android.customlocale2
查看更多
登录 后发表回答