Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intellij with Android SDK : lambda expressions are not supported in -source 1.7

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?

like image 779
CybeX Avatar asked Dec 14 '22 21:12

CybeX


2 Answers

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
    }
}
like image 63
braaks55 Avatar answered Feb 01 '23 23:02

braaks55


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.

like image 29
Code-Apprentice Avatar answered Feb 02 '23 01:02

Code-Apprentice