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
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.
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.
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 }
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With