A
A
aosvxs7ui2018-07-04 23:02:33
Android
aosvxs7ui, 2018-07-04 23:02:33

Is it possible to control an android application via adb?

Good day. There is a need to automate the work with the application on the android device. Accordingly, the questions arose:
1. Is it possible to control the application through adb by sending it any commands?
2.If yes - how to find out the command data?
Any taskers and other applications for android do not fit the task.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Varakosov, 2018-07-05
@thelongrunsmoke

Easy.
adb shell monkey -p <имя пакета> 1- application launch.
adb shell input tap <x> <y>- tap in the specified coordinates.
A screenshot can be obtained as follows.

adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
adb shell rm /sdcard/screen.png

In conjunction with python and OpenCV, this is enough for arbitrary automation.

E
Evgeny Kumanin, 2018-07-05
@jackkum

UI Automator used such a thing to automate some actions in other people's applications (Requires a tethered device).

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class AutomationTester {

  private UiDevice mDevice;

  @Before
  public void before() {
    mDevice = UiDevice.getInstance(getInstrumentation());
    assertThat(mDevice, notNullValue());
  }

  @org.junit.Test
  public void test() throws Exception
  {
    mDevice.pressHome();
    Thread.sleep(1000);
    openApp("com.viber.voip");

    UiObject2 menu = waitForObject(By.res("com.viber.voip:id/fab_new_contact"));
    menu.click();

    UiObject2 phoneField = waitForObject(By.res("com.viber.voip:id/phone_number"));
    phoneField.setText(phone);

    UiObject2 nextBtn = mDevice.findObject(By.res("com.viber.voip:id/continue_btn"));
    nextBtn.click();

    // etc...
  }

  private void openApp(String packageName) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);
  }

  private UiObject2 waitForObject(BySelector selector, int timeout) throws Exception {
    UiObject2 object = null;
    int delay = 1000;
    long time = System.currentTimeMillis();
    while (object == null) {
      object = mDevice.findObject(selector);
      Thread.sleep(delay);
        if (System.currentTimeMillis() - timeout > time) {
            throw new InterruptedException("Faild waiting object");
        }
    }
    return object;
  }

  private UiObject2 waitForObject(BySelector selector) throws Exception {
    return waitForObject(selector, 30000);
  }
}

Installation
#!/bin/bash

app_package="com.jackkum.tester"

ADB="adb -s deviceId"
ADB_SH="$ADB shell"

path_sysapp="/system/app"
path_tmpapp="/data/local/tmp"
apk_host="./tester/build/outputs/apk/tester-debug-androidTest.apk"

./gradlew :tester:assembleDebug :tester:assembleDebugAndroidTest || exit -1 

# Install APK: using adb root
$ADB push $apk_host $path_tmpapp/com.jackkum.tester.test || exit -1 
$ADB_SH su -c "mv $path_tmpapp/com.jackkum.tester.test $path_sysapp/com.jackkum.tester.test" || exit -1 

# Stop the app
$ADB_SH "am force-stop $app_package"

$ADB_SH pm install -r "$path_sysapp/com.jackkum.tester.test" || exit -1

launch
#!/bin/bash

ADB="adb -s deviceId"
ADB_SH="$ADB shell"

# Re execute the tests
$ADB_SH su -c "am instrument -w -r -e debug false com.jackkum.tester.test/android.support.test.runner.AndroidJUnitRunner"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question