Unable to load dex file: java.nio.HeapByteBuffer

I saw this error in a few other forum topics, but I couldn’t find a fix for this: On android I get the following error in logcat when my app launches:

12-06 12:34:24.476 17240 17240 E System  : Unable to load dex file: java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
12-06 12:34:24.476 17240 17240 E System  : java.io.IOException: Bad range
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.DexFile.createCookieWithArray(Native Method)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.DexFile.openInMemoryDexFile(DexFile.java:367)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.DexFile.<init>(DexFile.java:108)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.DexPathList.makeInMemoryDexElements(DexPathList.java:302)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.DexPathList.<init>(DexPathList.java:108)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:126)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.InMemoryDexClassLoader.<init>(InMemoryDexClassLoader.java:35)
12-06 12:34:24.476 17240 17240 E System  :      at dalvik.system.InMemoryDexClassLoader.<init>(InMemoryDexClassLoader.java:46)
12-06 12:34:24.476 17240 17240 E System  :      at java.lang.Runtime.nativeLoad(Native Method)
12-06 12:34:24.476 17240 17240 E System  :      at java.lang.Runtime.loadLibrary0(Runtime.java:1014)
12-06 12:34:24.476 17240 17240 E System  :      at java.lang.System.loadLibrary(System.java:1669)
12-06 12:34:24.476 17240 17240 E System  :      at com.roli.juce.Java.<clinit>(Java.java:9)
12-06 12:34:24.476 17240 17240 E System  :      at com.roli.juce.Java.initialiseJUCE(Native Method)
12-06 12:34:24.476 17240 17240 E System  :      at com.roli.juce.JuceApp.onCreate(JuceApp.java:13)
12-06 12:34:24.476 17240 17240 E System  :      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1158)
12-06 12:34:24.476 17240 17240 E System  :      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6399)
12-06 12:34:24.476 17240 17240 E System  :      at android.app.ActivityThread.access$1300(ActivityThread.java:248)
12-06 12:34:24.476 17240 17240 E System  :      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1830)
12-06 12:34:24.476 17240 17240 E System  :      at android.os.Handler.dispatchMessage(Handler.java:106)
12-06 12:34:24.476 17240 17240 E System  :      at android.os.Looper.loop(Looper.java:216)
12-06 12:34:24.476 17240 17240 E System  :      at android.app.ActivityThread.main(ActivityThread.java:7266)
12-06 12:34:24.476 17240 17240 E System  :      at java.lang.reflect.Method.invoke(Native Method)
12-06 12:34:24.476 17240 17240 E System  :      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
12-06 12:34:24.476 17240 17240 E System  :      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

The app still runs after this, but I’m having other problems and don’t know if this has anything to do with them. It looks like the exception happens at line 198 in juce_android_JNIHelpers.cpp at the following statement:

                byteCodeClassLoader = LocalRef<jobject> (env->NewObject (AndroidInMemoryDexClassLoader,
                                                                         AndroidInMemoryDexClassLoader.constructor,
                                                                         byteBuffer.get(), defaultClassLoader.get()));

Some debugging showed me that this happens when uncompressedByteCode.getDataSize() == 0 (I’m guessing that would cause a “Bad range” exception). I notice that several lines below at line 202 there’s a condition “if (uncompressedByteCode.getDataSize() >= 32)” that seems to guard against this. So to prevent the exception from happening I modified line 188 to read “if (sdkVersion >= 26 && uncompressedByteCode.getDataSize() >= 32)”. (BTW, I’m targetting sdk version 28).

Is this the right fix for this? Should I be getting uncompressedByteCode.getDataSize() == 0 here or is there something wrong with my build?

1 Like