Grpc-java: Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code.

Created on 2 Nov 2016  路  2Comments  路  Source: grpc/grpc-java

When I'm trying to build an APK for my project, I encountered 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.

Seems that some of the libraries are getting compiled into Java 8 bytecode, which won't be compatible with current javac. I had to use jack with the new bytecode which takes minutes on a debug build and it is very annoying.

My app/build.gradle is set up according to the one in protobuf-gradle-plugin:

apply plugin: 'com.android.application'

def gitSha1 = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()

apply from: '../signing.gradle'

android {
    compileSdkVersion 25
    buildToolsVersion "25"
    defaultConfig {
        applicationId "<some.application.id>"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 2
        versionName "1.0.0"
        buildConfigField "String", "GIT_SHA1", "\"${gitSha1}\""
        resValue 'string', 'application_id', applicationId
        jackOptions {
            enabled false
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}
apply plugin: 'com.google.protobuf'
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.2'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.1'
        }
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
                grpc {
                    option 'lite'
                }
                javalite {}
        }
    }
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
apply plugin: 'android-apt'

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

    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'

    compile 'com.google.android.gms:play-services-auth:9.8.0'

    compile 'com.google.protobuf:protobuf-lite:3.0.0'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
    compile 'io.grpc:grpc-core:1.0.1'
    compile 'io.grpc:grpc-okhttp:1.0.1'
    compile ('io.grpc:grpc-protobuf-lite:1.0.1') {
        // Otherwise Android compile will complain "Multiple dex files define ..."
        exclude module: 'protobuf-lite'
    }
    compile 'io.grpc:grpc-stub:1.0.1'
    compile 'javax.annotation:javax.annotation-api:1.3'

    compile "com.andkulikov:transitionseverywhere:1.6.9"
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
    compile 'com.jakewharton:butterknife:8.4.0'
    compile 'com.jakewharton.threetenabp:threetenabp:1.0.4'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'me.zhanghai.android.materialedittext:library:1.0.5'
    compile 'me.zhanghai.android.materialprogressbar:library:1.3.0'
}
$ uname -a
Linux ZH-Laptop 4.8.4-1-ARCH #1 SMP PREEMPT Sat Oct 22 18:26:57 CEST 2016 x86_64 GNU/Linux
$ java -version
openjdk version "1.8.0_112"
OpenJDK Runtime Environment (build 1.8.0_112-b15)
OpenJDK 64-Bit Server VM (build 25.112-b15, mixed mode)

Most helpful comment

Sorry for the noise. I've found out the offending jar by running gradle --debug assembleDebug, and it turned out to be javax.annotation:javax.annotation-api:1.3, which uses 1.8 bytecode while the 1.2 version of this artifact is built into 1.7 bytecode.

All 2 comments

I believe we limit gRPC to 1.6, can you try with that level?

Sorry for the noise. I've found out the offending jar by running gradle --debug assembleDebug, and it turned out to be javax.annotation:javax.annotation-api:1.3, which uses 1.8 bytecode while the 1.2 version of this artifact is built into 1.7 bytecode.

Was this page helpful?
0 / 5 - 0 ratings