Search code examples
javaandroidapache-commons-text

Apache commons-text throwing NoClassDefFoundError on Android 4.4.4


I have a library which uses the Apache Commons Text library.

A test suite for my library runs fine in JRE. However, when I use the library in an Android app and try to run it on an old device (running Android 4.4.4 aka KitKat), it crashes with a NoClassDefFoundError. The offending call seems to be StringSubstitutor.replace().

What is the issue here?


Solution

  • As per the release history, Apache Commons Text requires Java 8 since version 1.4 (released in mid-2018), which is not fully supported on older Android versions.

    Specifically, one of the missing classes is java.util.Base64, which was introduced with Java 8. On Android it requires API 26 (Android 8) or higher.

    If you need to support Android back to version 7, going back to Apache Commons Text version 1.3 works, as long as you are not relying on any of the features introduced in later versions. (StringSubstitutor.replace() was already available back then.)

    If you decide to go down that route, bear in mind that you will be stuck with a library that (as of January 2023) is almost five years old, to support an OS that is at least six years old and for which security updates were discontinued more than three years ago.