14

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'

}
Levon
  • 1,681
  • 2
  • 18
  • 40
Carlos Hernández Gil
  • 1,853
  • 2
  • 22
  • 30
  • show your `build-gradle` – IntelliJ Amiya Apr 08 '16 at 10:03
  • okay . for test case use `compile 'com.google.android.gms:play-services:7.3.0'` no need `services-analytics:8.3.0` – IntelliJ Amiya Apr 08 '16 at 10:12
  • any crash if comment out `apply plugin: 'com.google.gms.google-services'` ?? you can set this bottom line of your configuration – IntelliJ Amiya Apr 08 '16 at 10:14
  • I've implemented the solution given by Levon? Do you want me to put again global_tracker.xml and try it? – Carlos Hernández Gil Apr 08 '16 at 10:24
  • when using compile 'com.google.android.gms:play-services:7.3.0' without apply plugin: 'com.google.gms.google-services' i get the following error building: * What went wrong: Execution failed for task ':app:transformClassesWithJarMergingForDebug'. > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/android/gms/internal/zzmx.class – Carlos Hernández Gil Apr 08 '16 at 10:30
  • 1
    this error dissapears when i change from compile 'com.google.android.gms:play-services-gcm:8.3.0' to 7.3.0 – Carlos Hernández Gil Apr 08 '16 at 10:34
  • comment `multiDexEnabled true` – IntelliJ Amiya Apr 08 '16 at 10:35
  • FAILURE: Build failed with an exception. commenting multiDexEnabled: * What went wrong: Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 2 – Carlos Hernández Gil Apr 08 '16 at 10:41

1 Answers1

20

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.

Levon
  • 1,681
  • 2
  • 18
  • 40
  • The error dissapeared, but the problem is that Analytics is not showing current User. I have done another little app to test Analytics with this site https://developers.google.com/analytics/devguides/collection/android/v4/#set-up-your-project and this one https://www.javacodegeeks.com/2014/04/working-with-google-analytics-api-v4-for-android.html all worked perfectly(including global_tracker.xml). It is strange – Carlos Hernández Gil Apr 08 '16 at 10:22
  • Showing current user is a different unrelated issue. If my answer helped you with your original question, please mark it as accepted answer. Then post a different question specifically about current user issue. Thanks. – Levon Apr 08 '16 at 10:40
  • don't we need the tracker object ? Tracker mTracker = application.getDefaultTracker(); – iappmaker Jun 21 '16 at 05:16
  • @iappmaker Yes, you still need the Tracker object in your code, but the question here was about the configuration of the project, rather than how to implement it in code. – Levon Jun 21 '16 at 10:12
  • How can we do this and still apply custom settings? Is there an alternative solution that involves 'fixing' the autogeneration of a global_tracker.xml? – androidguy Jul 03 '17 at 02:49
  • The problem dissapear but now I'm getting: "You have wrong OAuth2 related configurations, please check. Detailed error: INVALID_AUDIENCE" – Diego Venâncio Aug 02 '17 at 18:02
  • @DiegoVenâncio Please start a new post about your OAuth2 error, as it's not related to what the OP asked. Thanks! – Levon Aug 03 '17 at 23:47