No static method ThreadLocal.withInitial() : commons-io dependency

617 views Asked by At

I am using commons-io dependency in my Android Studio project.

implementation 'commons-io:commons-io:2.11.0'

But I am seeing these weird crashes in Crashlytics. These crashes are showing up on Android 7 and below.

Fatal Exception: java.lang.NoSuchMethodError: No static method withInitial(Ljava/util/function/Supplier;)Ljava/lang/ThreadLocal; in class Ljava/lang/ThreadLocal; or its super classes (declaration of 'java.lang.ThreadLocal' appears in /system/framework/core-oj.jar)
   at org.apache.commons.io.IOUtils.<clinit>(IOUtils.java:183)
   at org.apache.commons.io.IOUtils.closeQuietly(IOUtils.java:534)

I have also included Java 8 compatibility in my project.

android {

...
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
        jvmTarget = "1.8"
    }
}
2

There are 2 answers

0
Ali Asjad On

Internally commons-io is using ThreadLocal#withInitial API which is not part of desugared library, hence the method is not available on API levels lower than 26 (Android OREO)

To read more about this you can read the discussion of this thread on google issue tracker.

Solution:

We can use the previous versions of commons-io which are not using the ThreadLocal API. So i ended up using implementation 'commons-io:commons-io:2.5'

0
prem On

You can downgrade the library to version '2.8.0' as after this version they are using ThreadLocal which is not supported in android 7.

So just use this in app/build.gradle

 implementation 'commons-io:commons-io:2.8.0'