The Network Traffic tool is deprecated. If you're using Android Studio 3.0 or higher, you should use the Network Profiler to examine how and when your app transfers data over a network.
The network traffic generated by an app can have a significant impact on the battery life of the device where it is running. In order to optimize that traffic, you need to both measure it and identify its source. Network requests can come directly from a user action, requests from your own app code, or from a server communicating with your app.
The Network Traffic tool (part of the DDMS tools) enables you to view how and when your app transfers data over a network.
This lesson shows you how to measure and categorize network requests by tagging your source code, then shows you how to deploy, test and visualize your apps's network traffic.
Tag network requests
Apps use the networking hardware on a device for various reasons. In order to properly optimize your app's use of networking resources, you must understand how frequently your app is using the network and for what reasons. For performance analysis purposes, you should break down use of network hardware into these categories:
- User-initiated network requests - Requests initiated by the user, such as a user request for an updated articles list in a news app.
- App-initiated network requests - Requests initiated within Android app code that are not used to immediately satisfy a user action, such as an app request to cache the text of unread articles in a news app.
- Server-initiated network requests - Requests initiated by a server to your app that are not used to immediately satisfy a user action, such as notification of a newly available article in a news app.
This procedure shows you how to tag your app's source code with constants to categorize traffic as one of these three request types. The Network Traffic tool represents each type of traffic with a different color, so you can visualize and optimize each traffic stream separately. The technique described here reports network traffic based on the execution of threads in your app which you identify as a user, app or server source.
- In your app's development project, define three constants to represent the different types
of network use:
Kotlin
const val USER_INITIATED: Int = 0x1000 const val APP_INITIATED: Int = 0x2000 const val SERVER_INITIATED: Int = 0x3000
Java
public static final int USER_INITIATED = 0x1000; public static final int APP_INITIATED = 0x2000; public static final int SERVER_INITIATED =0x3000;
- Find networking code in your app by searching for the most common classes used for
this purpose:
- In Android Studio, choose Edit > Find > Find in Path.
- Paste the following string into the Text to find field:
extends GcmTaskService|extends JobService|extends AbstractThreadedSyncAdapter|HttpUrlConnection|Volley|Glide|HttpClient
- Check Regular expression.
- Check File mask(s) and type
*.java
. - Click the Find button.
-
Based on your findings in the previous step, tag your app's use of network traffic by adding the
setThreadStatsTag(int)
method to each execution thread in your app that uses network resources, as shown in the following code example.Kotlin
if (BuildConfig.NETWORK_TEST && Build.VERSION.SDK_INT >= 14) { try { TrafficStats.setThreadStatsTag(USER_INITIATED) // make network request using HttpClient.execute() } finally { TrafficStats.clearThreadStatsTag() } }
Java
if (BuildConfig.NETWORK_TEST && Build.VERSION.SDK_INT >= 14) { try { TrafficStats.setThreadStatsTag(USER_INITIATED); // make network request using HttpClient.execute() } finally { TrafficStats.clearThreadStatsTag(); } }
Note: Ensure the tagging does not get into your production code by making inclusion of this code conditional, based on the build type used to generate the APK. In the example above, the
BuildConfig.NETWORK_TEST
field identifies this APK as a test version.
Note: This technique for tagging network traffic from your app depends on
how the APIs that you are using access and manage network sockets. Some networking libraries
may not allow the TrafficStats
utilities to tag traffic from your app.
For more information about tagging and tracking network traffic with the Network Traffic tool, see Detailed Network Usage in DDMS.
Configure a network test build type
When you run performance tests, your APK should be as close as possible to the production
build. In order to achieve this for your network testing, create a network-test
build type, rather than using debug
build type.
- Open your app in Android Studio.
- Create a debuggable build type for your network test by modifying your project's
build.gradle
file as shown in the following code example:android { ... buildTypes { debug { // debuggable true is default for the debug buildType } network-test { debuggable true } } ... }
Deploy the network test APK
To deploy the APK generated by the network-test
build type configured in the previous
proceedure:
- Check that Developer Options are enabled on your test device. For information about how to check and enable this option, see Using Hardware Devices.
- Using a USB cable, connect your test device to your development computer.
- In Android Studio, select Build Variants on the left edge of the window.
- Click the Sync Project with Gradle Files button to populate the
Build Variants list with
network-test
for the app module. - Choose
network-test
from the list. - Deploy the debuggable version of your app to your device by choosing Run > Debug.
Run network traffic tool
The Network Traffic tool in Android Studio helps you see how your app uses network resources in real time, while it is running.
To improve the repeatability of your testing, you should start with a known initial state for your app by clearing app data. The following procedure includes a step that shows you how to clear all app data including previously cached data and networking data. This step puts your app back to a state where it must re-cache all previously cached data. Do not skip this step.
To start the Network Traffic tool and visualize the network requests:
- Start the Network Traffic tool by launching Android Studio and starting Android Device Monitor. When asked, allow incoming network connections.
- In the Android Device Monitor window, click the DDMS button along the top and choose the Network Statistics tab. If you don't see this tab, widen the window and then try Window > Reset Perspective.
- Select your app to debug from the list of debuggable apps on your device in the
Devices tab, then click the Start button in the
Network Statistics tab.
Note: You may be prompted to Allow USB Debugging on your device. Select OK to allow debugging to proceed.
- Clear your app data using the following adb command:
adb shell pm clear package.name.of.app
- Start your app and run a testing plan that exercises your app's primary use cases. Your plan should also allow for app idle time, where the user is not interacting with the app, to allow app-initiated and server-initiated network access to occur.
- Repeat the test by clearing the app data and running your test plan again. You should repeat the test a few times to verify the repeatability of your performance data.
Use of tagging for network traffic helps you visually distinguish each request category by producing a different color for each network traffic in the Network Traffic tool, as shown in Figure 1.

Figure 1. Network traffic tagged for the three categories.