94

I just switched to Android Studio 2.1 and this error showed up when trying to compile an app the was previously working:

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.

I had already updated the main project's gradle.build file to force Java 1.7 code generation:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        apply plugin: 'java'
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
}

I had also updated the module gradle.build as follows to set the java version:

android {
compileSdkVersion 19
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.abc.def"
    minSdkVersion 19
    targetSdkVersion 19
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

The submodule being built with Maven. In the pom.xml file I have also tried to force 1.7 code generation.
I understand that I am using an assembly artifact, which incorporates subordinate modules, but i have not changed any of the subordinate modules and the resulting .jar file for the module ran fine last time I compiled.

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId> <!-- maven-compiler-plugin -->
            <version>2.6</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target> 
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My question:

  1. Is this an Android Studio 2.1 problem? Have others seen it?
  2. Assuming this is my error and since the error message gives no help in finding the bad module, are there any recommendations on finding the V52 code? I can't just omit the libraries without breaking large amount of code. Can one inspect a .jar file to find the code revision?
starball
  • 20,030
  • 7
  • 43
  • 238
Hephaestus
  • 4,337
  • 5
  • 35
  • 48
  • 1
    I'm currently facing this error right now. Any luck for solutions? – MetaSnarf May 05 '16 at 06:38
  • Me too updated Android Studio to 2.1. Since then I am facing this problem. Did you got any solution? – Suresh Kumar May 05 '16 at 11:54
  • An earlier error message (that has since gone away) suggests that the pubnub jar file was part of the problem. So we commented out every reference to pubnub and it now compiles and runs. I believe the error message went away when we added the compiler directives (shown above) to force the code to "1.7", however it seems that some of the 1.8 code was still leaking through. – Hephaestus May 05 '16 at 16:39
  • Here is another SO discussion which relates: http://stackoverflow.com/questions/36968728/android-studio-testing-library-dependencies-that-have-been-compiled-using-java. But which does not answer the question, other than to say "start with a simpler test project". – Hephaestus May 05 '16 at 16:41
  • 1
    The only thing we did was to pull out the PubNub library and replace with an older verison. That seemed to fix it. But in this case, we tested by commenting out the library import and its method calls and determining that it was at fault. But the PubNub library was loosely integrated and we could comment it out pretty easily. If we had many libraries with tight integration, it would be painful. – Hephaestus May 23 '16 at 07:22

15 Answers15

89

just use java 1.8 with Android Studio 3.0+ and set following works for me: it seems need the latest build tools

classpath 'com.android.tools.build:gradle:3.0.0'

and

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        ...        
        //jackOptions { // DEPRECATED
            //enabled true
        //}
    }
    dexOptions {
        incremental true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
foolcage
  • 1,084
  • 7
  • 9
  • 1
    Thank you. However, I am building to SDK 19 and I note that you are on 23. I thought Java 8 was only for Android N. I don't think that I can use Java 8 and still be backwards compatible to 19. Am I incorrect? – Hephaestus May 23 '16 at 07:18
  • 3
    I compiled code with java 8 while targeting Android N, but ran app on Android 16 with no problems. You can test it yourself – Deepscorn Jun 15 '16 at 14:44
  • Example: https://github.com/Deepscorn/libgdx-gradle-template/blob/master/android/build.gradle – Deepscorn Jun 15 '16 at 15:25
  • 7
    Apparently dexOptions.incremental is no longer required since it defaults to true, see https://stackoverflow.com/questions/37522668 – devconsole Oct 06 '16 at 13:51
  • 1
    Please be sure you understand the limitations of using java 8 and that not all language features are backwards compatible. https://developer.android.com/guide/platform/j8-jack.html – TrevJonez Nov 02 '16 at 00:54
  • 1
    Using Java 8 can lead to issues while trying to resolve dependencies between modules in an Android project built with Gradle. I had to rollback to Java 7 because jack compiler did not manage to make my 2 modules know each other. – Alex Nov 02 '16 at 10:23
  • Just to add, I had the same issue, I did not make any changes in the submodules project or iml file but just changed it to android.compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } in my main project.. and was able to compile the project and install. Jackoptions is definitely required, but did not need to apply pluin java for my prject. – Jalpesh Dec 31 '16 at 06:41
  • 3
    Note that "The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process." – Jonik Feb 21 '17 at 15:34
  • Note also that `jack is deprecated` now and should not be used – Nexen Aug 10 '17 at 05:26
  • I have a similar kind of issue but above code didn't worked. here's the link https://stackoverflow.com/questions/51720018/obfuscation-minifyenabled-true-not-working-in-debug-and-release/51739504?noredirect=1#comment90440186_51739504 – Naveen Aug 08 '18 at 07:16
16

If you have a module with a java library that is not Android-specific, this should work: apply plugin:'java'

Put it at the top of the build.gradle file, then rebuild.

    apply plugin: 'java'
    apply plugin: 'jacoco'

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.11'

        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
PLNech
  • 3,087
  • 1
  • 23
  • 52
Hesham Fas
  • 876
  • 1
  • 9
  • 20
8

If you use org.jetbrains:annotation:15 and retrolambda plugin then remove line compile org.jetbrains:annotations:15.0 from your build.gradle and the error will disappear. It works for me.

tir38
  • 9,810
  • 10
  • 64
  • 107
0wl
  • 836
  • 8
  • 19
7

Possibly, some of your dependencies was compiled with Java 8, not for Android especially. Try to switch that dependencies to older version. I don't exactly know which library you should downgrade, because you haven't attached a list of dependencies of your main module.

For example: I had the same problem. After hours of searching, I've found that library org.codehaus.httpcache4j.uribuilder:2.0.0 requires Java 8, as of github. So, when i've switched to 1.1.0, project has been successfully builded and deployed.

fobo66
  • 410
  • 7
  • 20
  • fobo66: Yes, I agree. This is pretty much what we did. I think, unfortunately, more and more libraries will soon be compiled with Java 8 and this will then be a common issue. This seems just like the world of Python where many libraries seem to still be on 2.6. – Hephaestus May 23 '16 at 07:25
  • Perhaps we will soon find, like Python, that all libraries are available separately as both J7 and J8 versions. – Hephaestus May 23 '16 at 07:25
7

Try to add to main build.gradle in section allprojects

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

or add this in dependencies

    sourceCompatibility = 1.7
    targetCompatibility = 1.7

in all modules manually

vitalinvent
  • 433
  • 1
  • 5
  • 11
7

I was able to solve this issue by adding the following lines:

jackOptions {
    enabled true
}

to defaultConfig in build.gradle file.

You can follow the guidelines for Java 8 on the link - https://developer.android.com/guide/platform/j8-jack.html

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
Faisal Ali
  • 71
  • 1
  • 2
5

I had the same problem with the greendao-generator dependency. I mistakenly added that dependency into my build.gradle (compile 'org.greenrobot:greendao-generator:3.1.0') and AndroidStudio showed me the same error message.

Probably it's because that module has been compiled with Java 8.

So I removed that dependency from my build.gradle and all compiled happily :)

2

I solved this problem as belowed :

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
李歆扬
  • 21
  • 1
2

Switching off Instant Run in Android Studio 2.2 with Gradle plugin 2.2.2 fixed it for me. Switching back to an older version of the Gradle plugin (such as 2.2.0) also fixed it, but that's less desirable imho.

Stephan Bezoen
  • 166
  • 2
  • 5
2

This happened to me with Android Studio 2.3.3. The solution I found was to delete the build folder and then Rebuild Project. It was as simple as that.

mp501
  • 666
  • 8
  • 12
1

I also faced same error in Android 2.3.3, after adding few JAR depencies. Problem was due to the depenecy io.netty:netty-all:4.1.16.Final. This 4.1.16 version JAR was compiled with Java 1.8 and all others was generated with Java 1.7.

This get resolved, after including older version of netty(which was generated with Java 1.7) in my build.gradle file.

compile 'io.netty:netty-all:4.1.5.Final'

rashok
  • 12,790
  • 16
  • 88
  • 100
0

I have come across this issue when trying to upgrade to auto-value v 1.5 in Android Studio v 2.3.3. Auto-value 1.5 will presumably be compatible with AS 3 (It requires an updated java compiler)

For now auto-value 1.4.1 works.

0

I face the same issue with Reactive Location APIs Library for Android and RxJava 2.Just update build.gradle to 3.0.1 and reduce the Reactive Location APIs Library for Android and RxJava 2 library version from 1.0.4 to 1.0.3 It works fine in my case.

0

I have come across this issue when trying to import a jar compiled by jdk 1.8 in Android Studio 3.0. I tried all above solution, but none work. so, I asked the developer of that jar to re-compile it with jdk 1.7, and then it work well, not come across this issue again.

uncle long
  • 11
  • 5
0

If possible for you:

  1. Upgrade android tools to: classpath 'com.android.tools.build:gradle:3.0.0'
  2. buildToolsVersion "26.0.2"

This should take care of the issue.

Reference: https://developer.android.com/studio/write/java8-support

Kanika
  • 714
  • 8
  • 24