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.
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.
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
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
Related
Maybe I'm just missing it but is there a way to view WHAT is running in the background on-device in android studio?
I'm getting a battery usage alert on my phone (galaxy s8 - OS v8.0), indicating my app is doing something in the background and I'd like to see what it is.
Thank you
Yea, via the terminal function. This is one of options at the bottom of the Android studio.
You need to run ADB to connect to the device. Once connected you can use Unix command lines to see the processes running.
From google dev : https://developer.android.com/studio/command-line/adb
Also, the command I use to connect an emulator to run unix commands is:
adb -s <DEVICE> shell
Normally the ADB is stored :
USER\AppData\Local\Android\Sdk\platform-tools
and then once connected you can use the following to see the running processes :
https://www.howtogeek.com/107217/how-to-manage-processes-from-the-linux-terminal-10-commands-you-need-to-know/
Hope this helps.
I'm working on fastlane and want to start 3 emulators [phone, tab7, tab10] and take screenshots on them.
How do I create a script to create and start emulators via fastlane or commandline
You will need a script which does the following:
Start the emulator
Wait for the emulator to boot
Start screengrab for this emulator
Shut down the emulator
Then you call the script for your three different screen sizes.
Some more details:
To start the emulator use the program emulator which is located under android-sdk/emulator/emulator. I had problems with android-sdk/tools/emulator so be sure to use the right one. The call can look like this:
emulator #'your_emulator_name_variable' &
The & is important when you are using a shell script to continue after the call.
You can call adb shell getprop sys.boot_completed to see if the emulator has booted already. Do this in a loop until it returns 1
Call fastlane screengrab and append the specific type flag:
--specific-type 'device_type_variable'
with 'device_type_variable' equals to phone, sevenInch or tenInch. According to the emulator you started.
Call adb emulator-5554 emu kill to shut down you emulator. If it uses another, non standard port, adjust accordingly.
Now you can call this script with the two variables your_emulator_name_variable and device_type_variable for each of your emulator.
Hope this helps.
I'm running tests for my app, and I want to validate that it works in various time zones.
I'm trying solutions suggested here running adb shell setprop persist.sys.timezone "Pacific/Honolulu", but the timezone (and time) on emulator is not changing- can this work without restarting the device?
If you mean UI tests, I can not change android Date & Time settings on emulator via adb shell.
The only solution I found is to set the timezone during the creation of the emulator with -timezone option.
For example, you can create android emulator with a command like $ emulator #Nexus_5X_API_23 -timezone Europe/Paris.
See more emulator command line options here
I setup a couple of robotium tests and stuff for our CI.
Everthing works fine so far. I just cant figure out how i can define the Default Language for the AVD i creating.
I read Managing AVD but there is no option listed for this.
How you do this when your Robotium Tests also make use of the "searchText" feature, but fail because the emulator runs on different language ?
Using the android avd command also don't show any option for that. I had a quick look in the config files for for AVDs created and there also is no language setting.
You can try to set Android emulator default language on starting command using -change-locale:
emulator -avd <avd-name> -change-locale fr-CA
Relase note - https://androidstudio.googleblog.com/2019/09/emulator-29112-stable.html
Here is one way to switch the language after the emulator was started:
adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN
source
I'm planning to build an automated system for deploying an Android build to various devices, in order to make development for multiple platforms a bit more comfortable. Is there a way to get the sdk version of a connected device through android shell or adb?
There will be a computer to which several test devices is connected, and I was planning to write a script which will fetch the correct build for each of those from a build-server, install the different apks on their respective devices, launch them and collect log info, to be made available through some other program whose specifications are beside the point.
The point is that I need to know the sdk version each device is running to install the correct apk, and I was hoping I could get this through adb, but I can't seem to find a way to access it short of building a tiny app, compatible with all versions, whose sole purpose would be to output android.os.Build.VERSION.SDK or similar somewhere my script could read it.
you can use this command:
adb shell grep ro.build.version.sdk= system/build.prop
It will output something like this:
ro.build.version.sdk=10
adb shell getprop ro.build.version.sdk
Note #Tim: this works even on phones without grep support on all host OS :-). (i.e. on old phones where toolbox does not support grep you you need to have busybox on your phone).
I also discovered a way to get the exact version of Android e.g. 4.2.2 based on the following web article http://xayon.net/looking-for-android-version-with-adb/ You need to be using a unix-like operating system - Linux and Mac OSX are fine, and windows users can use cygwin or equivalent.
At a command line:
echo version=$(adb shell getprop |awk -F":" '/build.version.release/ { print $2 }')|tr -d '[]'
Here is the result for my Nexus 4:
version= 4.2.2
I think you can by accessing the device with adb shell - change directories to position you at system and do a cat of build.prop. Here you will find for instance, ro.build.description=google_sdk-eng 2.2, ro.build.version.release=2.2 etc