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

17
我安装了Android Studio,试图从gradle导入项目时出现“解析错误”:

无法加载类'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'。

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

2
请查看此链接:https://dev59.com/KV4b5IYBdhLWcg3wnCpy - Emre Eran
4个回答

6

这个页面可能会帮助解决问题。他们的说法是:

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$1.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


4

我可以使用以下三种方法来修复这个错误信息。

  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 (模块:app)

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'])
}

4

我曾经有同样的问题。我在命令行上运行gradle,这个方法是可行的。

然后,在文件->设置->构建、执行、部署->构建工具->Gradle中,"使用本地gradle分发"被激活。将其更改为"使用默认的gradle wrapper(推荐)",就可以解决问题了。


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Jim In Texas
你救了我的命!在从1.4.1升级到1.5.1版的过程中,我遇到了这个问题。而这个答案起了作用。 - Better Shao

0

我曾经遇到过同样的问题,但是我找到了解决办法。

原因

这个问题是由于Android Gradle插件与Gradle版本不匹配引起的。

解决办法

在项目中编辑build.gradle文件。Gradle插件版本必须满足Android Studio的要求。

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 Studio、Android Gradle插件和Gradle之间的版本兼容性信息。

https://dev59.com/KV4b5IYBdhLWcg3wnCpy#29695756


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接