Mobile Development 11 min read

Android Network Traffic Measurement Methods and Tools

This article explains various Android network traffic measurement techniques, including reading Linux proc files, using the TrafficStats and NetworkStatsManager APIs, and built‑in monitoring tools, while discussing their advantages, limitations, and practical code examples for accurate per‑app data usage analysis.

Baidu Intelligent Testing
Baidu Intelligent Testing
Baidu Intelligent Testing
Android Network Traffic Measurement Methods and Tools

Network traffic consumption is a sensitive metric for app users because it can be directly linked to cost; uncontrolled traffic can lead to poor user experience.

Following a previous discussion on CPU performance testing, this article briefly introduces network traffic statistics.

Terminology

Network traffic refers to the data flow generated by a device connected to a network. Performance data distinguishes between received (downlink) and transmitted (uplink) traffic, measured in bytes.

Approaches

Traffic measurement tools and methods can be grouped into three categories based on their underlying principle:

Reading Linux traffic statistic files

Using Android traffic statistic APIs

Tcpdump capture + Wireshark analysis (requires root, not covered here)

The article focuses on the first two methods.

Reading Linux traffic statistic files

Android, being Linux‑based, stores traffic data in files similar to CPU data in /proc/pid/stat . Relevant files include:

/proc/net/dev – system‑wide traffic

/sys/class/net – per‑interface statistics (e.g., rx_bytes , tx_bytes )

/proc/uid_stat/{uid} – tcp_rcv and tcp_snd for low‑version phones

/proc/net/xt_qtaguid/stats – UID‑specific traffic

1. Reading /proc/net/dev

The file lists overall system traffic. Example screenshot:

Columns include bytes , packets , errs , and drop . Interfaces such as wlan0 (Wi‑Fi) and rmnet0 (GPRS) may vary by ROM, and some devices lack this file.

Limitations: the data is not per‑app; it reflects total device traffic.

2. Reading /sys/class/net

Each interface directory contains a statistics subdirectory with rx_bytes and tx_bytes . Example screenshot:

Like the previous method, it does not differentiate traffic by application.

3. Reading /proc/uid_stat/{uid}

This approach can retrieve traffic for a specific UID, allowing per‑app measurement. UID is the user identifier assigned to each Android app.

Obtaining UID

Methods include:

Running adb shell dumpsys package and locating userId in the package section.

Reading /proc/{pid}/status after identifying the process PID; the Uid line provides the UID (requires the process to be running).

Once the UID is known, /proc/uid_stat/{uid} contains tcp_rcv and tcp_snd files with received and sent bytes.

Limitations: only TCP traffic is counted, and the path is absent on Android 4.3.1 and later.

4. Reading /proc/net/xt_qtaguid/stats

This file offers comprehensive traffic statistics, compatible with newer Android versions. The Android TrafficStats API internally parses this file.

Sample content (image):

Key columns include iface (interface), acct_tag_hex (socket), uid_tag_int (UID), cnt_set (0 = foreground, 1 = background), rx_bytes (received), and tx_bytes (transmitted). By filtering rows with the target UID and summing rx_bytes and tx_bytes , per‑app traffic can be calculated.

Pseudo‑code for obtaining current cumulative Wi‑Fi traffic (image):

By periodically sampling the cumulative values and computing differences, short‑interval consumption and overall performance results can be derived.

Using Android TrafficStats API

Since Android 2.2, the android.net.TrafficStats class provides static methods (returning long ) to query device‑wide and UID‑specific traffic. If a method returns –1, the device does not support the query.

Typical functions (image):

Limitations:

Data resets after device reboot; persistence is required for long‑term monitoring.

Cannot distinguish traffic by network type (e.g., Wi‑Fi vs. cellular) without additional handling.

No API to query traffic for a specific time interval.

NetworkStatsManager (Android 6.0+)

The newer NetworkStatsManager API offers more robust historical data without the reboot limitation.

Key functions (image):

Drawbacks: requires the privileged android.permission.PACKAGE_USAGE_STATS permission, which must be granted by the user.

Built‑in Tools

Developers can also use native tools to monitor traffic:

Device’s built‑in traffic monitor (screenshots shown).

DDMS (Dalvik Debug Monitor Server) provides network usage graphs from Android 4.0 onward.

Android Studio’s Network Monitor assists during development and testing.

Author

Yu Hui – a newcomer to automated testing, aspiring to excel at coding while also enjoying badminton and dancing.

Mobile developmentAndroidperformance testingnetwork trafficNetworkStatsManagerTrafficStats
Baidu Intelligent Testing
Written by

Baidu Intelligent Testing

Welcome to follow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.