How to clear cache Android

you can call system service clearApplicationUserData() ( working only for >= KitKat version )

so you can do a check for version and then everything will go fine :) here is the code :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ((ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE))
                    .clearApplicationUserData();
    }

I hope this helps you in getting further

public static void trimCache(Context context) {
    File dir = context.getCacheDir();
    if(dir!= null && dir.isDirectory()){
        File[] children = dir.listFiles();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            File temp;
            for (int i = 0; i < children.length; i++) {
                temp = children[i];
                temp.delete();
            }
        }

    }

} 

this will delete cache

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}