不能在Android中使用带有Java 1.8的lambda函数 [英] Cannot use lambda functions in Android with Java 1.8

查看:2762
本文介绍了不能在Android中使用带有Java 1.8的lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的库是








Gradle



  apply plugin:'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion25.0.0
defaultConfig {
applicationIdthis.is.where.i.have.my.id
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.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(包括: ['* .jar'],dir:'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
排除组:'com .android.support',module:'support-annotations'
})
compile'c​​om.android.support:appcompat-v7:25.0.1'
compile'c​​om.android.support :support-v4:25.0.0'
编译'com.android.support:design:25.0.0'
编译'com.android.support:preference-v7:25.0.0'
编译'com.android.support:preference-v14:25.0.0'
compile'c​​om.google.an droid.gms:play-services-appindexing:9.8.0'
testCompile'junit:junit:4.12'
compile'c​​om.google.code.gson:gson:2.8.0'
编译文件('libs / my_lib_that_contains_lambda.jar')
}


解决方案

Android SDK< 24不支持 java.util.function.Function java.util.function.Predicate 接口,因为Java 8允许包含默认方法的功能接口,仅在SDK 24及更高版本中受支持。当我使用消费者时,我遇到了这个问题,我修复它的方法是创建我自己的 java.util.function.Consumer 接口的小丑,没有所有默认方法。
您还可以通过将应用的最低SDK增加到24来解决此问题。



更新



将以下内容添加到 build.gradle 文件

  defaultConfig {

jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_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.


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')
}

解决方案

Android SDK < 24 does not support java.util.function.Function and java.util.function.Predicate interfaces because Java 8 allows functional interfaces to contain default methods which is only supported in SDK 24 and above. I had this problem when I was using consumers, and the way I fixed it was to create my own clown of java.util.function.Consumer interface without all the default methods. You can also fix this issue by increasing the minimum SDK of your app to 24.

Update

Add the following to your build.gradle file

defaultConfig {

  jackOptions {
    enabled true
  }
}
compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

这篇关于不能在Android中使用带有Java 1.8的lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆