Programmatically Switch Android Input Methods with UiAutomator and ADB
This guide shows how to automatically change the Android input method to UTF‑7 or any other IME during UiAutomator tests by executing ADB commands, listing available IMEs, and handling command output on both macOS and Windows environments.
Purpose
When UiAutomator tests need the UTF‑7 input method, the device’s default input method must be switched before the test and restored afterwards. The following Java utilities automate this process via ADB, suitable for multiple test devices.
Switch to UTF‑7 IME
public void setMobileInputMethodToUtf() {
execCmdAdb("adb shell settings put secure default_input_method jp.jun_nama.test.utf7ime/.Utf7ImeService");
}Sets the secure setting default_input_method to the component name of the UTF‑7 IME ( jp.jun_nama.test.utf7ime/.Utf7ImeService).
Switch to an alternative IME
public void setMobileInputMethodToOthers() {
List<String> imeList = execCmdAndReturnResult("adb shell ime list -s");
for (String ime : imeList) {
if (!ime.contains("utf7ime")) {
execCmdAdb("adb shell settings put secure default_input_method " + ime);
break; // use the first non‑UTF‑7 IME
}
}
}Retrieves the list of enabled input methods with adb shell ime list -s, skips the UTF‑7 entry, and sets the first remaining IME as the default.
Helper: Execute an ADB command without return value
public void execCmdAdb(String cmd) {
System.out.println("Executing: " + cmd);
String os = System.getProperty("os.name");
try {
Process p = os.contains("Mac")
? Runtime.getRuntime().exec(ADB_PATH + cmd)
: Runtime.getRuntime().exec("cmd /c " + cmd);
// Standard output
try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = r.readLine()) != null) {
System.out.println(line);
saveToFile(line, "runlog.log");
}
}
// Error output
try (BufferedReader er = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
String line;
while ((line = er.readLine()) != null) {
System.err.println(line);
saveToFile(line, "runlog.log");
}
}
} catch (IOException e) {
System.err.println("Command failed: " + cmd);
e.printStackTrace();
}
}The method runs the supplied command, prints both stdout and stderr, and appends each line to runlog.log. It distinguishes macOS (uses ADB_PATH) from Windows (prefixes cmd /c).
Helper: Execute an ADB command and capture output
public List<String> execCmdAndReturnResult(String cmd) {
System.out.println("Executing: " + cmd);
List<String> result = new ArrayList<>();
String os = System.getProperty("os.name");
try {
Process p = os.contains("Mac")
? Runtime.getRuntime().exec(ADB_PATH + cmd)
: Runtime.getRuntime().exec("cmd /c " + cmd);
try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = r.readLine()) != null) {
result.add(line);
}
}
} catch (IOException e) {
System.err.println("Command failed: " + cmd);
e.printStackTrace();
}
return result;
}This variant returns the command’s output lines as a List<String>, which is used by the IME‑switching logic.
Integration note
Include these utilities in a UiAutomator test suite and invoke setMobileInputMethodToUtf() before a test that requires the UTF‑7 keyboard, then call setMobileInputMethodToOthers() after the test to restore a regular input method. The approach works across multiple devices without manual interaction. Ensure that ADB_PATH points to the adb executable (e.g., /path/to/adb) and that saveToFile(String text, String fileName) is implemented according to your logging policy.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
