Language Level Java 8 (Retrolambda) on AndroidStudio 4+?

13,268

Solution 1

It is removed on new project, but it is available in the properties after the project is created. Note that I haven't actually compiled with any new Java 8 features yet, so let's hope that works.

Right click on your project, go to Open Module Settings. I think there's a bug somewhere, as once I navigate away from those language settings tab, I can never get back to it.

To work around that, you can edit MyProject/.idea/misc.xml and look for the languageLevel attribute.

To enable Project settings once you navigate away, edit MyProject/.idea/workspace.xml and search for project.structure.last.edited. Set the value to "Project".

Solution 2

I've found that you can set the "language level" to Java 8 in Android Studio by modifying the build.gradle to include the following:

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Share:
13,268

Related videos on Youtube

spierce7
Author by

spierce7

Updated on September 15, 2022

Comments

  • spierce7
    spierce7 almost 2 years

    So, someone backported Lambdas for Java 8 back to Java 6, and 7. Apparently it also works for Android. The project is called Retrolambda. I wanted to play around with this on AndroidStudio, but it seems in recent version they have removed the ability to set Java 8 as the Language Level (so I don't get compile errors while using Lambdas). Does anyone know a way around this? I imagine it'd have something to do with the fact that AndroidStudio is a modified IntelliJ. Any help would be appreciated.

  • spierce7
    spierce7 over 10 years
    I can't find the setting in the properties after the project is created. It looks like they removed it there too. Please give detailed instructions.
  • spierce7
    spierce7 over 10 years
    That actually is what I was hoping for. Thanks!
  • spierce7
    spierce7 over 10 years
    Update: This option has just been added back into the latest build of Android Studio.
  • HolySamosa
    HolySamosa about 10 years
    FYI: I'm seeing the same bug (?) with the General settings disappearing from the Module Settings screen.
  • spierce7
    spierce7 about 10 years
    @HolySamosa you could try to just switch to IntelliJ. It's almost the exact same as Android Studio due to it being the base for Android Studio, and you can better control this type of stuff.
  • Paul Verest
    Paul Verest over 9 years
  • IgorGanapolsky
    IgorGanapolsky over 9 years
    @joe My .idea/misc.xml already has languageLevel="JDK_1_8". Yet I am experiencing this error.
  • Papipo
    Papipo about 9 years
    What happens if I have a module which is not an android project? I can't use this solution there.
  • Brent Aureli
    Brent Aureli over 8 years
    this worked, I can compile and run but android studio still flags the code as error(red underlined ect..) in the editor. Any suggestions?
  • azwar_akbar
    azwar_akbar about 8 years
    Great solution. Thanks.
  • Dino Tw
    Dino Tw over 7 years
    Android does not support all Java 8 language features, developer.android.com/guide/platform/j8-jack.html