I have a problem, my app generate this exception,and i don't understand.
I have implement the multiDexEnabled in my build.gradle
Caused by: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.example.AnalyticsApplication
My Class Java
public class Splash extends Activity {
private Tracker mTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
//Analitycs
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.setScreenName("Splash");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
file Gradle
defaultConfig {
multiDexEnabled true
}
Manifest.xml
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".Splash">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter></activity>
</application>
I think you should extend the AnalyticsApplication
class into your own class, like this:
public class YourApplicationName extends AnalyticsApplication {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
//Analitycs
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.setScreenName("Splash");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}
// Here you will enable Multidex
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(getBaseContext());
}
}
After this, you must change your AndroidManifest.xml
file to this:
<application
android:name="path.to.YourApplicationName"
...
Please, check this link for more information: http://developer.android.com/reference/android/support/multidex/MultiDex.html
I have the same issue this cause of you set android:name="android.support.multidex.MultiDexApplication"
in your manifest so your application call is type of MultiDexApplication
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".Splash">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter></activity>
</application>
my application class
public class MyApplication extends Application {
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 .
}
public HashMap<TrackerName, Tracker> mTrackers = new HashMap<>();
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker tracker = (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 , tracker);
}
return mTrackers.get(trackerId);
}
private Activity mCurrentActivity = null;
public void setCurrentActivity(Activity mCurrectActivity) {
this.mCurrentActivity = mCurrectActivity;
}
public Activity getCurrentActivity() {
return mCurrentActivity;
}
private static Context mAppContext;
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
mInstance = this;
this.setAppContext(getApplicationContext());
}
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
public static MyApplication getInstance() {
return mInstance;
}
public static Context getAppContext() {
return mAppContext;
}
public void setAppContext(Context mAppContext) {
this.mAppContext = mAppContext;
}
}
this why when I call this method (initialize application instance ) it crash , it try cast MultiDexApplication
to MyApplication
MyApplication application = (MyApplication)getApplication();
so to fix this issue just change your manifest attribute name to MyApplication
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication"> // add path to your application class here
You should extend your Application
class from MultidexApplication
and use it in manifest instead of using android.support.multidex.MultiDexApplication
directly.
To React Native:
// MainApplication
...
import android.content.Context;
import android.support.multidex.MultiDex;
...
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(getBaseContext());
}
}
Just remove that line
android:name="android.support.multidex.MultiDexApplication"
from manifest file and put your Application name . i.e
android:name="com.yourAppNamae.activity.YourApplicationName"
That worked for me
There are several correct solutions here. But, if you're using react-native >= 0.60
or you're using androidx
then this is the solution:
// MainApplication
...
import androidx.multidex.MultiDexApplication;
...
public class MainApplication extends MultiDexApplication implements ReactApplication {
...
}