自动更改Android模拟器区域设置

时间:2023-01-28 22:49:33

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.

对于自动化测试(使用Hudson),我有一个脚本,可以为Android OS版本,屏幕分辨率,屏幕密度和语言的许多组合生成一组模拟器。这种方法很好,除了语言部分。

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:

我需要找到一种自动更改Android系统区域设置的方法。这是我能想到的一些方法,按优先顺序排列:

  • Extracting/editing/repacking a QEMU image directly before starting the emulator
  • 在启动仿真器之前直接提取/编辑/重新打包QEMU映像

  • Running some sort of system-locale-changing APK on the emulator after startup
  • 启动后在模拟器上运行某种系统区域设置更改APK

  • Changing the locale settings on the emulator filesystem after startup
  • 启动后更改模拟器文件系统上的区域设置

  • Changing the locale settings in some SQLite DB on the emulator after startup
  • 启动后在模拟器上更改某些SQLite DB中的区域设置

  • Running a key sequence (via the emulator's telnet interface) that would open the settings app and change the locale
  • 运行一个键序列(通过模拟器的telnet接口),打开设置应用程序并更改语言环境

  • 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!

解决方案:感谢dtmilano关于相关属性的信息,以及我的一些进一步调查,我提出了一个比上述所有想法更好更简单的解决方案!

I have updated his answer below with the details.

我已经在下面更新了他的答案和详细信息。

2 个解决方案

#1


53  

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更改语言环境:

$ 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.

此区域设置将在将来运行模拟器时保留,但您当然可以在启动时或运行时期间再次更改它。

#2


4  

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

接受的答案不再有效。 persist.sys.language和persist.sys.country已从模拟器属性中删除。

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

我的解决方案是使用预装在Android模拟器“自定义区域设置”应用程序。只需使用额外的语言参数发送意图,如下所示:

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.

更多信息 - 为UI测试自动化准备android模拟器。

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

更新:根据Jonas Alves的评论,以下命令适用于API 28+:

adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE "en_US" com.android.customlocale2

#1


53  

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更改语言环境:

$ 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.

此区域设置将在将来运行模拟器时保留,但您当然可以在启动时或运行时期间再次更改它。

#2


4  

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

接受的答案不再有效。 persist.sys.language和persist.sys.country已从模拟器属性中删除。

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

我的解决方案是使用预装在Android模拟器“自定义区域设置”应用程序。只需使用额外的语言参数发送意图,如下所示:

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.

更多信息 - 为UI测试自动化准备android模拟器。

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

更新:根据Jonas Alves的评论,以下命令适用于API 28+:

adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE "en_US" com.android.customlocale2