Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird Android Studio error with Java 8

I just upgraded to Android Studio 3.0 from 2.3.3 and was eager to try the new Java 8 features now supported on pre-API 26 builds. I set sourceCompatibility and targetCompatibility to JavaVersion.VERSION_1_8 in my build.gradle file. Then, unfortunately, I got an error in the following class:

package com.zigzagworld.util;

import java.util.LinkedHashMap;

public class LRUCache<K, V> extends LinkedHashMap<K, V> {
    private final int maxSize;

    public LRUCache(int maxSize) {
        super(Math.min(16, (int) ((maxSize + 1) / 0.75f)), 0.75f, true);
        this.maxSize = maxSize;
    }

    @Override protected boolean removeEldestEntry(Entry<K, V> eldest) {
        return size() > maxSize;
    }
}

The error is:

Error:(13, 51) error: Entry is not public in LinkedHashMap; cannot be accessed from outside package

The editor itself shows no error at all; it only shows up as output from the Gradle task :app:compileDebugJavaWithJavac.

I'm using the default tool chain. I tried cleaning and rebuilding the project. I also tried invalidating the caches and restarting Android Studio.

I can avoid the error by changing the language back to 1.7 in my build.gradle file. Unfortunately, since my minSdkVersion is 17, I then wouldn't be able to use try-with-resources and other goodies.

Interestingly, I discovered that I can work around the error by using the fully qualifying name java.util.Map.Entry instead of Entry. I have no idea why this works, while the bare name Entry generates an error.

Is there some configuration setting I'm missing? Is this a known bug in AS 3.0?

like image 801
Ted Hopp Avatar asked Oct 28 '22 23:10

Ted Hopp


1 Answers

You must take the map from the Entry

@Override
 protected boolean removeEldestEntry (Map.Entry<K,V> eldest) {
  return size() > maxSize;
  }

this line Map.Entry

We need to get the Entry from the Map interface because the Entry interface was previously made, but it was defined in the latest updates to the Map interface. Now we have to call the Map to access the interface or Changing the package and folder caused this

like image 118
younes Avatar answered Nov 10 '22 15:11

younes