Home » Android » Could not find class com.google.android.gms.analytics.GoogleAnalytics

Could not find class com.google.android.gms.analytics.GoogleAnalytics

Posted by: admin June 15, 2020 Leave a comment

Questions:

Well, this is a weird error and difficult to find. Just before starting:

  • In my app I have Admob installed and working. So the configuration of GooglePlayServices seems ok (lib added as a project…).
  • I am using v17 of googleplayservices. Also tested with v21 and v22.
  • My mobile has android 2.3

Basically the problem I am having is the following, I followed up the tutorial from android (one of the worst I have seen…) and in the Application class (that was existing before) I included the tracker function:

public synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      Tracker t = analytics.newTracker(PROPERTY_ID);
      mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}

I added the public keyword to the function because it was not visible…

So, when I use that function in whatever part of the app I get the following error:

09-27 16:43:54.099: E/dalvikvm(2407): Could not find class
‘com.google.android.gms.analytics.GoogleAnalytics$b’, referenced from
method
com.google.android.gms.analytics.GoogleAnalytics.enableAutoActivityReports

And later on the following error:

09-27 16:43:54.129: E/AndroidRuntime(2407): Caused by:
java.lang.NullPointerException 09-27 16:43:54.129:
E/AndroidRuntime(2407): at
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
09-27 16:43:54.129: E/AndroidRuntime(2407): at
com.google.android.gms.analytics.t.(Unknown Source) 09-27
16:43:54.129: E/AndroidRuntime(2407): at
com.google.android.gms.analytics.t.q(Unknown Source) 09-27
16:43:54.129: E/AndroidRuntime(2407): at
com.google.android.gms.analytics.GoogleAnalytics.(Unknown
Source) 09-27 16:43:54.129: E/AndroidRuntime(2407): at
com.google.android.gms.analytics.GoogleAnalytics.getInstance(Unknown
Source)

Any idea how can be solved?

How to&Answers:

So, finally it got solved. Just in case somebody experienced the same problem as myself. I provide here what’s the last action I made that finally made working google analytics:

  • Using device 2.3.6 (Samsung Galaxy Ace)
  • I updated Android SDK tools from 24.0.1 to 24.0.2
  • I updated Android Support Repository from 10 to 11.
  • My application was using android-support-v7 and google_play_services
  • It was already using Admob from google-play-services (no evidence google-play-services was wrongly installed).

So, I don’t know what make it work finally, but looks like related to Android SDK tools or Android Support REpository updates.

I am just leaving this information just in case somebody is facing the same problem and you are able to isolate the problem. If someone is reading this solution I propose to isolate the updates so that we can provide better information other people.

Hope it helps!

Answer:

Make sure that you not only added the library to your project, but you have to link it as well. Go to project properties, then android, and in the right pane you will see “library”. Make sure you see the google play services library that you want with a green check. If you don’t see it, add it.

Another cause could be that you did not import the parts of the library that you need into your activity via your import statements, but usually the problem above is what most often occurs or what people forget to do. But the NULL pointer exception message is exactly what it means, and if you have problems in either of the two mentioned areas, that’s the exact message you will get because it can’t find a resource or class that you are referencing.