java 尝试谷歌分析时出现“错误:找不到符号变量 xml”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32078992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:37:17  来源:igfitidea点击:

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

javaandroidxmlgoogle-analytics

提问by faz

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.

我正在尝试为我的一个 android 应用程序实施谷歌分析。我对分析和 Android 应用程序开发完全陌生。我想尝试谷歌开发者网站中给出的例子。在尝试编译他们的代码时,我收到指向分析应用程序 java 文件的错误,mTracker = analytics.newTracker(R.xml.global_tracker);行,.xml 突出显示。我在这里发布了整个代码。

This is AnalyticsApplication.java

这是 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

我的 Global_tracker.xml 文件

<?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.

谢谢。

回答by Hussein El Feky

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

您需要在 res 文件夹中创建一个名为的新文件夹,xml并将您的文件移动到该文件夹​​中。还要确保正确调用布局名称,因为在您的问题中,您写的Global_tracker.xmlglobal_tracker.xml. 这太重要了。

Hope that helps.

希望有帮助。

回答by Arsen Khondkaryan - Android

Check if you don't forget to put

检查你是否没有忘记把

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

应用插件:'com.google.gms.google-services'

in app level build.gradle

在应用级 build.gradle

回答by evasyuk

I had the similar issue when was preforming downgrade to lower version from

我在执行降级到较低版本时遇到了类似的问题

com.google.gms:google-services:3.0.0

to

com.google.gms:google-services:1.3.1

(Do not ask why(it was an assumption in order to fix CI build script in a hurry))

(不要问为什么(这是为了匆忙修复 CI 构建脚本的假设))

So, make sure that you did not made downgrade changes

因此,请确保您没有进行降级更改

回答by Devidas M Das

I also faced the same issue with play service library version 10.2.1 for gcm and google analytics and issue is simply fixed by adding global_tracker.xmlas discussed in the @Hussein El FekyAnswer but only that fix is not solved my issue also i've added two more lines in strings.xml:

我也遇到了与 gcm 和 google 分析的 play 服务库版本 10.2.1 相同的问题,问题只是通过添加global_tracker.xml来解决,如@Hussein El FekyAnswer 中所述,但只有该修复程序没有解决我的问题,我也是在strings.xml 中添加了另外两行:

<string name="global_tracker">UA-XXXXXXXX-XX</string>
<string name="gcm_defaultSenderId">PROJECT-NO</string>

回答by Abel Callejo

The brute forcesolution 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

而不是遵循 android 常规程序

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 stringvalue of the Google Analytics tracking code.

这是解决问题的一种非常野蛮的方法,但它确实有效,因为newTracker()方法只需要Google Analytics 跟踪代码的实际字符串值。

The official reference documenteven have the same paradigm example.

官方参考文档甚至有相同的典型的例子。

回答by Shashank Bhong

I was facing the similar issue.

我面临着类似的问题。

Use classpath com.android.tools.build:gradle:1.2.3. That solved my problem.

使用类路径com.android.tools.build:gradle:1.2.3。那解决了我的问题。