gms.analytics.GoogleAnalytics - NoClassDefFoundError

3.3k views Asked by At

My Aplication Class

public class MyApplication extends Application {

private static final String PROPERTY_ID = "UA-XXXXXX-X";
public static int GENERAL_TRACKER = 0;

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg:
                    // roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a
                        // company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public MyApplication() {
    super();
}

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

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics
                .newTracker(PROPERTY_ID)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics
                        .newTracker(R.xml.global_tracker) : analytics
                        .newTracker(R.xml.ecommerce_tracker);
        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
  }
}

app_tracker.xml

<string name="ga_trackingId">UA-XXXXXX-X</string>
<string name="ga_sampleFrequency">100</string>

<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>

<integer name="ga_sessionTimeout">-1</integer>

<screenName name="com.mobihouse.portabilidade.activities.HomeActivity_">HomeActivity</screenName>

global_tracker.xml

<integer name="ga_sessionTimeout">300</integer>

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<string name="ga_logLevel">verbose</string>

<!-- The screen names that will appear in reports -->
<screenName name="com.mobihouse.portabilidade.activities.HomeActivity_">HomeActivity Portabilidade</screenName>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-XXXXXX-X</string>

At my Manifest, i already added this:

    <application
    android:name="com.mobihouse.portabilidade.MyApplication"

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.gms.analytics.globalConfigResource"
        android:resource="@xml/global_tracker" />

I changed my PROPERTY_ID to post here.

And the problem is, log:

11-25 09:23:30.031: E/AndroidRuntime(8392): FATAL EXCEPTION: main 11-25 09:23:30.031: E/AndroidRuntime(8392): java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics 11-25 09:23:30.031: E/AndroidRuntime(8392): at com.mobihouse.portabilidade.MyApplication.getTracker(MyApplication.java:45)

And:

11-25 09:23:29.992: I/dalvikvm(8392): Could not find method com.google.android.gms.analytics.Tracker.setScreenName, referenced from method com.mobihouse.portabilidade.activities.HomeActivity.onCreate

11-25 09:23:29.992: I/dalvikvm(8392): Could not find method com.google.android.gms.analytics.GoogleAnalytics.getInstance, referenced from method com.mobihouse.portabilidade.activities.HomeActivity.onStart

I searched several sites, however all solutions not worked, someone could help me?

I already update the google play services lib, my current version = 21, according to ADB Manager, I am trying to implement in Android 5.0 - API 21.

Also I already put the google play services as a lib in my project

1

There are 1 answers

0
Artem Mostyaev On BEST ANSWER

You need to link Google Play Services as Library project.

You can read more about Library projects on the official site. Shortly - you need to use library projects when additional projects includes not only source code, but resources also (layouts, strings).

If you need to use library that includes only source code, you can simply copy .jar file into libs folder.

Current version (v4) of Google Analytics for Android is included into Google Play Services project, that includes resources, so it must be referenced as library projects.

For example, previous version (v3) of Google Analytics for Android wasn't included into Google Play Services projects and was a simple .jar library.