Android Automation Interaction: Traditional ADB Scripts and Native Methods
This article examines Android automation techniques, comparing simple ADB script‑based interactions with native‑code approaches using activity lifecycle callbacks, view identification, and MotionEvent simulation, and discusses their advantages, limitations, and suitable application scenarios.
Android automation can replace manual repetitive tasks such as black‑box testing and third‑party app execution by programmatically launching apps, clicking views, dragging, and entering text. As app security improves, achieving full‑process automation becomes harder, prompting a review of common solutions, their pros and cons, and appropriate use cases.
1. Traditional Script Solution
ADB, Google’s command‑line tool, allows scripted interaction by sending commands that act on screen coordinates obtained via UIAutomator or XML dumps.
adb shell input tap 100 500The above command simulates a tap at coordinates (100, 500).
adb shell input swipe 100 500 200 600This command simulates a swipe from (100, 500) to (200, 600).
adb shell input keyevent "KEYCODE_BACK"This command simulates the back button press.
A complete automation flow can be built by executing these commands in sequence.
Advantages of ADB scripts
Simple to implement; only element coordinates are needed.
Can automate WebView interactions.
Disadvantages of ADB scripts
Low flexibility; hard‑coded coordinates break when UI changes.
Requires a stable ADB or socket connection; network issues affect reliability.
Typical scenarios for ADB scripts
Simple, low‑frequency interaction apps with low security.
WebView pages or apps built with Flutter.
2. Android Native Method for Automation
By leveraging plugin frameworks and registering ActivityLifecycleCallbacks , developers can monitor activity lifecycles and interact with UI elements directly.
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
private ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity.getClass() == MainActivity.class) Log.d(TAG, "MainActivityCreated.");
else if (activity.getClass() == SecondActivity.class) Log.d(TAG, "SecondActivityCreated.");
}
@Override public void onActivityStarted(Activity activity) { Log.d(TAG, "onActivityStarted."); }
@Override public void onActivityResumed(Activity activity) { Log.d(TAG, "onActivityResumed."); }
@Override public void onActivityPaused(Activity activity) { Log.d(TAG, "onActivityPaused."); }
@Override public void onActivityStopped(Activity activity) { Log.d(TAG, "onActivityStopped."); }
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override public void onActivityDestroyed(Activity activity) { Log.d(TAG, "onActivityDestroyed."); }
};
@Override public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
@Override public void onTerminate() {
unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
super.onTerminate();
}
}After detecting an activity, UIAutomator can retrieve view IDs and properties, allowing different automation strategies based on view characteristics.
2.1 Handling Simple Views
For views with a resource ID and clickable=true, the following code can be used:
int fl_btn = activity.getResources().getIdentifier("dashboard_title", "id", "com.android.settings");
View v = activity.findViewById(fl_btn);
v.performClick();2.2 Handling Hidden or Non‑Clickable Views
When views are hidden or lack IDs (e.g., in React Native or Flutter), Xpath can locate them, but performClick fails. Instead, simulate low‑level touch events using MotionEvent :
private void simulateClick(View view, float x, float y) {
long time = SystemClock.uptimeMillis();
MotionEvent downEvent = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, x, y, 0);
time += 500;
MotionEvent upEvent = MotionEvent.obtain(time, time, MotionEvent.ACTION_UP, x, y, 0);
view.onTouchEvent(downEvent);
view.onTouchEvent(upEvent);
}For swipe actions, additional MotionEvent.ACTION_MOVE events can be inserted.
Advantages of plugin‑based native automation
Strong maintainability: directly interacts with view objects, so UI changes often require no code changes.
Higher fidelity: mimics real user interactions more closely than scripts.
Disadvantages
Limited support for WebView and Flutter‑based apps.
Applicable scenarios
Apps with frequent version updates.
Apps not built with Flutter.
Both script‑based and native‑method approaches can handle basic interaction flows, and they can be extended to more complex tasks such as automated verification.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.