Disable Google Android Instrumentation Test Tracking

Google has started to track Android developers that run instrumentation tests sending all sorts of data about you and your device to Google Analytics. The opt-out solution they give is to add these options to the adb command:

adb shell am instrument -e disableAnalytics true

However, this is not how I typically run my tests. A more convinient and permanent solution is to add the following to your build.gradle file.

android {
...
defaultConfig {
...
testInstrumentationRunnerArguments disableAnalytics: 'true'
}
}

That’s it. Now the opt-out should work.

I could not find the code of androidx.test.internal.runner.tracker.AnalyticsBasedUsageTracker online, but in case you are interested here’s what they send:

new StringBuilder()
.append(APP_NAME_PARAM)
.append(encode(targetPackage, UTF_8))
.append(TRACKER_ID_PARAM)
.append(encode(trackingId, UTF_8))
.append("&v=1") // Protocol Version.
.append("&z=") // Cache Buster (optional)
.append(SystemClock.uptimeMillis())
.append(CLIENT_ID_PARAM)
.append(encode(userId, UTF_8))
.append(SCREEN_RESOLUTION_PARAM)
.append(encode(screenResolution, UTF_8))
.append(API_LEVEL_PARAM)
.append(encode(apiLevel, UTF_8))
.append(MODEL_NAME_PARAM)
.append(encode(model, UTF_8))
.append("&t=appview") // Hit type
.append("&sc=start") // Session Control
.toString();