How to fix Error: Duplicate resources for global_tracker.xml when adding Google Analytics?

Carlos Hernández Gil picture Carlos Hernández Gil · Apr 8, 2016 · Viewed 9.1k times · Source

I'm trying to implement Google Analytics in my App , but when compiling I get the following error:

Error:Execution failed for task ':app:mergeDebugResources'. [xml/global_tracker] C:\Users\Carlos\AndroidStudioProjects\Capstone\SP\Stocks Panel Lite\app\src\main\res\xml\global_tracker.xml [xml/global_tracker] C:\Users\Carlos\AndroidStudioProjects\Capstone\SP\Stocks Panel Lite\app\build\generated\res\google-services\debug\xml\global_tracker.xml: Error: Duplicate resources

I've tried to run clean project, but I get the same error. I also get the same error if I remove app/build.

Thanks in advance.

UPDATE: build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.carlos.capstone"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
    }

}
repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:gridlayout-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:support-annotations:23.1.1'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.1.1'
    compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
    compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    compile 'com.bignerdranch.android:expandablerecyclerview:2.0.4'
    compile 'com.crazyhitty.chdev.ks:rss-manager:0.21'
    compile 'com.github.frankiesardo:linearlistview:1.0.1@aar'
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.facebook.stetho:stetho-urlconnection:1.3.1'
    compile 'com.facebook.stetho:stetho:1.3.1'
    compile 'com.android.support:customtabs:23.0.0+'
    compile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    compile 'org.apache.commons:commons-lang3:3.1'
    compile 'org.apache.poi:poi:3.14'
    compile 'net.sf.supercsv:super-csv:2.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'com.google.android.gms:play-services-analytics:8.3.0'

}

Answer

Levon picture Levon · Apr 8, 2016

This happens when you place a global_tracker.xml in your resources xml folder and in addition to that add apply plugin: 'com.google.gms.google-services' in your build.gradle file. That is mixing the old way of implementing the tracker with the new way of doing things. The solution would be to simply remove the global_tracker.xml from your xml folder, as the google-services plugin will automatically add all the required data from the google-services.json configuration file when building your app.

Also, it's advisable to place the apply plugin: 'com.google.gms.google-services' line at the bottom of your build.gradle after the dependencies section. It's strange, but having it at the top causes errors in some configurations.

In addition, you might see error: cannot find symbol variable global_tracker when trying to build your project, especially if you are converting an older project, and downloaded your google-services.json from your Firebase console while still using it with the older Google Analytics. In that case, open the newly added google-services.json and find "analytics_service" in the "services" object. You will need to add the following:

"analytics_property": {
    "tracking_id": "<your tracking id>"

so the entire section looks like this (place your own UA-XXXXXXXX-X tracking ID there):

"analytics_service": {
  "status": 2,
  "analytics_property": {
      "tracking_id": "UA-12345678-9"
  }
},

After that make sure to Clean your project and re-sync Gradle before you rebuild. Hope that helps.