4

Good day all

I know there are various questions on this issue, and I have visited quite a number of them, however they do not provide any "solution".

The general answer is to set the Language level to 8 (Allowing for lambdas) as I have done for the 2 modules built with grade, see below

enter image description here

and

enter image description here

I would like to confirm that I have Java 8 install

java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

When setting the Language level, it resolves the issue it has in the IDE,but when building my project to run on my devvice, I get this as an error:

Information:Gradle tasks [:app:assembleDebug]
/home/cybex/Documents/University/Year 5/Semester 2/WRAP302 - Advanced Programming 2/Assignments/Assignment1/Task1_SOS2/app/src/main/java/wrap302/nmu/task1_sos/SOSButton.java
Error:(15, 25) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
/home/cybex/Documents/University/Year 5/Semester 2/WRAP302 - Advanced Programming 2/Assignments/Assignment1/Task1_SOS2/app/src/main/java/wrap302/nmu/task1_sos/MainActivity.java
Error:(85, 34) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.054 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

Any thoughts?

UPDATE

For those suggesting it may be Gradle issues:

Project Gradle build

<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Task1_SOS2" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="java-gradle" name="Java-Gradle">
      <configuration>
        <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
        <option name="BUILDABLE" value="false" />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>

Module Gradle build

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "wrap302.nmu.task1_sos"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:gridlayout-v7:26.0.0-alpha1'
}

To those suggesting that I need to change my project language 8 (from 7), I have done so already (reeason for images as proof), yet my error persists, hence the reason for my question unless I am missing something in plain site.

Side question: something of concern, each time I change an the language level to 8, followed by a gradle change (i.e. added dependency, etc), the language level defaults to 7, is this normal?

CybeX
  • 2,060
  • 3
  • 48
  • 115
  • The error message seems pretty clear to me. You cannot use lambda expressions when compiling Java 7 source code. Lambdas are introduced in Java 8. – Code-Apprentice Jul 27 '17 at 21:39
  • 3
    Possible duplicate of [Java "lambda expressions not supported at this language level"](https://stackoverflow.com/questions/22703412/java-lambda-expressions-not-supported-at-this-language-level) – Code-Apprentice Jul 27 '17 at 21:42
  • A quick search gave the above hit. This should answer your question. – Code-Apprentice Jul 27 '17 at 21:43
  • Unless i am missing something in plain sight, i have already set the language level to 8 but the error persists – CybeX Jul 27 '17 at 21:44
  • 1
    I believe the settings you show in the screenshots only affect the IDE itself so that it handles code correctly in the editor. This is separate from the settings for Gradle and the compiler. – Code-Apprentice Jul 27 '17 at 21:46
  • 1
    See this comment: https://stackoverflow.com/questions/22703412/java-lambda-expressions-not-supported-at-this-language-level#comment53386555_22704620 – Code-Apprentice Jul 27 '17 at 21:47

3 Answers3

8

I am using Android Studio 3.0 Beta 4. With this version adding the following lines to build.gradle (Module:app) does the trick. See also: https://developer.android.com/studio/write/java8-support.html

android {
...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
braaks55
  • 105
  • 1
  • 6
4

Check your build.gradle files. They might have settings that override what you specified in the IDE. If this is the case, either remove the settings completely or modify them for Java 8 compatibility.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    The solution ended up requiring me to add the bild.gradle entry `sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8` and the `jackOptions enabled`. I attempted the other solutions refered to in your comment https://stackoverflow.com/questions/22703412/java-lambda-expressions-not-supported-at-this-language-level#comment53386555_22704620 to no avail. I have never had to do this before. note, I never explicilty used lambdas, nor IntelliJ with Android SDK, always Android Studio. Does AS automate this? – CybeX Jul 27 '17 at 22:36
  • @KGCybeX I have not tried to use lambdas in Android Studio, so I don't know how it handles them. I believe you would need to add the two configuration options as you specified here. Also note that [the Jack tool chain is officially deprecated](http://tools.android.com/tech-docs/jackandjill). Android Studio 3.0 Canary supports Java 8 directly in the standard build tool chain. – Code-Apprentice Jul 27 '17 at 22:39
  • Please add the link and the jackOptions to your answer, incase anyone else has the same issue. Thanks for the help – CybeX Jul 27 '17 at 22:43
0
  1. Close Project.
  2. Remove /app/app.iml and /<name>.iml files.
  3. Reopen Project.
tim4dev
  • 2,846
  • 2
  • 24
  • 30