Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception android.support.multidex.MultiDexApplication cannot be cast class

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>
like image 567
David Hackro Avatar asked Feb 26 '16 18:02

David Hackro


People also ask

How do I enable multidex?

To enable Multidex, edit the build. gradle file at the module level: android { compileSdk 31 defaultConfig { ... minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" multiDexEnabled true // Add this to enable Multidex testInstrumentationRunner "androidx.

What is Multidexapplication in Android?

In Android, the compilers convert your source code into DEX files. This DEX file contains the compiled code used to run the app. But there is a limitation with the DEX file. The DEX file limits the total number of methods that can be referenced within a single DEX file to 64K i.e. 65,536 methods.

How do I enable multidex in flutter project?

How do I enable multidex in flutter project? The multidex support is enabled automatically. If your app supports Android SDK versions below 21, and it exceeds the 64K method limit, simply pass the –multidex flag to flutter build appbundle or flutter build apk and your app will support multidex.


1 Answers

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

like image 198
Dan Cantir Avatar answered Sep 22 '22 08:09

Dan Cantir