How to: Separate your debug Android activities from your live Analytics with Gradle
Utilizing Gradle build types to inject different configurations for separating analytics tracking for different build types

  Jan 29, 2015 -   read
  android, gradle, google-analytics

Gradle plugin for Android is the official build tool for Android, and is getting better everyday with new features allowing you to customize your build process more and more. One of its feature is allowing one to inject resources that can have different values per build type/product flavor. We can utilize it to separate analytics data for development and production.

In your build.gradle:

android {
    defaultConfig {
        applicationId "<appId>"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        resValue "bool", "debug", "true"

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
            signingConfig signingConfigs.release
            zipAlignEnabled true
            resValue "bool", "debug", "false"

        <other build types>

This script uses resValue to set debug bool resource to true by default for all build types, and overriden by release build type. We can then access this bool resource from Java code to configure Google Analaytics based on debug value (I did it in my Application class in this case)

public class Application extends {
    public void onCreate() {
        if (getResources().getBoolean(R.bool.debug)) {

Update (May 12th, 2015)

From Google Play Serivces 7.0.3, Logger interface has been deprecated and one can only configure log level per device, by using adb shell setprop log.tag.GAv4 <log-level>

For more information, see API documentation.

With the above setup, Google Analytics will show verbose GAV4 logging but not sending real hit to Google Analytics server except for your release builds, which I assume are only used for production. More advanced settings can be applied in similar fashion to configure different GA tracker per build type, or some other setups based on your needs.

Ha Duy Trung
Currently cooking, brewing and building Android stuffs.