Search code examples
androidgoogle-analyticscrashcocos2d-xsoomla

ClassCastException: com.soomla.SoomlaApp cannot be cast to org.cocos2dx.cpp.AnalyticsApplication


I want to implement Google-Analytics and Soomla Store for In-App-Purchase. The problem is, every time I want to track a screen, my app crashes with Android. Under iOS it works like a charm.

The reason for the crash:

java.lang.ClassCastException: com.soomla.SoomlaApp cannot be cast to org.cocos2dx.cpp.AnalyticsApplication

I think there is an error in the AndroidManifest.xml. I'm very new to Android, but I'm experienced in iOS development.

The crash occurs when I call GoogleAnalyticsTracker::sendScreen("Test Screen") in my C++ code.

Part of GoogleAnalyticsTracker.cpp:

void GoogleAnalyticsTracker::sendScreen(const char* screenName)
{
    sendScreenJNI(screenName);
}

Part of GoogleAnalyticsTracker_JNI.cpp

#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include "GoogleAnalyticsTracker_JNI.h"
#define CLASS_NAME "org/cocos2dx/cpp/AppActivity"

void sendScreenJNI(const char* screenName) {
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "sendScreen", "(Ljava/lang/String;)V"))
    {
        jstring jScreenName = t.env->NewStringUTF(screenName);
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jScreenName);
        t.env->DeleteLocalRef(jScreenName);
        t.env->DeleteLocalRef(t.classID);
    }
}

Part of my AndroidManifest.xml:

    <application android:name="com.soomla.SoomlaApp" android:label="@string/app_name"
             android:icon="@drawable/icon">
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <!-- Tell Cocos2dxActivity the name of our .so -->
    <meta-data android:name="android.app.lib_name"
              android:value="cocos2dcpp" />
    <activity android:name="org.cocos2dx.cpp.AppActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:configChanges="orientation">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="org.cocos2dx.cpp.AnalyticsApplication"/>
    <activity android:name="com.soomla.store.billing.google.GooglePlayIabService$IabActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
    <meta-data android:name="billing.service" android:value="google.GooglePlayIabService"/>
</application>

Here's the Code of the AnalyticsApplication.java

package org.cocos2dx.cpp;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import ch.steeve.game.R;

import android.app.Application;

public class AnalyticsApplication extends Application {
    Tracker mTracker;
    synchronized Tracker getTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(R.xml.ga_tracker);
        }
        return mTracker;
    }
}

Part of AppActivity.java

public static void sendScreen(String screenName) {
    Tracker t = ((AnalyticsApplication) Cocos2dxActivity.getContext().getApplicationContext()).getTracker();
    t.setScreenName(screenName);
    t.send(new HitBuilders.AppViewBuilder().build());
}

Solution

    1. This string in AndroidManifest is incorrect:

      activity android:name="org.cocos2dx.cpp.AnalyticsApplication"

      AnalyticsApplication not activity, it Application. Remove this string.

    2. Your AnalyticsApplication should extends com.soomla.SoomlaApp

    3. In AndroidManifest replace string:

      application android:name="com.soomla.SoomlaApp" android:label="@string/app_name" android:icon="@drawable/icon"

      to

      application android:name="org.cocos2dx.cpp.AnalyticsApplication" android:label="@string/app_name" android:icon="@drawable/icon"