I needed to migrate from Android Studio to IntelliJ IDEA as I needed to do some other non-Android work in Java. I cloned my project from git and imported it into IDEA; however, I encountered an error with Gradle in the process. I've searched but could not find an answer that fixes my error.
Here is the Event Log
03:39:42 PM All files are up-to-date
03:39:42 PM 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
The settings.gradle (myproject is an example for SO, want to omit the actual name)
include ':myproject'
The build.gradle in the root directory (e.g. /myproject/build.gradle)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
and the other build.gradle (e.g /myproject/myproject/build.gradle)
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '20'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName '1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
versionNameSuffix "-alpha"
}
}
productFlavors {
}
packagingOptions{
exclude 'META-INF/DEPENDENCIES.text'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'commons-validator:commons-validator:1.4.0'
}
I presume it's something pertaining to the Gradle version or incompatibilities between Studio and IDEA but I'm not sure. Would highly appreciate an answer. Thanks.
I think it's because your Gradle project is too new for the version of the Android plugin that IntelliJ IDEA ships with.
There are a couple possible workarounds:
You can try to downgrade the version of the Android Gradle plugin in the build file until you hit a version it will work with. This will run into problems if you move back so far that other parts of it are incompatible. To do that, reduce the version number in the line:
classpath 'com.android.tools.build:gradle:0.12.+'
However, if you do this, know that this build file won't be compatible with modern releases of Android Studio. There isn't a good workaround to this at the moment.
Another approach would be to try the IntelliJ 14 EAP, which will likely have a later version of the plugin. Be aware that IntelliJ 14 is still in a very active phase of its development cycle, and there are likely to be bugs. Use at your own risk. http://confluence.jetbrains.com/display/IDEADEV/IDEA+14+EAP
Long-winded explanation:
There are many different pieces at work:
The plugin in #3 is the same plugin for both Android Studio and IntelliJ, but the problem is that it's evolving very quickly, and Android Studio is on a much quicker release schedule, and each release ships with the latest code. IntelliJ IDEA is on a less frequent release schedule, and to be honest, I'm not entirely sure what Android plugin it ships with; I haven't looked at their branching strategy.
Also evolving quickly is the Android plugin for Gradle. Often times, this moves in lockstep with new versions of Android Studio; to support new features we need in Android Studio, users need a newer minimum version of the Gradle plugin, and vice versa -- this is why there have often been forced migrations of the plugin version in the past.
So what's happening is that your build file needs a recent version of the Android Gradle plugin, which needs a recent version of the IntelliJ Android plugin, which is shipping in Studio but not the current release of IJ 13.
Things are settling down, so we hope this will improve in the future. In particular, version 1.0 of the Android Gradle plugin will work across platforms and will be compatible with future releases of all products for a long time, if not forever.
You have to use previous version of android tools. Version 0.10.+ works with Idea 13
So just put
classpath 'com.android.tools.build:gradle:0.10.+'
Unfortunately this will not work with Android studio nor Idea 14 EAP, and if you have coworkers working on the same project there is no good solution.
Option is to continue using Idea 13 with android tools 0.10.+ version, or migrate to EAP and/or Android studio and start using 0.11.+ version of android tools. :( NOTE: 0.12.+ will not work with Idea 14 EAP
Or you can try introducing the workaround that we use in the team while this does not stabilize:
In settings.gradle add
boolean fromIdea14 = System.properties["idea.version"]?.toString().equals("14")
org.gradle.api.internal.initialization.DefaultScriptHandler.metaClass.toolsVersion = fromIdea14 ? "0.11.+" : "0.10.+"
Then in build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'com.android.tools.build', name: "gradle", version: getBuildscript().toolsVersion
}
}
This will work for everyone using Idea 13 and for the guys using 14 EAP or AndroidStudio they should add to Gradle VM arguments -Didea.version=14.
Note: It seams that Idea does not evaluate the script in order to configure android projects by invoking gradle itself so getting other property or env variable will not work :( The only way is to specify gradle VM argument in settings.
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