简体   繁体   中英

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

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"
    }
}

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM