Android Studio 无法加载 class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'

Android Studio Unable to load class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'

我安装了 Android Studio,当我尝试从 gradle 导入项目时,这个 解决错误 显示:

Unable to load class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'.

我删除了我的用户 .gradle 文件夹中的文件并尝试了不同的 gradle 版本。我不知道如何解决这个问题。

This 页面可能有助于解决问题。他们说的是:

So we leveraged this version to add a new artifact, named groovy-backports-compat23. This artifact shouldn’t be necessary for most of you, but if you face an error like:

Caused by: java.lang.ClassNotFoundException:
org.codehaus.groovy.runtime.typehandling.ShortTypeHandling    at
java.net.URLClassLoader.run(URLClassLoader.java:372)

in your project, then it means that a class has been compiled with Groovy 2.3+ but that you are trying to use it with an older version of Groovy. By adding this jar on classpath, you give a chance to your program to run. This may be particularily interesting for Gradle users that want to use a plugin built on Gradle 2+ on older versions of Gradle and face this error. Adding the following line to their build files should help:

buildscript {
    // ...
    dependencies {
         classpath 'some plugin build on gradle 2'
         classpath 'org.codehaus.groovy:groovy-backports-compat23:2.3.5'
    } }

Note that for now, this jar only contains the ShortTypeHandlingClass. Future versions may include more. - See more at: http://glaforge.appspot.com/article/groovy-2-3-5-out-with-upward-compatibility#sthash.mv7Y8XQv.dpuf

我可以使用这三种方法修复此错误消息。

  1. 使用最新的 gradle 版本
  2. 使用最新的 android SDK 和工具。
  3. 使用proguard-rules.pro

build.gradle(项目:xxxxx)

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

allprojects {
    repositories {
        mavenCentral()
    }
}

build.gradle(模块:应用程序)

apply plugin: 'android'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

我遇到了同样的问题。我从命令行 运行 gradle 并且确实有效。 之后找到File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle。 "Use local gradle distribution" 处于活动状态。将其更改为 "Use default gradle wrapper (recommended)" 并且有效。

我遇到了同样的问题。我找到了解决方案。

原因

此问题是由于 android gradle 插件与 gradle 版本不匹配引起的。

解决方案

在项目中编辑 build.gradle。 gradle 插件版本必须满足 android 工作室的要求。

dependencies {
   classpath 'com.android.tools.build:gradle:1.1.0'
}

并在 gradle/wrapper/gradle-wrapper.properties 中编辑 distrubutionUrl。 gradle 的版本必须满足 gradle 插件的要求。

distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip

您可以在本页

中找到 android 工作室、android gradle 插件和 gradle 之间的版本兼容性