title: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.xxx.MyApplication
body: <p>I'm having a crash with an app published in Play Store that happens very infrequently (&lt; 1%) and I cannot find the cause.</p>
  
  <p>The app crashes at launch, and in Play Store console I have the crash logged with the following stack trace:</p>
  
  <pre><code>    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6077)
      at java.lang.reflect.Method.invoke(Method.java)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.xxx.MyApplication
      at com.xxx.xxx.activities.BaseActivity.getApplicationComponent(BaseActivity.java:83)
      at com.xxx.xxx.views.activities.BaseActivity.onCreate(BaseActivity.java:65)
      at com.xxx.xxx.views.activities.startup.StartupActivity.onCreate(StartupActivity.java:78)
      at android.app.Activity.performCreate(Activity.java:6664)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
      ... 9 more
  </code></pre>
  
  <p>The crashing method <code>getApplicationComponent</code> looks like this:</p>
  
  <pre><code>public ApplicationComponent getApplicationComponent() {
      return ((MyApplication) getApplication()).getApplicationComponent();
  }
  </code></pre>
  
  <p>And the manifest references my custom application class:</p>
  
  <pre><code>&lt;application
          android:name=".MyApplication"
  </code></pre>
  
  <p>The app is installed in a few hundred thousand devices (same apk) and it works perfectly.</p>
  
  <p>So far this only seems to happen on some Nexus 5X/6P devices running Nougat.</p>
  
  <p>I'm out of ideas. Does anyone has any clue why this would happen only a few times?</p>
  
  <p><strong>edit</strong></p>
  
  <p>MyApplication class:</p>
  
  <pre><code>public class MyApplication extends Application {
      private ApplicationComponent applicationComponent;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          this.applicationComponent = this.createApplicationComponentBuilder().build();
      }
  
      public ApplicationComponent getApplicationComponent() {
          return this.applicationComponent;
      }
  
      protected DaggerApplicationComponent.Builder createApplicationComponentBuilder() {
          return DaggerApplicationComponent.builder()
                  .applicationModule(new ApplicationModule(this));
      }
  }
  </code></pre>