Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleteDirectory: java.lang.NoSuchMethodError: No virtual method toPath

Android Studio 3.4.2 Android 6.0

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'commons-io:commons-io:2.6'
    implementation 'edu.vt.middleware:vt-password:3.1.2'
    implementation 'org.apache.commons:commons-collections4:4.1'

    implementation "org.androidannotations:androidannotations-api:$AAVersion"

Before android 6.0 code success work

 import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
       import org.apache.commons.io.FileUtils;

private File myPhotoTempDir;
    if (myPhotoTempDir != null && myPhotoTempDir.exists()) {
                    FileUtils.deleteDirectory(myPhotoTempDir);
    }

on Android 6.0 I get runtime error:

FATAL EXCEPTION: main
Process: com.myproject.debug, PID: 7667
java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-libart.jar)
    at org.apache.commons.io.FileUtils.isSymlink(FileUtils.java:3107)
    at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1616)
    at com.myproject.profile.EditProfileFragment.goToPrevScreen(EditProfileFragment.java:915)
    at com.myproject.profile.EditProfileFragment_.access$201(EditProfileFragment_.java:20)
    at com.myproject.profile.EditProfileFragment_$3.run(EditProfileFragment_.java:95)

What is wrong with my code? Thanks.

like image 440
Alex Avatar asked Jul 10 '19 12:07

Alex


2 Answers

I have the same issue.

This code cause the same error that you described:

private void clearHttpCacheDirectory(String fileName) {
        File httpCacheDirectory = new File(getCacheDir(), fileName);
        try {
            FileUtils.deleteDirectory(httpCacheDirectory);
        } catch (Exception e) {
            Crashlytics.log(e.getMessage());
        }
    }

This lib works ok, but it looks old for me:

implementation group: 'commons-io', name: 'commons-io', version: '2.4'

But this one is crashed:

implementation group: 'commons-io', name: 'commons-io', version: '2.6'

So I decided to use 2.5 version, because if we look at official releases, version 2.6 has three release candidates, and no stable version yet. So this worked for me: https://github.com/apache/commons-io/releases

like image 161
yozhik Avatar answered Oct 24 '22 03:10

yozhik


I got that error using commons-io-2.7, but when I turned back to commons-io-2.5 all went good.

Got it from here: https://downloads.apache.org//commons/io/binaries/commons-io-2.5-bin.zip

like image 7
angarrido Avatar answered Oct 24 '22 02:10

angarrido