java.lang.ClassCastException: cyh cannot be cast to dalvik.system.BaseDexClassLoader

Hello! We have made google instant app on defold, but on some devices app crashes on startup with this error:

03-04 13:54:30.288: E/AndroidRuntime(5709): FATAL EXCEPTION: main
03-04 13:54:30.288: E/AndroidRuntime(5709): Process: com.adoregames.coastalhill, PID: 5709
03-04 13:54:30.288: E/AndroidRuntime(5709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adoregames.coastalhill/com.dynamo.android.DefoldActivity}: java.lang.ClassCastException: cyh cannot be cast to dalvik.system.BaseDexClassLoader
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread.-wrap14(ActivityThread.java)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.os.Looper.loop(Looper.java:154)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread.main(ActivityThread.java:6776)
03-04 13:54:30.288: E/AndroidRuntime(5709): at java.lang.reflect.Method.invoke(Native Method)
03-04 13:54:30.288: E/AndroidRuntime(5709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
03-04 13:54:30.288: E/AndroidRuntime(5709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
03-04 13:54:30.288: E/AndroidRuntime(5709): Caused by: java.lang.ClassCastException: cyh cannot be cast to dalvik.system.BaseDexClassLoader
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.NativeActivity.onCreate(NativeActivity.java:164)
03-04 13:54:30.288: E/AndroidRuntime(5709): at com.dynamo.android.DefoldActivity.onCreate(DefoldActivity.java:205)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.Activity.performCreate(Activity.java:6955)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
03-04 13:54:30.288: E/AndroidRuntime(5709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
03-04 13:54:30.288: E/AndroidRuntime(5709): … 9 more
03-04 13:54:30.295: E/Isotope(3758): UID: [10236] PID: [3758] IActivityManagerProxy : Instant app: com.adoregames.coastalhill crashed: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adoregames.coastalhill/com.dynamo.android.DefoldActivity}: java.lang.ClassCastException: cyh cannot be cast to dalvik.system.BaseDexClassLoader
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread.-wrap14(ActivityThread.java)
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
03-04 13:54:30.295: E/Isotope(3758): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 13:54:30.295: E/Isotope(3758): at android.os.Looper.loop(Looper.java:154)
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread.main(ActivityThread.java:6776)
03-04 13:54:30.295: E/Isotope(3758): at java.lang.reflect.Method.invoke(Native Method)
03-04 13:54:30.295: E/Isotope(3758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
03-04 13:54:30.295: E/Isotope(3758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
03-04 13:54:30.295: E/Isotope(3758): Caused by: java.lang.ClassCastException: cyh cannot be cast to dalvik.system.BaseDexClassLoader
03-04 13:54:30.295: E/Isotope(3758): at android.app.NativeActivity.onCreate(NativeActivity.java:164)
03-04 13:54:30.295: E/Isotope(3758): at com.dynamo.android.DefoldActivity.onCreate(DefoldActivity.java:205)
03-04 13:54:30.295: E/Isotope(3758): at android.app.Activity.performCreate(Activity.java:6955)
03-04 13:54:30.295: E/Isotope(3758): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
03-04 13:54:30.295: E/Isotope(3758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
03-04 13:54:30.295: E/Isotope(3758): … 9 more

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="{{android.package}}"
        android:versionCode="{{android.version_code}}"
        android:versionName="{{project.version}}"
        android:installLocation="auto"
xmlns:dist="http://schemas.android.com/apk/distribution"
android:targetSandboxVersion="2">

<dist:module dist:instant="true" />

    <uses-feature android:required="true" android:glEsVersion="0x00020000" />
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="28" />
    <application
        {{#has-icons?}}
        android:icon="@drawable/icon"
        {{/has-icons?}}
        android:label="{{project.title}}" 
        android:hasCode="true"
        android:name="android.support.multidex.MultiDexApplication">
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
			
        <activity android:name="com.dynamo.android.DefoldActivity"
                android:label="{{project.title}}"
                android:configChanges="orientation|screenSize|keyboardHidden"
                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                android:screenOrientation="{{orientation-support}}"
                android:launchMode="singleTask">
            <meta-data android:name="android.app.lib_name"
                    android:value="{{exe-name}}" />
            {{#android.push_field_title}}
            <meta-data
                android:name="com.defold.push.field_title"
                android:value="{{android.push_field_title}}" />
            {{/android.push_field_title}}
            {{#android.push_field_text}}
            <meta-data
                android:name="com.defold.push.field_text"
                android:value="{{android.push_field_text}}" />
            {{/android.push_field_text}}
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="com.android.vending.BILLING" />

  <!-- Required to access Google Play Licensing -->
  <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

</manifest>
<!-- END_INCLUDE(manifest) -->

I haven’t seen this error before.
And google doesn’t give much info.

What devices doesn’t it happen on?
What OS version are they running?
DO you use native extensions, and if so, which ones?

We currently use Android SDK 26, so I’m not sure you can simply change it to targetSdkVersion=28 and expect it to work?

Yes, I did not find any useful info too.
We have two devices that reproduce this issue:
Samsung SM-A510F with Android 7.0
Samsung Galaxy Tab S3 with Android 7.0

Xiaomi Redmi Note 4 and Redmi 3 with Android 6.0 works fine.
Samsung Galaxy Tab A6 with Android 8.1 works fine.
Huawei Honor 8 with Android 8.0 works fine.

We use our own native extension to call Instant Apps Install API. So I have to integrate some Google Play Services jars extracted from aars.

Changing targetSdkVersion to 26 did not resolve the issue.

1 Like

It’s possible that your google play services collide with ours?
Have your removed google play services from the engine?

Yes.
.appmanifest:

armv7-android:
context:
excludeLibs: [“physics”,“LinearMath”,“BulletDynamics”,“BulletCollision”,“record”,“vpx”,“profilerext”,“facebookext”]
excludeJars: ["(.)/facebooksdk.jar","(.)/facebook_android.jar","(.)/google-play-services.jar", "(.)/android-support-v4.jar"]
excludeSymbols: [“ProfilerExt”,“FacebookExt”]
libs: [“physics_2d”,“record_null”]
linkFlags: []

Is the regex correct, or is it getting messed up by the forum?

Its supposed to be (.*)/google-play-services.jar:

platforms:
    armv7-android:
        context:
            excludeJars: ["(.*)/google-play-services.jar", "(.*)/android-support-v4.jar"]

Try unpacking your .apk to see if it contains the correct jar files

Yes it is the forum.

excludeJars: ["(.*)/facebooksdk.jar","(.*)/facebook_android.jar","(.*)/google-play-services.jar", "(.*)/android-support-v4.jar"]

And apk does not contain jars at all. I have only libCoastalHill.so.

Sorry, I meant the classes.dex file (the jar files gets assembled into that file)
It’s ofc a bit trickier then to check that you’re getting the correct versions.

I’m not sure I have any great ideas for you.
In the long run, we’d like to move google play services from the engine into its own native extension.
Then it’d be easier to maintain imo.

I think it is not about google play services.
If there are conflicts build fails. And crash occurs before all gms initialization. It is inside activity initialization.
Some people say that it is because of NativeActivity.
Maybe multidex. Is there a way to disable it?

I have converted dex to jar.classes-dex2jar.zip (2.8 MB)

Currently, there’s no way to disable multidex by an option.
But since the jar file you sent only contains ~3300 classes, it’s far from being multidexed.
You can try removing the manifest flag MultiDexApplication?

Unfortunately it did not help.
Error occurs at com.dynamo.android.DefoldActivity.onCreate(DefoldActivity.java:205)
Maybe you could look for that line.

Not sure what to deduce from that though.
We don’t seem to be doing anything special.

Yes It crashes inside super.onCreate on Android 7.0

NativeActivity.java:164 from Android SDK 24
BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();

I found this: http://www.voidcn.com/article/p-vrhunkto-bpq.html

We have internal projects that use Native Extensions, and run fine on Android 7.0.
I’m still guessing it’s related to either your android-support jars that you provide, or perhaps something special with those devices?

This might be related to the discussion in this thread; Game crashes on user's devices

1 Like

I use android-support jars from your admob plugins. It is very difficult to write extensions without aar support.
As was mentioned on that Chinese forum that code inside NativeActivity is different on Android 7.0 and 6.0. Our instant app works fine on 6.0 , 8.0, 8.1, 9.0. In 6.0 the code is more reliable and I think google fixed that problem in 8.+.
It is about some application class loader. I don’t know how to setup it.
Maybe it is related with issue which Johan_Beck-Noren mentioned.
I use Defold version 1.2.147
2fd3beebdf3810cae3bc023b0c87b956d7645804
5f1c6286a8bb1cfd8505517c1734f6d0666bf5df

Are your devices 64-bit?

Well, the CPUs are 64 bit.