Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Dex cannot parse version 52 byte code

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? Thanks in advance. -Hephaestus

like image 393
Hephaestus Avatar asked May 04 '16 06:05

Hephaestus


People also ask

Can Android run byte code?

The class file i.e the byte code for Android is first optimized even more to make it mobile friendly (Which usually has a custom format according to specifications of Dalvik VM),which differs from normal bytecode. Hence direct JAVA bytecode wont't run.

What is byte code Android?

Programs for Android are commonly written in Java and compiled to bytecode for the Java Virtual Machine, which is then translated to Dalvik bytecode and stored in .dex (Dalvik EXecutable) and .odex (Optimized Dalvik EXecutable) files; related terms odex and de-odex are associated with respective bytecode conversions.


2 Answers

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     } 
like image 24
Hesham Fas Avatar answered Sep 28 '22 06:09

Hesham Fas


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     } } 
like image 163
foolcage Avatar answered Sep 28 '22 06:09

foolcage