Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use lambda functions in Android with Java 1.8

The library I'm using is bychan, I can't even include it in a Nougat empty app project either loaded as module or through jar.

So this is an example of what was returned in Android Studio's messages:

Can someone explain what they mean by "unknown interface" in the errors below?

Error:TokenDefinitionBuilder.java:118-119: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Function
Error:TokenDefinitionBuilder.java:118-119: Lambda coming from jar file need their interfaces on the classpath to be compiled, unknown interfaces are java.util.function.Predicate
...
Error:com.android.jack.JackAbortException

This came from a library I've used. I thought adding the types in angle brackets would help? I don't know if it did anything though.

This is the line 118-119 in that library:

strings.stream().<StringMatcher>map(StringMatcher::new).map(
    m -> m.tryMatch(input, searchStart)).filter(Objects::nonNull).findFirst().orElse(null));

I'm not sure what it is complaining about? What should I do or change to evade this problem? I had thought that maybe Android doesn't support Lambda Expressions, but the error clearly stated "Lambda coming from...", which means it recognized I used a lambda expression.

Please help.


Update

I have included the library by compiling it in a jar inside another jar library, if that helps. I'm new to the world of Android if you can't tell.

Updates 2

I don't think this is related to the API version? I've set my build target to 25. But the build is still failing.

Also, if you look below, I don't see an option for 1.8? But I've set the build JDK to my 1.8 JDK location.

enter image description here

enter image description here


Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "this.is.where.i.have.my.id"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        jackOptions {
            enabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:support-v4:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support:preference-v7:25.0.0'
    compile 'com.android.support:preference-v14:25.0.0'
    compile 'com.google.android.gms:play-services-appindexing:9.8.0'
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.8.0'
    compile files('libs/my_lib_that_contains_lambda.jar')
}
like image 988
Daniel Cheung Avatar asked Jan 19 '17 07:01

Daniel Cheung


People also ask

Does Java 1.8 support lambda expressions?

Lambda Expressions were added in Java 8.

Is lambda introduced in Java 8?

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .

Does Java 11 support lambda expressions?

From Java 11 var can also be used for lambda parameter types. Here is an example of using the Java var keyword as parameter types in a lambda expression: Function<String, String> toLowerCase = (var input) -> input.

Does Java 7 support lambda expressions?

From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .


1 Answers

I would not suggest to use java 8. It is not supported nowadays. But, even, it will: remember, that each devices has got its own java machine that will not update. I mean, if you wanna be sure your app works properly setup your project as java 6. Old devices this never support up-to-date JMV

Read this discussion too

Which JDK version (Language Level) is required for Android Studio?

like image 53
Vyacheslav Avatar answered Oct 22 '22 12:10

Vyacheslav