androidnoclassdeffounderrorbuilderstaticlayout

Why does calling StaticLayout.Builder throw the exception java.lang.NoClassDefFoundError?


Android M has changed the setters for StaticLayout and instead we have the Builder pattern. But with the following code:

StaticLayout staticLayout = StaticLayout.Builder.obtain("Hello", 0, "Hello".length(), getTextPaint(), 100).build();

I get the following exception:

Process: sharpdevs.com.legalapp, PID: 7695 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/text/StaticLayout$Builder;

I've tried everything, could this be an Android bug or am I missing a support library dependency?


Solution

  • It looks like this questions has already been solved in the comments, so to add it as an actual answer...

    StaticLayout.Builder was added in API 23, so trying to run it on an earlier API will cause an exception. You can use the standard StaticLayout constructors for earlier API versions.