Often in Android code we use this pattern of storing data in the Application object and then accessing it via casting as such:
((MyApplicationObject)getApplication()).getDaggerComponent()
getApplication()
should always be MyApplicationObject
because I have specified it in the AndroidManifest.xml
. However, when you deploy this code, sometimes it causes a ClassCastException
and we get a different object instead.
How can this be?
What is the correct workaround for this issue?
Resolved in the comments: this is an Android bug, not one related to Dagger.
See this public Google IssueTracker link, #37137009: java.lang.ClassCastException: Cannot cast android.app.Application to com.xxx.xxApplication (ctate's comment in #17):
Unfortunately, for the time being, there doesn't seem to be much of an explanation or official workaround, but at least the issue is predominantly limited in scope to Android 7.0.