4

why I replace JAVA 8 with JAVA 7,Android studio error?

> Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using  Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing android/support/v7/recyclerview/R$attr.class
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing android/support/v7/recyclerview/R$dimen.class
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing android/support/v7/recyclerview/R$id.class
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.  
 If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing android/support/v7/recyclerview/R$styleable.class 
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing android/support/v7/recyclerview/R.class
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:...while parsing com/example/administrator/comjarlib/R$anim.class
Error:Error converting bytecode to dex:
............

I think to use retroLambda in Android Studio.

Anyone facing similar issue / resolved can give some pointer to resolve it.

I am using Android Studio 2.1 Preview 3 with grade 2.10

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
allen218
  • 49
  • 1
  • 1
  • 2
  • Did you try adding those two lines to your build.gradle file? – ecnepsnai Mar 26 '16 at 05:17
  • 1
    Did you read the error log? `If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.` – ecnepsnai Mar 28 '16 at 04:13

2 Answers2

13

This one helped me. In your project's main build.gradle file add these lines of code into allprojects section (or add this section if it isn't there).

allprojects {

    tasks.withType(JavaCompile) {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }

}
Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37
Victor Nidens
  • 474
  • 1
  • 4
  • 11
  • Had this problem upgrading from Studio 2.1 to 2.2 with non-android Jars, this was the answer. – Autumn Sep 02 '16 at 21:28
  • This is how my build.gradle look like, but it does not work allprojects { repositories { jcenter() } tasks.withType(JavaCompile) { sourceCompatibility = 1.7 targetCompatibility = 1.7 } } – Ray Feb 27 '17 at 05:00
  • For me, this ended up working. It seems like the best solution if you have other dependencies that will also throw this error as well. – Andrea Thacker Mar 30 '17 at 19:21
5

If I don't get wrong gradle android plugin extends 'java' plugin, so you can just add these two lines in your "android" block in your build.gradle :

apply plugin: 'com.android.application'

android {

    targetCompatibility = '1.7'
    sourceCompatibility = '1.7'

    ...
}

Worked fine for me.

Nicolas Bossard
  • 1,077
  • 1
  • 10
  • 15