Wednesday, December 21, 2016

How to setup Android Studio for Java 8

Today, I wanted to move one of the project I was working on to Android Studio 2.2 to support Java 8. It's kind of amazing there is not enough information about how to convert an existing project to support Android Studio + Java 8.

1. In your project build.gradle add classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  So it should be like this

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

2. In your Module build.gradle add

apply plugin: 'me.tatarka.retrolambda'

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}


apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        ..
    }
    buildTypes {
        ...
    }
     
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
 ....
}