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

发布于 2024-08-24 04:58:48 字数 614 浏览 4 评论 0原文

对于自动化测试(使用 Hudson),我有一个脚本,可以为 Android 操作系统版本、屏幕分辨率、屏幕密度和语言的多种组合生成一堆模拟器。
除了语言部分之外,这工作得很好。

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

  • 在启动模拟器之前直接提取/编辑/重新打包 QEMU 映像
  • 启动后在模拟器上运行某种系统区域设置更改 APK 在启动后
  • 更改模拟器文件系统上的区域设置 运行按键序列(通过模拟器的
  • 启动后更改模拟器上某些 SQLite DB 中的区域设置
  • telnet 接口),这将打开设置应用程序并更改区域设置
  • 手动为每个平台版本启动模拟器,在设置中手动更改区域设置,保存并存档图像以供以后部署

有什么想法是否可以通过上述方法或其他方法来完成?

您知道系统在哪里保存/读取区域设置吗?


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

我在下面更新了他的答案并提供了详细信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗地喜欢 2024-08-31 04:58:48

我个人认为最简单的方法是启动模拟器,可能是一个干净的实例,除非您正在运行依赖于其他应用程序的集成测试,然后使用 adb:

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

或您想要设置的任何区域设置更改区域设置。
要验证您的更改是否成功,只需使用

$ adb shell 'getprop persist.sys.language'

您可能还想在已知端口上运行模拟器,请检查 我在此线程中的回答


请注意,您还可以在启动模拟器时直接设置系统属性:

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

这样,您可以创建任何类型的普通旧模拟器,然后使用您选择的区域设置立即启动它,无需 首先必须对模拟器图像进行任何修改。

该区域设置将持续存在于模拟器的未来运行中,当然您始终可以在启动时或运行时再次更改它。

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.

你的他你的她 2024-08-31 04:58:48

接受的答案不再有效。 persist.sys.languagepersist.sys.country 已从模拟器属性中消失。

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

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

更多信息请参见 - 为 UI 测试自动化准备 Android 模拟器

更新:根据 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

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
花海 2024-08-31 04:58:48

似乎 Android 模拟器现在支持在启动时设置区域设置:

emulator -avd <avd-name> -change-locale fr-CA

来源 - https: //androidstudio.googleblog.com/2019/09/emulator-29112-stable.html

Seems that Android emulator now supports setting the locale when starting it:

emulator -avd <avd-name> -change-locale fr-CA

Source - https://androidstudio.googleblog.com/2019/09/emulator-29112-stable.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文