Android: Use Java 8 in the project-gradle

0

I'm starting a small project on Android and I found that by default the great features of Java 8 are not available: Lambdas, streams, Time API and others that I did not give

This is my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.wesosdqueso.reportesseguridad"
        minSdkVersion 15
        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(dir: 'libs', include: ['*.jar'])
    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.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

I'm not sure if I need to modify it, but I can not use LocalDate. The lambdas do work. And no, he does not want to go back to the nightmare of the Date class.

EDIT:

I already found the solution, I just had to change the API level to 26. Close this question.

    
asked by Wesos de Queso 17.10.2017 в 14:04
source

2 answers

0

If you want to enable Java 8 to use lambdas (et al) do the following:

If you want to enable Java 8 and Jack language functions for your project, write the following in your module level file build.gradle :

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

Actually the build.gradle shown in the question is configured. Check the documentation: Use Java 8 language functions

    
answered by 17.10.2017 в 16:31
0

Taken from the documentation

  

Android supports all functions of the Java 7 language and a   subset of Java 8 language functions that vary according to the   version of the platform.

     

For compatibility with functions of the Java 8 language, it is required   a new compiler called Jack. Jack is compatible only with   Android Studio 2.1 and later. Therefore, if you wish   to use functions of the Java 8 language, you must use Android Studio 2.1 to   compile your app.

link

In summary, you can use Java 8 but it will depend on the version of the platform you support. To use Lambdas on all platforms I would recommend Retrolambda ( link )

Outside the context of your question, to use a language with more features, more concise and more elegant I would recommend Kotlin. This is accepted by Google as the official language for Android, the learning curve is low and it is 100% interoperable with Java ( link ).

Greetings

    
answered by 17.10.2017 в 16:46