Class cast exception to same class on Android

孤街醉人 提交于 2019-11-28 07:32:01

Ok I where the problem is. Looks like Samsung on Android 5.0 probably still uses old implementation of multidex (used in Android L Preview), so if your app exceeds the 64k limit, it will crash in random places on your app where you use (ExampleClass) object cast.

More info on this problem can be found here and here.

We're also seeing this crash happen thousands of times per day. Crashlytics reports the crash as 100% Samsung devices (99% "SM G900F[Q]" device), and 100% Android 5.x. Seems to be another multidex issue on Samsung + Lollipop devices, as a.jaskev reports in #3.

Looks like we have to wait until Samsung resolve this problem. Right now all we can do is watching our spam on mailbox with bug reports :)

Actually there is a solution.

Samsung uses modified version of makeDexElements method (with additional arguments) and solution is to modify source code of MultiDex to call it only for this model:

    /**
     * A wrapper around
     * {@code private static final dalvik.system.DexPathList#makeDexElements}.
     */
    private static Object[] makeDexElements(
            Object dexPathList, ArrayList<File> files, File optimizedDirectory,
            ArrayList<IOException> suppressedExceptions)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {

        Method makeDexElements = findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class, ArrayList.class, ClassLoader.class);
        makeDexElements.setAccessible(true);
        return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory, suppressedExceptions,
                SamsungS5.class.getClassLoader());
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!