Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: ApiVersionImpl cannot be cast to java.lang.Integer

I have android + gradle project. The following exception appears when I try to start application:

'ClassCastException: com.android.build.gradle.internal.model.ApiVersionImpl cannot be cast to java.lang.Integer: com.android.build.gradle.internal.model.ApiVersionImpl cannot be cast to java.lang.Integer'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
    }
}

apply plugin: 'android'
apply plugin: 'android-apt'

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile files('libs/android-support-v4.jar')
    compile 'org.androidannotations:androidannotations-api:3.0.1'
    apt 'org.androidannotations:androidannotations:3.0.1'
}

android {
    compileSdkVersion 15
    buildToolsVersion '19.1'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 15
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}
like image 998
Serhii Bohutskyi Avatar asked Jul 08 '14 13:07

Serhii Bohutskyi


People also ask

What is ClassCastException in Java?

In this short tutorial, we'll focus on ClassCastException, a common Java exception. ClassCastException is an unchecked exception that signals the code has attempted to cast a reference to a type of which it's not a subtype. Let's look at some scenarios that lead to this exception being thrown and how we can avoid them.

What is ClassCastException in thread “main”?

Exception in thread “main” java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.String (java.math.BigDecimal and java.lang.String are in module java.base of loader ‘bootstrap’) We can fix the exception printing by means of converting the code in the below format:

Why can't I cast an integer to a long?

You cannot cast an Integer object to a Long object. Integer is not a subclass of Long (both are subclasses of Number). Were you trying to do that? orElse () is declared to take the generic type T as argument, so an object. So Java autoboxes your 0 to an Integer. Because 0 is an int. This also explains why 0L worked: now Java autoboxes to a Long.

Why does the last line throw a ClassCastException in Java?

The last line will throw a ClassCastException as it cannot transform a D ouble reference to Long. 4. Generic Types When using generics in Java, we must be wary of type erasure, which can lead to ClassCastException as well in some conditions.


1 Answers

There is bug report open to google. For Intellij 13.1.4 it is still failure.

Rolling back build tools version to 1.10.+ can help.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
    }
}

I realized that IDEA Intellij 14 EAP has no such problem and support Android L as well.

like image 200
Andrii Liubimov Avatar answered Oct 17 '22 09:10

Andrii Liubimov