1

I am using a small compression library for short strings in java. Surprisingly when I run my application on my Nexus 7 (lollipop) it runs fine but when I run it on my ICS tab it give a noSuchMethod exception whenever I try calling CharBuffer.subSequence.

14016-14016/reach.project E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NoSuchMethodError: java.nio.CharBuffer.subSequence at reach.project.utils.MiscUtils.compress(MiscUtils.java:239)

Error on this line :

String temp = String.valueOf(charBuffer.subSequence(0, 1));

I am using JDK 7

compileOptions {
    sourceCompatibility VERSION_1_7
    targetCompatibility VERSION_1_7
}

Min SDK version 14

Any help is much appreciated.

Dexter
  • 1,710
  • 2
  • 17
  • 34

2 Answers2

0

It seems that this is a bug in the java compiler. Building with version 6 should resolve your problem.

According to this link and many others.

Kiril Aleksandrov
  • 2,601
  • 20
  • 27
  • How can there be a bug in Java compiler :S well anyway, thanks, I'll try building with java 6 – Dexter Dec 14 '14 at 06:45
  • Yep, this works but this way you are creating another ```String``` object which may not be the needed behavior in some cases. Just wanted to mention it :) – Kiril Aleksandrov Dec 14 '14 at 14:30
0

See detailed description of the phenomenon here: Exception in thread "main" java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer

Workaround for new compilers:

package green_green_avk.anotherterm.utils;

import android.os.Build;

import androidx.annotation.NonNull;

import java.nio.CharBuffer;

public final class Compat {
    private Compat() {
    }

    @NonNull
    public static CharBuffer subSequence(@NonNull final CharBuffer that,
                                         final int start, final int end) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            return that.subSequence(start, end);
        return (CharBuffer) ((CharSequence) that).subSequence(start, end);
    }
}