Search code examples
androidcrashandroid-multidex

Trying to set MultiDexApplication , ClassCastException


Scene:

I'm trying to set Firebase Messaging to the application, but when I add classpath 'com.google.gms:google-services:3.0.0' , apply plugin: 'com.google.gms.google-services' in the correct gradle files, and the google-services.json file, I try to launch the application but It crashes:

java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.res.Resources.getResourcePackageName(int)' on a null object reference 

Reading a lot in SO, I read in some litte comment that maybe occur due the 65K method limit. So I tried to "Implement" the multidex in the app.

I followed the few steps that are in android guide -> https://developer.android.com/studio/build/multidex.html

The problem, is now I get this error:

09-14 11:42:36.371 31466-31466/es.in2.otr.app.im E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: es.in2.otr.app.im, PID: 31466
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{es.in2.otr.app.im/es.in2.otr.app.im.app.WelcomeActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to es.in2.otr.app.im.app.ImApp
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                    Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to es.in2.otr.app.im.app.ImApp
                                                                       at es.in2.otr.app.im.app.ThemeableActivity.onCreate(ThemeableActivity.java:40)
                                                                       at es.in2.otr.app.im.app.WelcomeActivity.onCreate(WelcomeActivity.java:98)
                                                                       at android.app.Activity.performCreate(Activity.java:6876)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:158) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

In the gradle file I set multiDexApplication true and compiled de multidex library.

IN the Manifest I have:

<application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher_in2"
        android:label="@string/im_label"
        android:theme="@style/AppTheme"
        tools:node="merge"
        tools:replace="icon, label, android:allowBackup">
.....
</application>

I changed my android:name=".app.ImApp" for android:name="android.support.multidex.MultiDexApplication"

Then I went to ImApp class and added the

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

And tried to launch the App, but crashed with the error I post it before. After this I went to WelcomeActivity and ThemeableACtivity, and I have done the same. I added the attachBaseContext.

But I have the same problem.

How can I fix it? Thanks.


Solution

  • android:name="your.package.YourApplicationClass"
    

    Try this. Here extend yoru YourApplicationClass to MultiDexApplication

    YourApplicationClass extends MultiDexApplication