Search code examples
androidcrashandroid-api-levels

Unable to use Consumer<T> of java functional interfaces in android app API level 22


I am new to android development. I am targeting API level 22 and with the following code, the app crashes:

Consumer<String> onError = new Consumer<String>(){
    public void accept(String t){
    }
}

I tried with a custom class implementing this interface and still the same behavior. Can someone explain it why.


Solution

  • Functional interfaces like java.util.Objects.Consumer from java8 cannot be accessed on devices targeted java7 or lower. According to documentation native support for functional interfaces is supported only from API 24