6

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')
}
Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
  • What is your destination Android API level. Should be supported with [API Level 23](https://developer.android.com/guide/platform/j8-jack.html). – PeterMmm Jan 19 '17 at 07:13
  • @PeterMmm Wait, I have min 21. Let me try updating it . I've only checked `java.util.function.*` is supported in API Level 24 – Daniel Cheung Jan 19 '17 at 07:18
  • Can you add the gradle file? – Iulian Popescu Jan 19 '17 at 07:30
  • @IulianPopescu Added – Daniel Cheung Jan 19 '17 at 07:32
  • On what Android are you trying to run the app? – Iulian Popescu Jan 19 '17 at 08:13
  • @IulianPopescu That is irrelevant since I'm not even able to build the project. – Daniel Cheung Jan 19 '17 at 08:13
  • 2
    I'm not sure, but this might be related to https://code.google.com/p/android/issues/detail?id=211386 – Sartorius Jan 19 '17 at 08:44
  • @Sartorius But I see some other people can get Lambda working by browsing SO :( – Daniel Cheung Jan 19 '17 at 09:15
  • "other people can get Lambda working". In a library (not the app itself)? With Jack? Can you share a link? – Sartorius Jan 19 '17 at 09:19
  • @Sartorius I can't because I have no proof they used a library in a jar. They did it with Jack though. Do you have any idea what the error message mean? Since the library itself is open source and I can edit it, if I know what it's complaining about, I may be able to fix it without removing all my Lambdas. – Daniel Cheung Jan 19 '17 at 09:21
  • 1
    Unfortunately not. I'm still using [retrolambda](https://github.com/orfjackal/retrolambda), not Jack. No problems with libraries there. – Sartorius Jan 19 '17 at 09:25
  • I'd create a simple test. Replace the library with a mock that has one single lambda expression, using a `java.lang.Runnable` as functional interface. If this results in `unknown interfaces are java.lang.Runnable` I'd say it is close to a proof that @Sartorius is correct. I've seen complaints about this [over](http://stackoverflow.com/questions/39683608/android-jack-lambda-coming-from-jar-file-need-their-interfaces-on-the-classpath) and [over](https://www.reddit.com/r/androiddev/comments/5nagq8/using_libraries_which_contain_java_8_lambdas_fail/) but nobody seems to know how to deal with it. – Stefan Zobel Jan 19 '17 at 14:56
  • 1
    A slightly hackish workaround would be to compile bychan to Java 7 using retrolambda. You would have to replace the six interface default methods (5 in Lexeme, 1 in Token) by abstract methods, and add the corresponding default implementation in the implementing classes. Should be doable in an hour or two. – Sartorius Jan 20 '17 at 13:07
  • @Sartorius I was thinking of doing that actually, might go do that later. – Daniel Cheung Jan 20 '17 at 13:08

3 Answers3

1

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?

Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • I don't aim for a practical use of this app. Just learning Android. But after all I've seen, I think Android is quite messed up. E.g. `View.getTextSize()` returns `px` and `View.setTextSize(size)` requires `sp`. – Daniel Cheung Jan 19 '17 at 07:34
  • @DanielCheung, you will be surprised in future thst some APIs can work unpredictable: depends on both vendors and device models – Vyacheslav Jan 19 '17 at 08:46
1

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
}
Aria Pahlavan
  • 1,338
  • 2
  • 11
  • 22
  • 1
    I just tried changing the minsdk setting to 24 and there was the same error – Daniel Cheung Jan 19 '17 at 07:21
  • Add defaultConfig { ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }` to you build.gradle file – Aria Pahlavan Jan 19 '17 at 07:40
  • Have you seen my gradle update. I have the exact same thing. – Daniel Cheung Jan 19 '17 at 07:44
  • I see. Um, you mentioned that your min SDK is 24 but I see in the provided Gradle file that you have *minSdkVersion 21* . Just to make sure it's clear, could you try changing that to *minSdkVersion 25* – Aria Pahlavan Jan 19 '17 at 07:50
  • Same thing happened. Nothing changed. I don't believe the API matters at this point because the app can't even be built – Daniel Cheung Jan 19 '17 at 07:55
  • Would you be able to run `java -version` in either command prompt or terminal (depending on you OS) and verify that you have Java 8 properly installed on your system? – Aria Pahlavan Jan 19 '17 at 17:04
  • 2
    I have it properly installed because I only have Java 8 installed – Daniel Cheung Jan 19 '17 at 17:09
0

Overall set up looks good to me except for the Java JDK 1.8 part.You need to set a correct path to JDK installation folder in Android Studio in order to use all the new Java 8 features.please verify your JDK location

ishwor kafley
  • 938
  • 14
  • 32
  • I did, I've set the build JDK to my 1.8 JDK location. – Daniel Cheung Jan 19 '17 at 08:24
  • can you verify that you have properly installed java 1.8 in your system? Have you added java to the system path? IDE's not letting you use java 1.8 cause its clear that you messed something up while setting/installing the java to your system. – ishwor kafley Jan 19 '17 at 08:35