"error: cannot find symbol variable xml" while trying google analytics

11,093

Solution 1

You need to create a new folder in your res folder called xml and move your file to that folder. Also make sure you call the layout name correctly because in your question, you wrote Global_tracker.xml instead of global_tracker.xml. This is so important.

Hope that helps.

Solution 2

Check if you don't forget to put

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

in app level build.gradle

Solution 3

The brute force solution is to simply provide the actual analytics code.

GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = analytics.newTracker("UA-XXXXXXXXX-X");

rather than following the android conventional procedure

GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = analytics.newTracker(R.xml.global_tracker);

It's a pretty savage way of fixing the issue but it surely works because the newTracker() method only needs the actual string value of the Google Analytics tracking code.

The official reference document even have the same paradigm example.

Share:
11,093

Related videos on Youtube

faz
Author by

faz

Updated on September 28, 2022

Comments

  • faz
    faz about 1 year

    I am trying to implement google analytics for one of my android apps. I am totally new for analytics and android app development. I thought of trying the examples given in the google developers site. When trying to compile their code, I am getting the error pointing the analytics application java file, mTracker = analytics.newTracker(R.xml.global_tracker); line, .xml is highlighted. I posted the whole code here.

    This is AnalyticsApplication.java

    package com.google.samples.quickstart.analytics;
    
    import android.app.Application;
    
    import com.google.android.gms.analytics.GoogleAnalytics;
    import com.google.android.gms.analytics.Logger;
    import com.google.android.gms.analytics.Tracker;
    
    public class AnalyticsApplication extends Application {
      private Tracker mTracker;
    
      synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
          GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
          // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
          mTracker = analytics.newTracker(R.xml.global_tracker);
        }
        return mTracker;
      }
    }
    

    My Global_tracker.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!--Replace placeholder ID with your tracking ID-->
        <string name="ga_trackingId">UA-XXXXXXXX-X</string>
    
        <!--Enable automatic activity tracking-->
        <bool name="ga_autoActivityTracking">true</bool>
    
        <!--Enable automatic exception tracking-->
        <bool name="ga_reportUncaughtExceptions">true</bool>
    </resources>
    

    My manifest file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.google.samples.quickstart.analytics">
    
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
        <application
            android:name=".AnalyticsApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>
    

    I have already spent two complete days in this issue, and I need your suggestion to go forwards.

    Thanks.

  • faz
    faz over 8 years
    Thanks a lot. that saved me.
  • Luten
    Luten almost 8 years
    What layout you are talking about? global_tracking.xml is an xml resource file that should be generated by GoogleAnalytics gradle plugin.
  • Hussein El Feky
    Hussein El Feky almost 8 years
    @Luten It is just a values resource file that the asker created somewhere else rather than xml folder as the title clearly states, and the asker didn't know that he should create a new folder called xml to fix this problem. So he should definitely create it.
  • Luten
    Luten almost 8 years
    1. Your answer says about layout files, which is confusing as there are no layout files. Also layout files should not be placed in xml folder. 2. This is not the right way you should work with google analytics. global_tracker.xml file is generated automatically, and should not be created manually. The asker;s problem is caused by broken autogeneration. Discussion and workarounds about this problem may be found here: github.com/googlesamples/google-services/issues/26
  • Hussein El Feky
    Hussein El Feky almost 8 years
    @Luten You're right about me writing layout files. It was a mistake from me, and I fixed it yesterday. Anyway, this solution solved the asker's issue. Moreover, global_tracker.xml is not a layout file; it is a values resource file. Also it should not be auto-generated as the asker is required to input his tracking id in a resource file.
  • Luten
    Luten almost 8 years
    @HusseinElFeky Edited your answer too. Removed downvote. As I already wrote - this file is generated automatically by Google Analytics gradle plugin. Asker is not required to input tracking id, it's just autogeneration error.
  • Guilherme Simão Couto
    Guilherme Simão Couto about 7 years
    at the end of the file
  • Gaket
    Gaket about 7 years
    For me it works even in module-level build.gradle, after applying application plugin.