1

when I try to start my application in android. I get the following 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.

in build.gradle I have the following

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "jejapps.conexionremota"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    targetCompatibility = 1.7
    sourceCompatibility = 1.7
}

JAVA_HOME and I have the address of the java folder (C: \ Program Files (x86) \ Java \ jdk1.7.0_55)

thank you very much to all

  • "..is caused by *library dependencies* .." - check your libs directories and junit/that jbundle thing. One of them is compiled for Java 8, it's not your app or your (main module) gradle file that is causing this. – zapl Jun 10 '16 at 00:28
  • http://oi65.tinypic.com/s2ur1x.jpg there? – Jonatan Jourdan Jun 10 '16 at 00:35
  • where should i check if its compiled for java 8? @zapl – Jonatan Jourdan Jun 10 '16 at 00:47
  • Its either stated somewhere or is unfortunately quite complicated: http://stackoverflow.com/questions/3313532/what-version-of-javac-built-my-jar – zapl Jun 10 '16 at 00:52
  • jbundle `Build-Jdk: 1.6.0_29` junit `Build-Jdk: 1.6.0_45` which version of java would be? @zapl – Jonatan Jourdan Jun 10 '16 at 01:28
  • That's java 1.6 which is usually called java 6. We're currently at java 1.8 if you go by that version number (or 1.8.0_92 when you include the "update" number). So those two are ok. Is there maybe any hint in the build output about what it is currently processing? You can also try to clean the build (`gradle clean` if you build on the console, or http://stackoverflow.com/questions/16636848/equivalent-of-clean-build-in-android-studio - I guess it could also be something left over from an old build, in case you did build your code at one point with java 8. clean should get rid of it. – zapl Jun 10 '16 at 06:31

1 Answers1

2

I solve this problem by adding follow statements to project's gradle script(build.gradle):

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

so ,finally ,my project's build.gradle is like this:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}
allprojects {
    repositories {
        jcenter()
    }
    tasks.withType(JavaCompile) {
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }

}